QTableView clicked() versus doubleClicked()
-
I capture the TableView clicked(signal) in my code which works fine if I want the user to select a single row.
Unfortunately I also get this signal if the Ctrl or Shift keys were pressed that time the table was clicked on, and also when the user has double clicked a cell in the table view to edit a cell.
Further experimentation shows that I actually seem to require to triple click to get the item editor to open!!!
What do I need to do so I only get the clicked signal for a single click without modifiers? I still want the Ctrl-Click and Shift-Click actions to select ...
And so the a double click will work to open the item editor!
Thanks
David -
Hi,
What exactly are you trying to implement ?
-
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.

Does that clarify
@Perdrix For double-click there is https://doc.qt.io/qt-6/qabstractitemview.html#doubleClicked
-
I would rather use the currentChanged signal of the selection model to handle the data loading.
-
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 ¤t, const QModelIndex &previous)However connectSlotsByName() seems to disagree!
Puzzled
David -
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 ¤t, const QModelIndex &previous)However connectSlotsByName() seems to disagree!
Puzzled
David@Perdrix
void QTableView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) is[override virtual protected], it is not a signal!@SGaist referred you to void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous), which is a signal, but on the
QItemSelectionModel, not theQTableView. Hencevoid 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 explicitconnect()calls (using new-style syntax), then you would not have been stymied! -
@Perdrix
void QTableView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) is[override virtual protected], it is not a signal!@SGaist referred you to void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous), which is a signal, but on the
QItemSelectionModel, not theQTableView. Hencevoid 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 explicitconnect()calls (using new-style syntax), then you would not have been stymied!@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 0Yet 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 -
@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 0Yet 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@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 :) -
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?
-
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?
-
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?
@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?
-
@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?
-
@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); -
@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?
-
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. -
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.