Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Query dispays memory address instead of variable value

Query dispays memory address instead of variable value

Scheduled Pinned Locked Moved Unsolved General and Desktop
queryqstandarditemmodel
4 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Do something like this.

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved