Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidget Clear focus



  • Hello, I want to clear focus when escape key is pressed. I checked the QTableWidget Class documentation, I think there is no option like that for QTableWidget. I tried clear.focus(); but that did not work.

    
    void protocolForm::keyPressEvent(QKeyEvent *event)
    {
        if((event->key()==Qt::Key_Enter) ||(event->key()==Qt::Key_Return ))
        {
            qDebug()<<"Enter pressed";
    
        }
    
        else if (event->key()==Qt::Key_Escape)
        {
            qDebug()<<"ESC PRESSED";
            ui->tableWidget->clearFocus();
    
        }
    }
    

    So is there a way to unfocus from QTableWidget? Thanks.



  • @GunkutA
    Selected/current is not the same thing as focused. As I said, you need to test qDebug() << ui->tableWidget->hasFocus().

    Oh I see you did verify that too. You could try something like setCurrentItem(nullptr), I don't know if that works. Maybe there is an issue over individual cell having focus versus the table widget as a whole. Try setting the widget focus to some other widget and see if that clears the focus on the table.



  • @GunkutA said in QTableWidget Clear focus:

    I tried clear.focus(); but that did not work.

    What does "did not work" mean? What was the behaviour you observed and what did you expect?

    You could precede the ui->tableWidget->clearFocus() with qDebug() << ui->tableWidget->hasFocus(). If that returns false, the QTableWidget does not have focus.



  • @JonB I am printing the focused row and column with qDebug as:

    qDebug()<<"Selected Row:"<<ui->tableWidget->currentRow();
    qDebug()<<"Selected Column:"<<ui->tableWidget->currentColumn();
    

    And after I press ESC, (Also I check key press is detected so I am sure it enters that line) focus is still on the same column and the row.



  • @JonB Yes also it returns true. Maybe clear focus does not work on QWidgetTable then?



  • @GunkutA
    Selected/current is not the same thing as focused. As I said, you need to test qDebug() << ui->tableWidget->hasFocus().

    Oh I see you did verify that too. You could try something like setCurrentItem(nullptr), I don't know if that works. Maybe there is an issue over individual cell having focus versus the table widget as a whole. Try setting the widget focus to some other widget and see if that clears the focus on the table.



  • @JonB setCurrentItem(nullptr), works. Thanks!


Log in to reply