Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget Clear focus
Qt 6.11 is out! See what's new in the release blog

QTableWidget Clear focus

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GunkutA
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • G GunkutA

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @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.

      G 1 Reply Last reply
      3
      • G GunkutA

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @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.

        G 2 Replies Last reply
        0
        • JonBJ JonB

          @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.

          G Offline
          G Offline
          GunkutA
          wrote on last edited by
          #3

          @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.

          1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            G Offline
            G Offline
            GunkutA
            wrote on last edited by
            #4

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

            JonBJ 1 Reply Last reply
            0
            • G GunkutA

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @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.

              G 1 Reply Last reply
              3
              • JonBJ JonB

                @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.

                G Offline
                G Offline
                GunkutA
                wrote on last edited by
                #6

                @JonB setCurrentItem(nullptr), works. Thanks!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved