Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView clicked() versus doubleClicked()
Qt 6.12 LTS is out! See what's new in the release blog

QTableView clicked() versus doubleClicked()

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 5 Posters 8.7k Views 3 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.
  • PerdrixP Perdrix

    When I click on the row (single selection) the code loads an image file whose details are held in that rows backing data to display it:

    When I double-click on a cell I want to edit the cell not open the file.

    c0a198fa-1472-485e-afa4-4cb036440cf9-image.png

    Does that clarify

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @Perdrix For double-click there is https://doc.qt.io/qt-6/qabstractitemview.html#doubleClicked

    https://forum.qt.io/topic/113070/qt-code-of-conduct

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

      I would rather use the currentChanged signal of the selection model to handle the data loading.

      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
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #6

        I will try that.

        1 Reply Last reply
        0
        • PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by
          #7

          I added an entry to public slots in addition to the customContextMenuRequested one:

          public slots:
          		void on_tableView_customContextMenuRequested(const QPoint& pos);
          		void on_tableView_currentChanged(const QModelIndex& index, const QModelIndex& last);
          

          and added an implementation for it

          But when I run the code I get:

          QMetaObject::connectSlotsByName: No matching signal for on_tableView_currentChanged(QModelIndex,QModelIndex)
          

          I don't think I got the signature wrong as the docs for QItemSelectionModel::currentChanged signal says:

          void currentChanged(const QModelIndex &current, const QModelIndex &previous)
          

          However connectSlotsByName() seems to disagree!

          Puzzled
          David

          JonBJ 1 Reply Last reply
          0
          • PerdrixP Perdrix

            I added an entry to public slots in addition to the customContextMenuRequested one:

            public slots:
            		void on_tableView_customContextMenuRequested(const QPoint& pos);
            		void on_tableView_currentChanged(const QModelIndex& index, const QModelIndex& last);
            

            and added an implementation for it

            But when I run the code I get:

            QMetaObject::connectSlotsByName: No matching signal for on_tableView_currentChanged(QModelIndex,QModelIndex)
            

            I don't think I got the signature wrong as the docs for QItemSelectionModel::currentChanged signal says:

            void currentChanged(const QModelIndex &current, const QModelIndex &previous)
            

            However connectSlotsByName() seems to disagree!

            Puzzled
            David

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #8

            @Perdrix
            void QTableView::currentChanged(const QModelIndex &current, const QModelIndex &previous) is [override virtual protected], it is not a signal!

            @SGaist referred you to void QItemSelectionModel::currentChanged(const QModelIndex &current, const QModelIndex &previous), which is a signal, but on the QItemSelectionModel, not the QTableView. Hence void on_tableView_currentChanged(...) is not going to work.

            BTW, most (advanced) people here do not use connectSlotsByName(), it has too many potential issues. Consider changing over to explicit connect() calls (using new-style syntax), then you would not have been stymied!

            PerdrixP 1 Reply Last reply
            2
            • JonBJ JonB

              @Perdrix
              void QTableView::currentChanged(const QModelIndex &current, const QModelIndex &previous) is [override virtual protected], it is not a signal!

              @SGaist referred you to void QItemSelectionModel::currentChanged(const QModelIndex &current, const QModelIndex &previous), which is a signal, but on the QItemSelectionModel, not the QTableView. Hence void on_tableView_currentChanged(...) is not going to work.

              BTW, most (advanced) people here do not use connectSlotsByName(), it has too many potential issues. Consider changing over to explicit connect() calls (using new-style syntax), then you would not have been stymied!

              PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by Perdrix
              #9

              @JonB Thanks yet again!

              I'm now doing this:

              QItemSelectionModel* qsm = ui->tableView->selectionModel();
              connect(qsm, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
              			this, SLOT(tableView_currentChanged(const QModelIndex&, const QModelIndex&)));
              
              

              and that works fine, but there's a small puzzle.

              When I click on a table row for the FIRST time, the slot is invoked just fine, and it executes this code:

              qDebug() << "Table View selected item changed to row " << index.row();
              
              QItemSelectionModel * qsm = ui->tableView->selectionModel();
              		QModelIndexList selectedRows = qsm->selectedRows();
              qDebug() << "Number of selected rows is " << selectedRows.count();
              

              which produces the somewhat unexpected output:

              Table View selected item changed to row  3
              Number of selected rows is  0
              

              Yet the row in question is highlighted to tell me it's selected!!!

              Please can you explain for me?

              And it gets wierder when I click on another row, the index of the selected item I get is the one that WAS selected!????

              Should I instead of the currentChanged signal be using the selectionChanged signal???

              Thank you
              David

              JonBJ 1 Reply Last reply
              0
              • PerdrixP Perdrix

                @JonB Thanks yet again!

                I'm now doing this:

                QItemSelectionModel* qsm = ui->tableView->selectionModel();
                connect(qsm, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
                			this, SLOT(tableView_currentChanged(const QModelIndex&, const QModelIndex&)));
                
                

                and that works fine, but there's a small puzzle.

                When I click on a table row for the FIRST time, the slot is invoked just fine, and it executes this code:

                qDebug() << "Table View selected item changed to row " << index.row();
                
                QItemSelectionModel * qsm = ui->tableView->selectionModel();
                		QModelIndexList selectedRows = qsm->selectedRows();
                qDebug() << "Number of selected rows is " << selectedRows.count();
                

                which produces the somewhat unexpected output:

                Table View selected item changed to row  3
                Number of selected rows is  0
                

                Yet the row in question is highlighted to tell me it's selected!!!

                Please can you explain for me?

                And it gets wierder when I click on another row, the index of the selected item I get is the one that WAS selected!????

                Should I instead of the currentChanged signal be using the selectionChanged signal???

                Thank you
                David

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #10

                @Perdrix said in QTableView clicked() versus doubleClicked():

                connect(qsm, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
                this, SLOT(tableView_currentChanged(const QModelIndex&, const QModelIndex&)));

                Do yourself a one-time nice favor: change over to connect() "new" style syntax (yours is now really old) for ever, and don't look back :)

                You are not the first person to struggle with the difference between "current" and "selected", myself included! @SGaist may know better, as he made the suggestion. If you find selectionChanged() a better/more intelligible signal, I would try that :)

                1 Reply Last reply
                1
                • PerdrixP Offline
                  PerdrixP Offline
                  Perdrix
                  wrote on last edited by
                  #11

                  To answer my own question! This seems to work:

                  	void StackingDlg::tableView_selectionChanged([[maybe_unused]] const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected)
                  	{
                  
                  		QItemSelectionModel * qsm = ui->tableView->selectionModel();
                  		QModelIndexList selectedRows = qsm->selectedRows();
                  		qDebug() << "Number of selected rows: " << selectedRows.count();
                  		//
                  		// If only one row is selected, we want to know the filename
                  		//
                  		if (1 == selectedRows.count())
                  		{
                  			QModelIndex& ndx = selectedRows[0];
                  			qDebug() << "  Selected row: " << ndx.row();
                  

                  though there's one remaining oddity.

                  When I click on the single row, the row is highlighted to show it's selected, and the image loading is started in another thread, and the table view is disabled.

                  On completion of the loading, the table view is enabled again, but the highlight is lost until I click on somewhere else in the window (not the rows of the tableview).

                  I changed that code to NOT disable the table view and the highlight did not go away.

                  Is there a way to prevent USER interaction with a control but allow internal events to continue processing?

                  M JonBJ 2 Replies Last reply
                  0
                  • PerdrixP Perdrix

                    To answer my own question! This seems to work:

                    	void StackingDlg::tableView_selectionChanged([[maybe_unused]] const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected)
                    	{
                    
                    		QItemSelectionModel * qsm = ui->tableView->selectionModel();
                    		QModelIndexList selectedRows = qsm->selectedRows();
                    		qDebug() << "Number of selected rows: " << selectedRows.count();
                    		//
                    		// If only one row is selected, we want to know the filename
                    		//
                    		if (1 == selectedRows.count())
                    		{
                    			QModelIndex& ndx = selectedRows[0];
                    			qDebug() << "  Selected row: " << ndx.row();
                    

                    though there's one remaining oddity.

                    When I click on the single row, the row is highlighted to show it's selected, and the image loading is started in another thread, and the table view is disabled.

                    On completion of the loading, the table view is enabled again, but the highlight is lost until I click on somewhere else in the window (not the rows of the tableview).

                    I changed that code to NOT disable the table view and the highlight did not go away.

                    Is there a way to prevent USER interaction with a control but allow internal events to continue processing?

                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #12

                    @Perdrix said in QTableView clicked() versus doubleClicked():

                    the table view is enabled again, but the highlight is lost

                    Because the table lost the focus, set it again with:

                    tableView->setFocus(Qt::OtherFocusReason);
                    
                    PerdrixP 1 Reply Last reply
                    1
                    • PerdrixP Perdrix

                      To answer my own question! This seems to work:

                      	void StackingDlg::tableView_selectionChanged([[maybe_unused]] const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected)
                      	{
                      
                      		QItemSelectionModel * qsm = ui->tableView->selectionModel();
                      		QModelIndexList selectedRows = qsm->selectedRows();
                      		qDebug() << "Number of selected rows: " << selectedRows.count();
                      		//
                      		// If only one row is selected, we want to know the filename
                      		//
                      		if (1 == selectedRows.count())
                      		{
                      			QModelIndex& ndx = selectedRows[0];
                      			qDebug() << "  Selected row: " << ndx.row();
                      

                      though there's one remaining oddity.

                      When I click on the single row, the row is highlighted to show it's selected, and the image loading is started in another thread, and the table view is disabled.

                      On completion of the loading, the table view is enabled again, but the highlight is lost until I click on somewhere else in the window (not the rows of the tableview).

                      I changed that code to NOT disable the table view and the highlight did not go away.

                      Is there a way to prevent USER interaction with a control but allow internal events to continue processing?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #13

                      @Perdrix said in QTableView clicked() versus doubleClicked():

                      and the image loading is started in another thread, and the table view is disabled.

                      Just asking: do you really need a separate thread for this "image loading" and having this "disabling" and consequent "focus losing"? Maybe you do, but have you tried without bothering?

                      PerdrixP 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Perdrix said in QTableView clicked() versus doubleClicked():

                        and the image loading is started in another thread, and the table view is disabled.

                        Just asking: do you really need a separate thread for this "image loading" and having this "disabling" and consequent "focus losing"? Maybe you do, but have you tried without bothering?

                        PerdrixP Offline
                        PerdrixP Offline
                        Perdrix
                        wrote on last edited by
                        #14

                        @JonB Separate thread? Yes - it can take quite a while for some of these images.

                        JonBJ 1 Reply Last reply
                        0
                        • M mpergand

                          @Perdrix said in QTableView clicked() versus doubleClicked():

                          the table view is enabled again, but the highlight is lost

                          Because the table lost the focus, set it again with:

                          tableView->setFocus(Qt::OtherFocusReason);
                          
                          PerdrixP Offline
                          PerdrixP Offline
                          Perdrix
                          wrote on last edited by
                          #15

                          @mpergand said in QTableView clicked() versus doubleClicked():

                          tableView->setFocus(Qt::OtherFocusReason);

                          Woohoo! That worked a treat.

                          Thanks
                          David

                          1 Reply Last reply
                          0
                          • PerdrixP Perdrix

                            @JonB Separate thread? Yes - it can take quite a while for some of these images.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #16

                            @Perdrix said in QTableView clicked() versus doubleClicked():

                            it can take quite a while for some of these images.

                            Just because I am interested: do you mean (it takes much time) to load the image (once generated if that is what you are doing) into the UI, or do you mean to generate an image to load?

                            1 Reply Last reply
                            0
                            • PerdrixP Offline
                              PerdrixP Offline
                              Perdrix
                              wrote on last edited by
                              #17

                              Large images that take a while to load from file.

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

                                Do you want to show the full image size ?
                                Otherwise creating a cache with small versions of them might be a better solution in terms of speed and only load the real version when really needed.

                                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
                                • PerdrixP Offline
                                  PerdrixP Offline
                                  Perdrix
                                  wrote on last edited by
                                  #19

                                  Still need to load them - I am caching once loaded.

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

                                    Hence my suggestion, have a "pre-processing" phase where you create thumbnails to show quickly and load the complete file in the background. Once it's done notify your application and do what you need with it.

                                    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
                                    • PerdrixP Offline
                                      PerdrixP Offline
                                      Perdrix
                                      wrote on last edited by Perdrix
                                      #21

                                      No I can't do thumbnails as I don't know a-priori what files are involved and there might be 100s of them.

                                      Now back to the problem - selectionChanged works fine to detect the newly selected row to trigger image loading.

                                      Unfortunately, double clicking on a editable cell still results in selectionChanged driving the image loading to start.

                                      What it doesn't do is open the editor on the cell that was double clicked!

                                      I had a quick look at the code of mouseDoubleClickEvent in QAbstractItemView.cpp, and I think it is written so that it should always open the editor. I suspect that the way mousePressedEvent() is written means that the double click event isn't generated if the selection change is driven from there (I don't think it should be driven from there - rather it should be driven from a mouseClickedEvent()) which isn't present. I do see code in mousPressedEvent() that is intended to open the editor, but don't quite "get" why that wasn't invoked.

                                      David

                                      M 1 Reply Last reply
                                      0
                                      • PerdrixP Perdrix

                                        No I can't do thumbnails as I don't know a-priori what files are involved and there might be 100s of them.

                                        Now back to the problem - selectionChanged works fine to detect the newly selected row to trigger image loading.

                                        Unfortunately, double clicking on a editable cell still results in selectionChanged driving the image loading to start.

                                        What it doesn't do is open the editor on the cell that was double clicked!

                                        I had a quick look at the code of mouseDoubleClickEvent in QAbstractItemView.cpp, and I think it is written so that it should always open the editor. I suspect that the way mousePressedEvent() is written means that the double click event isn't generated if the selection change is driven from there (I don't think it should be driven from there - rather it should be driven from a mouseClickedEvent()) which isn't present. I do see code in mousPressedEvent() that is intended to open the editor, but don't quite "get" why that wasn't invoked.

                                        David

                                        M Offline
                                        M Offline
                                        mpergand
                                        wrote on last edited by
                                        #22

                                        @Perdrix said in QTableView clicked() versus doubleClicked():

                                        What it doesn't do is open the editor on the cell that was double clicked!

                                        Check editTriggers is set to QAbstractItemView::DoubleClicked

                                        1 Reply Last reply
                                        0
                                        • PerdrixP Offline
                                          PerdrixP Offline
                                          Perdrix
                                          wrote on last edited by
                                          #23

                                          It must be set because I can d-click the cell later (after the image has loaded) and it opens the editor just fine

                                          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
                                          • Unsolved