Query dispays memory address instead of variable value
Unsolved
General and Desktop
-
Hi
I use the following code to display data and an image from a db:#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QSqlDatabase db; db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("C:/Programming/Qtsamples/Image_from_db_to_Table/db.db"); if(!db.open ()) { qDebug() << "The database is NOT open!"; } else { qDebug() << "The database is open!"; } QSqlQuery query("SELECT ID,Pic FROM Items"); if(query.isActive()==true) { qDebug() << "The query is active."; } else { qDebug() << "The query is NOT active."; } query.first (); int ID; QStandardItemModel *smodel = new QStandardItemModel; QStandardItem *Item = new QStandardItem(); QStandardItem *Item2 = new QStandardItem(); Item->setData (ID = query.value (0).toInt ()); qDebug() << "ID = " << Item; smodel->setItem (0,0,Item); QByteArray ByteArray; ByteArray = query.value (1).toByteArray (); QPixmap Pixmap = QPixmap(); Pixmap.loadFromData (ByteArray); Item2->setData (QVariant(Pixmap),Qt::DecorationRole); smodel->setItem (0,1,Item2); ui->tableView->setModel (smodel); db.close (); } MainWindow::~MainWindow() { delete ui; }
When I run it I get the following messages in Application Output:
Starting C:\Programming\Qtsamples\build-Image_from_db_to_Table-Desktop_Qt_5_5_0_Static_MinGW_32_bit-Release\release\Image_from_db_to_Table.exe...
The database is open!
The query is active.
ID = 0x29fd30
C:\Programming\Qtsamples\build-Image_from_db_to_Table-Desktop_Qt_5_5_0_Static_MinGW_32_bit-Release\release\Image_from_db_to_Table.exe exited with code 0.What am I doing incorrectly? The ID should be 1 and I assume the memory address is displayed.
I appreciate any help. Thank you. -
Do something like this.
qDebug() << "ID = " << Item->data().toInt();
-
You're setting the wrong role. See my answer here.