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. Displaying an integer in tableView
Forum Updated to NodeBB v4.3 + New Features

Displaying an integer in tableView

Scheduled Pinned Locked Moved Solved General and Desktop
tableviewitem
4 Posts 3 Posters 2.2k Views 1 Watching
  • 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 29 Dec 2015, 15:20 last edited by
    #1

    Hi,
    I need to display an ID number (as an integer) in tableview. qDebug() displays the correct ID, but it doesn't show up in tableview.

    #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->data ().toInt ();
            smodel->setItem (0,0,Item);
    
            QByteArray ByteArray;
            ByteArray = query.value (1).toByteArray ();
         	QPixmap Pixmap;
    
    		Pixmap.loadFromData (ByteArray);
    
    		Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio);
    
            Item2->setData (QVariant(Pixmap),Qt::DecorationRole);
    
    		smodel->setItem (0,1,Item2);
            ui->tableView->verticalHeader ()->setDefaultSectionSize (100);
            ui->tableView->horizontalHeader ()->setDefaultSectionSize (100);
    
            ui->tableView->setModel (smodel);
    
    db.close ();
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    What should I do differently to see the ID (it is suppose to be 1).
    Thank you.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mjsurette
      wrote on 29 Dec 2015, 16:31 last edited by mjsurette
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 29 Dec 2015, 20:19 last edited by
        #3

        setData() takes a role parameter. You're not giving it so it uses the default Qt::UserRole + 1. This is not used by the table view. Same goes for data() when you retrieve it.

        You need to set the display role instead: Item->setData(ID = query.value (0).toInt (), Qt::DisplayRole);.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gabor53
          wrote on 30 Dec 2015, 04:19 last edited by
          #4

          Thank you. It worked.

          1 Reply Last reply
          0

          1/4

          29 Dec 2015, 15:20

          • Login

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