Qt World Summit: Submit your Presentation

Query dispays memory address instead of variable value

  • 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) :
        ui(new Ui::MainWindow)
        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!";
                qDebug() << "The database is open!";
            QSqlQuery query("SELECT ID,Pic FROM Items");
                  qDebug() << "The query is active.";
                 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 ();
        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.

  • Qt Champions 2017

    Do something like this.

    qDebug() << "ID = " << Item->data().toInt();

  • Thank you. That worked.
    I've noticed that I get no error message, but it displays nothing in the ID column. What did I do wrong there/
    Thank you.

  • Moderators

    You're setting the wrong role. See my answer here.