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. How to catch "change selected row" signal in a QTableView?
Forum Updated to NodeBB v4.3 + New Features

How to catch "change selected row" signal in a QTableView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
table viewsignals
10 Posts 4 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.
  • J Offline
    J Offline
    jdent
    wrote on 8 Jun 2024, 00:07 last edited by
    #1

    In QTableWidget one can connect

     &QTableWidget::cellChanged 
    

    to a slot you provide
    How is this done for QTableView?

    P S 2 Replies Last reply 8 Jun 2024, 02:56
    0
    • J jdent
      8 Jun 2024, 00:07

      In QTableWidget one can connect

       &QTableWidget::cellChanged 
      

      to a slot you provide
      How is this done for QTableView?

      P Offline
      P Offline
      Pl45m4
      wrote on 8 Jun 2024, 02:56 last edited by Pl45m4 6 Aug 2024, 02:57
      #2

      @jdent said in How to catch "change selected row" signal in a QTableView?:

      How is this done for QTableView?

      There is no such function, but you can implement a signal like this on your own.

      Looking at the QTableWidget source where cellChanged is emitted:

      void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
      {
          Q_Q(QTableWidget);
          if (QTableWidgetItem *item = tableModel()->item(index))
              emit q->itemChanged(item);
          emit q->cellChanged(index.row(), index.column());
      }
      
      • https://codebrowser.dev/qt6/qtbase/src/widgets/itemviews/qtablewidget.cpp.html#_ZN19QTableWidgetPrivate18_q_emitItemChangedERK11QModelIndex

      void QTableWidgetPrivate::_q_emitItemChanged emits cellChanged and is a private (hidden) function used in QTableWidget to make it work as we all know. But you can add this behaviour to your own QTableView subclass.

      void QTableWidgetPrivate::setup()
      {
          Q_Q(QTableWidget);
          // model signals
          QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                               q, SLOT(_q_emitItemChanged(QModelIndex)));
      }
      

      You see here, that this function from above is connected to the dataChanged(index, index) signal of the model during the initialization of the QTableWidget and its private logic.

      model is a QAbstractItemModel* internally.
      You can use your model, of course.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • J jdent
        8 Jun 2024, 00:07

        In QTableWidget one can connect

         &QTableWidget::cellChanged 
        

        to a slot you provide
        How is this done for QTableView?

        S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 8 Jun 2024, 18:49 last edited by
        #3

        @jdent hi,

        Something is not clear between your title and post content. The title mentions selection but the post is about cellChanged which are about two different things.

        If you are thinking about selection, then take a look at QItemSelectionModel.

        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
        • J Offline
          J Offline
          jdent
          wrote on 8 Jun 2024, 22:43 last edited by
          #4

          to be clear I am going to copy my code:

          VectorModel* model = new VectorModel{};
          QSortFilterProxyModel* proxy = new QSortFilterProxyModel{ this };
          proxy->setSourceModel(model);
          ui.tableView->setModel(proxy);
          ui.tableView->setSortingEnabled(true);
          
          
          QDataWidgetMapper* mapper = new QDataWidgetMapper{};
          mapper->setModel(proxy);
          mapper->addMapping(ui.lineEdit, 0);
          mapper->addMapping(ui.lineEdit_2, 1);
          mapper->toFirst();
          
          QItemSelectionModel* selection_model = new QItemSelectionModel{ proxy };
          
          
          connect(selection_model, &QItemSelectionModel::currentRowChanged , this, &QtWidgetsApplication1::currentRowChanged);
          

          But changing the selection in the QTableView does not trigger the currentRowChanged signal!!

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 9 Jun 2024, 06:35 last edited by SGaist 6 Oct 2024, 06:35
            #5

            What about using the selection model from the table view directly ?

            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
            • J Offline
              J Offline
              jdent
              wrote on 9 Jun 2024, 22:45 last edited by jdent 6 Sept 2024, 23:13
              #6

              How can I use the selectionModel of my VectorModel? My model inherits from QAbstractTableModel but I don't see any selectionModel in QAbstractTableModel functions/properties...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 10 Jun 2024, 06:35 last edited by SGaist 6 Oct 2024, 06:36
                #7

                Sorry, I miswrote my answer (fixed it by the way). The selection model is returned by the view you are using. See selectionModel.

                I meant to say, use that one directly.

                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
                1
                • J Offline
                  J Offline
                  JonB
                  wrote on 10 Jun 2024, 08:55 last edited by JonB 6 Oct 2024, 08:56
                  #8

                  @jdent
                  Your question and @SGaist's answer happen to relate to the reply I have just posted at https://forum.qt.io/topic/157167/what-should-i-do-to-use-treeview-childs-to-select-a-subset-of-data-to-show-in-a-tableview/2. As I wrote there, see e.g. See e.g. https://stackoverflow.com/questions/2062889/qtableview-what-signal-is-sent-when-user-selects-a-row-by-clicking-to-it for use of selectionModel(). Selection is handled by the view.

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    jdent
                    wrote on 11 Jun 2024, 21:02 last edited by
                    #9

                    I have access to selection Model as @SGaist recommended and I wrote:

                        QItemSelectionModel* selection_model = ui.tableView->selectionModel();
                        connect(selection_model, &QItemSelectionModel::currentRowChanged , this, &QtWidgetsApplication1::currentRowChanged);
                    

                    But I have a mapper like this:

                        mapper = new QDataWidgetMapper{};
                        mapper->setModel(proxy);
                        mapper->addMapping(ui.lineEdit, 0);
                        mapper->addMapping(ui.lineEdit_2, 1);
                        mapper->toFirst();
                    

                    and I need the mapper to "move to the new selected row", something like mapper->to(n) where n is the new current selection
                    so that the form widgets (e.g. ui.lineEdit) are kept in sync with the table view selection but how?

                    J 1 Reply Last reply 11 Jun 2024, 21:31
                    0
                    • J jdent
                      11 Jun 2024, 21:02

                      I have access to selection Model as @SGaist recommended and I wrote:

                          QItemSelectionModel* selection_model = ui.tableView->selectionModel();
                          connect(selection_model, &QItemSelectionModel::currentRowChanged , this, &QtWidgetsApplication1::currentRowChanged);
                      

                      But I have a mapper like this:

                          mapper = new QDataWidgetMapper{};
                          mapper->setModel(proxy);
                          mapper->addMapping(ui.lineEdit, 0);
                          mapper->addMapping(ui.lineEdit_2, 1);
                          mapper->toFirst();
                      

                      and I need the mapper to "move to the new selected row", something like mapper->to(n) where n is the new current selection
                      so that the form widgets (e.g. ui.lineEdit) are kept in sync with the table view selection but how?

                      J Offline
                      J Offline
                      JonB
                      wrote on 11 Jun 2024, 21:31 last edited by
                      #10

                      @jdent
                      Are you asking about void QDataWidgetMapper::setCurrentModelIndex(const QModelIndex &index)

                      Sets the current index to the row of the index if the orientation is horizontal (the default), otherwise to the column of the index.
                      This convenience slot can be connected to the signal currentRowChanged()

                      connect(myTableView->selectionModel(), &QItemSelectionModel::currentRowChanged,
                              mapper, &QDataWidgetMapper::setCurrentModelIndex);
                      
                      1 Reply Last reply
                      0

                      4/10

                      8 Jun 2024, 22:43

                      • Login

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