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. QTableView Question
Qt 6.11 is out! See what's new in the release blog

QTableView Question

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.7k 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
    Gundi
    wrote on last edited by
    #1

    I have a tableview and am loading the model based from data from a database. That is why I used the view instead of a tablewidget. The challenge is I can't seem to be able to get a particular cell value from the view. What I am trying to accomplish with the View is essentially this code in the TableWidget. The user selects a cell and I want to grab the string data. Seems simple but for some reason I can't seem to figure it out.

    Here is the equivalent code for the TableWidget I am trying to accomplish.
    @
    QList<QTableWidgetItem*> selectedRow = ui->tblRemoveTableLogList->selectedItems();
    if(!selectedRow.isEmpty())
    {
    QString cellValue = selectedRow.at(0)->text();
    return cellValue.toInt();
    }
    @

    Any help is appreciated.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gundi
      wrote on last edited by
      #2

      The Challenge is setModel() in QTableWidget is private and I need to set the model from the Database. Any ideas?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You can use the QSelectionModel::selectedIndexes and from there get the text.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gundi
          wrote on last edited by
          #4

          I was trying that but could not seem to get the data out of the QModelIndexList. Where is the actual data stored in this class? Or how do I access it? I believe this list is a 1-N rows of data selected, but can't seem to get to a specific cell data.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            ssenegas
            wrote on last edited by
            #5

            Hi,

            When the selection model is changed, you can be notified through the signal QItemSelectionModel::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )

            Then, you access selected indexes with QItemSelection::indexes()

            @QModelIndexList indexes = theItemSelection.indexes();
            foreach (QModelIndex index, indexes)
            {
            QString selectedItemAsString = index.data().toString();
            }@

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gundi
              wrote on last edited by
              #6

              Thanks Seb. I must be missing something because there is no signal selectionChanged in QTableView

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gaijinpunch
                wrote on last edited by
                #7

                selectionChanged() is inherited from QAbstractItemView

                http://qt-project.org/doc/qt-5/qabstractitemview.html#selectionChanged

                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