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. Replicate the contents of the current selected item in QTableView

Replicate the contents of the current selected item in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
model-viewviewsignal & slotqtableview
7 Posts 3 Posters 1.8k 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.
  • A Offline
    A Offline
    Arthur Araruna
    wrote on 9 Jul 2018, 05:17 last edited by
    #1

    Hello,

    I have a QLineEdit that should be used to echo the contents of the current selected item in a QTableView. I already managed to do it when I enter an item using the QItemSelectionModel::selectionChanged signal, but when I start editing it and commit the changes by pressing the return key, as the selection has not changed, the contents of the QLineEdit and the QTableView get out of sync...

    So, first of all, is there a way to make the contents of these two (getting only the data from the EditRole of the current item) be always in sync and that works in both situations? If not, how do I solve this desynchronization issue?

    I thought I could solve this by listening for the QAbstractItemDelegate::closeEditor signal, but I don't know if it is a good idea, because I would have to cast the editor parameter to the correct type in the slot implementation, and it would put extraneous logic to it.

    Thank you.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Jul 2018, 06:14 last edited by
      #2

      Hi,

      Sounds like QDataWidgetMapper could be of use to simplify all of that.

      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
      2
      • A Offline
        A Offline
        Arthur Araruna
        wrote on 11 Jul 2018, 12:42 last edited by
        #3

        Do you think attaching my QLineEdit to the current selected column and row, and updating it everytime the selection changes, is the way to go?

        I'm goingo to try it out! Thank you.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Arthur Araruna
          wrote on 11 Jul 2018, 14:56 last edited by
          #4

          Thank you, @SGaist. I was able to do what I wanted with the class you suggested. Yay!!

          I wonder if there was a simpler or more efficient solution, though.

          Anyway, it is working the way I commented previously:

          • I attached the QItemSelectionModel::currentChanged from my QTableView to a custom slot.
          • In this slot I remap the QLineEdit's section to the new column (if it changed) and select the new row as the current index using the QDataWidgetMapper)
          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 11 Jul 2018, 15:09 last edited by
            #5

            You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            A 1 Reply Last reply 12 Jul 2018, 01:05
            1
            • V VRonin
              11 Jul 2018, 15:09

              You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex

              A Offline
              A Offline
              Arthur Araruna
              wrote on 12 Jul 2018, 01:05 last edited by
              #6

              @VRonin said in Replicate the contents of the current selected item in QTableView:

              You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex

              Yes, but keep in mind that I'm using a QTableView and I want my QLineEdit to track the current cell. From what we can see in the case of that slot, the QDataWidgetMapper will only update either the column or the row, depending only on its orientation.

              So, as I have to respond to changes in both column and row indices, I chose to do it in the way I described so I only connect to one signal and not two.

              V 1 Reply Last reply 12 Jul 2018, 07:08
              0
              • A Arthur Araruna
                12 Jul 2018, 01:05

                @VRonin said in Replicate the contents of the current selected item in QTableView:

                You normally use it as in the example in this doc: http://doc.qt.io/qt-5/qdatawidgetmapper.html#setCurrentModelIndex

                Yes, but keep in mind that I'm using a QTableView and I want my QLineEdit to track the current cell. From what we can see in the case of that slot, the QDataWidgetMapper will only update either the column or the row, depending only on its orientation.

                So, as I have to respond to changes in both column and row indices, I chose to do it in the way I described so I only connect to one signal and not two.

                V Offline
                V Offline
                VRonin
                wrote on 12 Jul 2018, 07:08 last edited by
                #7

                @Arthur-Araruna said in Replicate the contents of the current selected item in QTableView:

                both column and row indices

                All you need to do is replace currentRowChanged with currentIndexChanged

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1

                3/7

                11 Jul 2018, 12:42

                • Login

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