Results 1 to 3 of 3

Thread: Image is not show that load from sqlite database

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Image is not show that load from sqlite database

    Hi!

    Photo are stored in a SQLite Database (from other application). Now I load data from database and want to load image in my label.
    When I debug it, QByteArray is ok. But QPixmap is invalid. How can I fix it?

    Thanks


    .h file
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QSqlDatabase>
    6. #include <QSqlQuery>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. QSqlQuery *query;
    21.  
    22. private slots:
    23. void on_pushButton_clicked();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDebug>
    4. #include <QPixmap>
    5. #include <QSqlError>
    6. #include <QBuffer>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::on_pushButton_clicked()
    21. {
    22. myDb = QSqlDatabase::addDatabase("QSQLITE","myconnection");
    23. myDb.setDatabaseName("D:/ArtistPhoto.db3");
    24.  
    25. if (!myDb.open()) {
    26. qDebug() << " not open";
    27. }
    28. else {
    29. qDebug() << " opened";
    30. query = new QSqlQuery ("SELECT Data FROM Artist WHERE NOT data IS NULL",myDb);
    31. if(query->first()) {
    32. QByteArray outByteArray = query->value("Data").toByteArray();
    33. QPixmap outPixmap = QPixmap();
    34. outPixmap.loadFromData( outByteArray );
    35. if(outPixmap.loadFromData(outByteArray,"PNG")) ui->label->setPixmap(outPixmap);
    36.  
    37. }
    38. myDb.close();
    39.  
    40.  
    41. }
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 



    Project source code and database file
    http://bit.ly/2F1FNmd

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Image is not show that load from sqlite database

    When I debug it, QByteArray is ok. But QPixmap is invalid.
    What do you mean by 'ok'?
    What does your QByteArray return for isNull() or isEmpty()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Image is not show that load from sqlite database

    ok means

    qDebug() << outByteArray.isEmpty() << outByteArray.isNull();

    false false

    Untitled.jpgUntitled.jpg

Similar Threads

  1. Replies: 2
    Last Post: 18th May 2014, 18:03
  2. Replies: 3
    Last Post: 26th February 2014, 05:56
  3. QML GridView load model from C++ SQLite
    By olegdim in forum Qt Quick
    Replies: 0
    Last Post: 21st August 2012, 20:01
  4. Load a new Sqlite database problem
    By beware in forum Qt Programming
    Replies: 15
    Last Post: 24th January 2011, 10:05
  5. Cannot load SQLITE driver on clean target
    By ForestDweller in forum Installation and Deployment
    Replies: 1
    Last Post: 15th December 2008, 01:49

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.