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 value from a cell in a tableView created by QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

Reading value from a cell in a tableView created by QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.7k Views 2 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 last edited by
    #1

    Hi,
    I have a table created by QStandardItemModel. on_tableview_clocked tells me the item the user clicked. I need to read the the first column (column 0) ID. Here is the code I currently have:

    void MainWindow::on_tableView_clicked(const QModelIndex &index)
    {
        int row = index.row ();
        int column = index.column ();
        qDebug() << "Index: " << "(" << row <<"," << column << ")";
    
        if(column == 2)
            {
                qDebug() << "Column 2 was chosen.";
    
                QPixmap imageToEnlarge = index.data(Qt::DecorationRole).value<QPixmap>();
    
                int w = ui->label_Temp->width();
                int h = ui->label_Temp->height ();
                ui->label_Temp->setPixmap (imageToEnlarge.scaled(w,h,Qt::KeepAspectRatio));
                ImageDisplay *mImageDisplay = new ImageDisplay;
                mImageDisplay->viewImage(imageToEnlarge);
                mImageDisplay->exec ();
    
            }
    }
    

    How can I get the value from column 0 knowing the row number from

      int row = index.row ();
    

    Thank you.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      @gabor53
      Create a QModelIndex using existing index that you get in onClicked slot. This index will give you row whereas you have the column. Once you get the new index use data with your required role perhaps Qt::DisplayRole unless you have your user defined roles. So to sum up:

      QModelIndex newindex(index.model()->index(index.row(), 0, index.parent())); //0th column 
      qDebug() << newindex.data(Qt::DisplayRole);
      

      157

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

        Thank you. It worked perfectly.

        1 Reply Last reply
        0
        • p3c0P p3c0

          @gabor53
          Create a QModelIndex using existing index that you get in onClicked slot. This index will give you row whereas you have the column. Once you get the new index use data with your required role perhaps Qt::DisplayRole unless you have your user defined roles. So to sum up:

          QModelIndex newindex(index.model()->index(index.row(), 0, index.parent())); //0th column 
          qDebug() << newindex.data(Qt::DisplayRole);
          
          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          @p3c0
          How can I assign the value displayed by qDebug to a QString or int?
          Thank you.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @gabor53 The data function returns a QVariant. You need the convert the returned value into your required type. QVariant has several built-in methods to do so. Have a look at those starting with to*.
            http://doc.qt.io/qt-5/qvariant.html#toString
            http://doc.qt.io/qt-5/qvariant.html#toInt

            157

            G 1 Reply Last reply
            2
            • p3c0P p3c0

              @gabor53 The data function returns a QVariant. You need the convert the returned value into your required type. QVariant has several built-in methods to do so. Have a look at those starting with to*.
              http://doc.qt.io/qt-5/qvariant.html#toString
              http://doc.qt.io/qt-5/qvariant.html#toInt

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              @p3c0
              I came up with the following and it works great:

                          QModelIndex newindex(index.model()->index(index.row(), 0, index.parent()));
                          qDebug() << newindex.data(Qt::DisplayRole);
                          QVariant v(newindex.data(Qt::DisplayRole));
                          FriendID = v.toString ();
                          qDebug () << "Friend ID: " << FriendID;
              

              Thank you for your help.

              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