QTableWidget Programmatically Select Cells



  • Hello,

    I currently have a QTableWidget setup with 5 rows and 3 columns and only the cells under column 1 are delectable. I have a slot:

    void MainWindow::on_imgTable_cellClicked(int row, int column)
    

    which makes it so when a cell in that column is selected a corresponding image is displayed in a separate QLabel widget. This works totally fine.

    However, column 0 contains buttons that when clicked either add an image path or remove one into a variable, depending on if one was there or not already. It is these images that are previewed when the cells in the column 1 are selected. I want to add the additional functionality so that when you click one of the buttons in column 0 to add an image, its corresponding cell in column 1 is automatically selected as if the user had clicked on it and that slot above is triggered (again as if the user had actually clicked the cell).

    I checked through the documentation and tried using "QAbstractItemView::setCurrentIndex() " and something like:

    select(TABLEWIDG->model()->index(row,colum), QItemSelectionModel::Select);
    

    but in both cases the cell is "selected" with a dull grey color that is barely distinguishable from the white and the slot is not activated. So, how can I have the cell programmatically selected with the typical blue highlight as if the user had clicked it and have the cells "clicked()" slot trigger? I am assuming I can just call the slot if having it trigger without the user actually clicking the cell isn't possible. If that is the case I just need to know how to get it to appear selected with the standard blue highlight.

    Thank you.


  • Lifetime Qt Champion

    Hi,

    IIRC, you should set the focus on the QTableWidget so it makes it the active widget.



  • @SGaist

    Thank you. That seemed to be the last missing piece. It does what I want now.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.