Important: Please read the Qt Code of Conduct -

How to detect two click events

  • Hi all,

    I have a QTableWidget. I need to return something when the cellClicked and a pushbutton clicked. Something like this:

    void MainWindow::on_tableWidget_cellClicked(int row, int column)
       if(ui->pushButton_4->clicked()) -------> I cant do this!
        qDebug()<< "\n content is:"<<ui->tableWidget->item(row,column=0)->text();

    It says:

    mainwindow.cpp:157:7: error: value of type 'void' is not contextually convertible to 'bool'

    What is the right way to do this?

    Thanks in advance

  • Lifetime Qt Champion

    @russjohn834 You can't click at two different widgets at the same time. What do you really want to do?

  • @russjohn834
    As @jsulm says above. Unless maybe you have two mice (mouses?) & two hands ;-)

    What is this pushbutton? Is it a pushbutton you have put in the cell in the table widget? Is it a totally unrelated button somewhere else?

    If by any chance you mean it's unrelated/elsewhere, and you want to know if it was clicked previously to the cell click, you would have to store the fact that something else happened earlier into a variable to examine now. But I don't know if that's what you might mean!

  • @jsulm
    I need to select a row in a QTabelWidget and press a button to send a signal (a string with cell content) and to open a new window.

  • @JonB
    As shown below. I selected the row. Now if I press Open 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 like on_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., something like

    QModelIndexList 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:


  • 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 :)

Log in to reply