How to detect two click events
-
@JonB
As shown below. I selected the row. Now if I pressOpen
button should open a new window with title as first column content (for ex here "PID02").
-
@russjohn834
Ah. So you don't want to be writing any code in anything likeon_tableWidget_cellClicked(int row, int column)
, that would be to do with clicking in the cell. You just want a slot for (each of) your pushbuttons'.clicked
signal. In that you can look at what row(s) is/are selected in the table widget (you can make it only able to select a single row, not multiple rows nor individual cells, if you haven't already). Have a read through e.g. https://stackoverflow.com/questions/5927499/how-to-get-selected-rows-in-qtableview, something likeQModelIndexList selection = yourTableView->selectionModel()->selectedRows();
So here you do not bother to place a slot on the click to make the selection in the table widget, you let it handle that and you just use its
selectedRows()
after the event to see which items are currently selected. -
@JonB Thank you.
I did this:
void MainWindow::on_pushButton_4_clicked() { QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0); qDebug()<<"\n The content is"<<selection; -------> How to get content }
I'm getting output as:
The content is (QModelIndex(1,0,0x0,QTableModel(0x37e460)))
How do I extract the content of that particular cell?
-
@russjohn834 Use selectedIndexes() instead of selectedRows(). Then you can use each index' data()-function to see the data in the selected cell.
edit: I just realized you used selectedRows(0). In that case you can just do selection(0).data().toString() to get a QString representation of the index data.
-
@qwasder85 Thank you.
Dont know what I'm doing wrong here:
QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0); qDebug()<<"\n The content is"<<selection(0).data().toString();
I'm getting error saying:
mainwindow.cpp:151:36: error: type 'QModelIndexList' (aka 'QList<QModelIndex>') does not provide a call operator
Any idea?
-
I got it, was a typo. It should be:
selection[0].data().toString();
-
Thank you @jsulm , @JonB , @qwasder85 :)
-
@russjohn834 said in How to detect two click events:
void MainWindow::on_pushButton_4_clicked()
As a side note, try using more meaningful names for widgets...
Six months from now I bet you'll forget what button 4 is (is it Open? New? Edit?) so:void MainWindow::on_pushButton_Open_clicked()
seems pretty straightforward, right?
-
@Pablo-J-Rogina That's very true. many thanks for your feedback :)