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. Reading a value from tableView
Forum Updated to NodeBB v4.3 + New Features

Reading a value from tableView

Scheduled Pinned Locked Moved Solved General and Desktop
tableviewqmodelindexindex
3 Posts 2 Posters 3.4k 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 have the following code to display data from a database to tableView:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
               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 * FROM Items ");
    
    
               if(query.isActive()==true)
                 {
                     qDebug() << "The query is active.";
                 }
                 else
                 {
                    qDebug() << "The query is NOT active.";
                 }
    
               QSqlQuery query2 ("SELECT Count(*) FROM Items");
    
     			int count;
               query2.first ();
                count = query2.value (0).toInt ();
    
    			qDebug() << "The number of rows: " << count;
    
    			//query.first ();
                QStandardItemModel *smodel = new QStandardItemModel;
    
                int ID;
    
    			ui->tableView->setModel (smodel);
    
                      	int row = 0;
    
                        while(query.next ())
                        {
    
                		QStandardItem *Item = new QStandardItem();
                        QStandardItem *Item2 = new QStandardItem();
    
                        Item->setData (ID = query.value (0).toInt (),Qt::DisplayRole);
                        qDebug() << "ID = " <<   ID;
                        smodel->setItem (row,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 (row++,1,Item2);
    			}
    
    
    
    
      					ui->tableView->resizeColumnsToContents ();
       					ui->tableView->resizeRowsToContents ();
              			ui->tableView->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
                        ui->tableView->verticalHeader ()->setVisible (false);
                        ui->tableView->setAlternatingRowColors (true);
                        ui->tableView->setStyleSheet ("alternate-background-color: cyan; background-color: white;");
                        ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers);
    
    
              smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID"));
              smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Picture"));
    
    
       		db.close ();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)
    {
       ui->tableView->setCurrentIndex (index);
       qDebug() << "Index: " << index;
       int r = index.row();
       int c = index.column ();
       qDebug() << "Row: " << r;
       qDebug() << "Column: " << c;
    }
    

    After double clicking on a row, I want to read the value from column 0 (ID). I managed to get the row and column numbers but not the value. Please help me to read the value.
    Thank you.

    RatzzR 1 Reply Last reply
    0
    • G gabor53

      Hi,
      I have the following code to display data from a database to tableView:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
                 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 * FROM Items ");
      
      
                 if(query.isActive()==true)
                   {
                       qDebug() << "The query is active.";
                   }
                   else
                   {
                      qDebug() << "The query is NOT active.";
                   }
      
                 QSqlQuery query2 ("SELECT Count(*) FROM Items");
      
       			int count;
                 query2.first ();
                  count = query2.value (0).toInt ();
      
      			qDebug() << "The number of rows: " << count;
      
      			//query.first ();
                  QStandardItemModel *smodel = new QStandardItemModel;
      
                  int ID;
      
      			ui->tableView->setModel (smodel);
      
                        	int row = 0;
      
                          while(query.next ())
                          {
      
                  		QStandardItem *Item = new QStandardItem();
                          QStandardItem *Item2 = new QStandardItem();
      
                          Item->setData (ID = query.value (0).toInt (),Qt::DisplayRole);
                          qDebug() << "ID = " <<   ID;
                          smodel->setItem (row,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 (row++,1,Item2);
      			}
      
      
      
      
        					ui->tableView->resizeColumnsToContents ();
         					ui->tableView->resizeRowsToContents ();
                			ui->tableView->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
                          ui->tableView->verticalHeader ()->setVisible (false);
                          ui->tableView->setAlternatingRowColors (true);
                          ui->tableView->setStyleSheet ("alternate-background-color: cyan; background-color: white;");
                          ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers);
      
      
                smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID"));
                smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Picture"));
      
      
         		db.close ();
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)
      {
         ui->tableView->setCurrentIndex (index);
         qDebug() << "Index: " << index;
         int r = index.row();
         int c = index.column ();
         qDebug() << "Row: " << r;
         qDebug() << "Column: " << c;
      }
      

      After double clicking on a row, I want to read the value from column 0 (ID). I managed to get the row and column numbers but not the value. Please help me to read the value.
      Thank you.

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by Ratzz
      #2

      @gabor53
      Something like this

      QModelIndex index = model->index(row, col, QModelIndex());
      ui->tableView->model()->data(index).toString();
      

      Or

      ui->tableView->model()->data(ui->tableView->model()->index(row,col)).toString()

      --Alles ist gut.

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

        Thank you. It worked!

        1 Reply Last reply
        0

        • Login

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