Important: Please read the Qt Code of Conduct -

QTableWidget keeps currentrow to deselected row

  • Hi I have a QTableWidget that I select items from. I show information about the currently selected item in the mainwindow widget. However when I deselect an item the current row stays as the row just deselected. What I'd like it to do is change current row to -1 if no row remains selected or change current row to the most recently selected row of the remaining selected rows. Thanks for the help.

  • Lifetime Qt Champion


    How are you currently connecting this together ?

    Maybe using the selection model would be a better solution.

    Hope it helps

  • Here's the code:

    void ReplicatorMainScreen::updateButtons()
        for (int count = 0; count < ui->jobsTableWidget->rowCount(); count++)
            if (ui->jobsTableWidget->item(count,0)->isSelected())
                selected = true;
        if (selected == true)
            emit updateJobInfo(ui->jobsTableWidget->currentRow(),0);

    I was thinking maybe I could use a stack to keep track of selected rows and change to the previous selected row when necessary... anything easier ideas?

  • Lifetime Qt Champion

    Why not use the selection model hasSelection method for that purpose ?

  • Sorry for the delayed response been busy.. can it do what I mentioned in the first post regarding current row being changed to most recently selected row?

  • Lifetime Qt Champion

    You have to store the last selected row and re-use it when needed.

Log in to reply