Results 1 to 16 of 16

Thread: Storage and View -> imageViewer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Re: Storage and View -> imageViewer

    okay, i wrote a little testapp that stores an image (or every file you give it) into a BLOB-field. the db-file grows to the size of the given image, so it should be in it, although the chars i get if i do a select to qDebug() are only 8, all unprintable. So, here's my app:
    Qt Code:
    1. int main( int argc, char *argv[] )
    2. {
    3. QCoreApplication app( argc, argv );
    4. QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    5. db.setDatabaseName( "/home/stefan/source/qt4/cdlauncher/pictures.db" );
    6. if ( !db.open() )
    7. {
    8. qDebug() << "Opening failed";
    9. return 1;
    10. }
    11. qDebug() << "Opening successfull, using database " << db.databaseName();
    12. qDebug() << "filling an image into the DB...";
    13. QFile file( "/home/stefan/pic/space/191853main_image_feature_929_full.jpg" );
    14. QByteArray fileData;
    15. if ( file.exists() )
    16. {
    17. if ( file.open( QIODevice::ReadOnly ) )
    18. {
    19. fileData = file.readAll();
    20. file.close();
    21. }
    22. }
    23. else
    24. {
    25. qDebug() << "File " << file.fileName() << " could not be located";
    26. }
    27. QSqlQuery query("UPDATE pictures SET data=? WHERE rowid=1;");
    28. query.addBindValue(fileData);
    29. query.exec();
    30. qDebug() << query.lastError();
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 
    that works everything is hardcoded, i know, but its only a test to see how it works.
    So, now i want to retrieve the data. the normal way to display an image is
    Qt Code:
    1. imgLabel->setPixmap(QPixmap::fromImage(QImage("filename")));
    To copy to clipboard, switch view to plain text mode 
    After some testing, reading and coffee-ing, i found the following way:
    Qt Code:
    1. int main( int argc, char *argv[] )
    2. {
    3. Q_INIT_RESOURCE( application );
    4. QApplication app( argc, argv );
    5. QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    6. db.setDatabaseName( "/home/stefan/source/qt4/cdlauncher/pictures.db" );
    7. if ( !db.open() )
    8. {
    9. qDebug() << "Opening failed";
    10. return 1;
    11. }
    12. QSqlQuery query( "SELECT data FROM pictures WHERE rowid=1;" );
    13. QSqlRecord record = query.record();
    14. query.next();
    15. qDebug() << query.lastError();
    16. QByteArray ba = query.value(0).toByteArray();
    17. QWidget *wid = new QWidget();
    18. QLabel *imgLabel = new QLabel( wid );
    19. QPixmap *pic = new QPixmap();
    20. pic->loadFromData( query.value( record.indexOf( "data" ) ).toByteArray() );
    21. imgLabel->setPixmap( *pic );
    22. wid->show();
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 
    it successfully reads out the image and displays it...
    a SELECT-query seems not to like the QSqlQuery::exec () method. So i used a single next() for that...
    So far, everything is fantastic, thank you very much Thomas and jacek

  2. #2
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Storage and View -> imageViewer

    One last issue.

    Make sure you delete stuff which you created with new.
    There are 10 people in the world. Those who understand binary and those who don't.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Storage and View -> imageViewer

    Quote Originally Posted by C167 View Post
    a SELECT-query seems not to like the QSqlQuery::exec () method.
    If you pass a string to QSqlQuery constructor, it will execute it immediately. It should be:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE pictures SET data=? WHERE rowid=1;");
    3. query.addBindValue(fileData);
    4. query.exec();
    To copy to clipboard, switch view to plain text mode 

    And the select should be:
    Qt Code:
    1. QSqlQuery query( "SELECT data FROM pictures WHERE rowid=1;" );
    2. query.next(); // <- positions the query on the first row
    3. QSqlRecord record = query.record();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Storage and View -> imageViewer

    Quote Originally Posted by jacek View Post
    If you pass a string to QSqlQuery constructor, it will execute it immediately. It should be:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE pictures SET data=? WHERE rowid=1;");
    3. query.addBindValue(fileData);
    4. query.exec();
    To copy to clipboard, switch view to plain text mode 

    And the select should be:
    Qt Code:
    1. QSqlQuery query( "SELECT data FROM pictures WHERE rowid=1;" );
    2. query.next(); // <- positions the query on the first row
    3. QSqlRecord record = query.record();
    To copy to clipboard, switch view to plain text mode 
    Okay, thank you
    Quote Originally Posted by Thomas View Post
    One last issue.
    Make sure you delete stuff which you created with new.
    I thought Qt is handling such things for all the classes that inherit QObject, at least their own stuff? Okay, thx I'll add everything to my destructors

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Storage and View -> imageViewer

    Quote Originally Posted by C167 View Post
    I thought Qt is handling such things for all the classes that inherit QObject, at least their own stuff?
    Yes, it does, but only for objects that have a parent.

  6. #6
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Storage and View -> imageViewer

    okay, thx

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.