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. difference between setFocus() and setFocusPolicy()

difference between setFocus() and setFocusPolicy()

Scheduled Pinned Locked Moved Solved General and Desktop
setfocus
8 Posts 2 Posters 8.8k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Pogi
    wrote on last edited by
    #1

    can someone tell me the difference?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      setFocus to set the keyboard focus on the widget, setFocusPolicy to tell how the widget should accept keyboard focus.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        setFocus to set the keyboard focus on the widget, setFocusPolicy to tell how the widget should accept keyboard focus.

        P Offline
        P Offline
        Pogi
        wrote on last edited by Pogi
        #3

        @SGaist
        i see thanks.

        i have a problem:
        i have a class that inherits QTextEdit called ChatBox, i promoted QTextEdit object to ChatBox
        on the ChatBox constructor i called this function:

        setFocusPolicy(Qt::FocusPolicy::ClickFocus);
        

        heres the reimplemntation of keyPressEvent
        void ChatBox::keyPressEvent(QKeyEvent *event)
        {
        if (event->key() == Qt::Key_Return)
        {
        qDebug() << "enter";
        }
        else
        {
        qDebug() << "notenter"
        QTextEdit::keyPressEvent(event);
        }
        }

        when i pressed enter, theres only a time that if statement will be executed.
        sometimes when i press enter, the else statemet is executed i dont know why.
        sometimes i need to click again on the widget to make it work, and etc.

        if i pressed enter it should and should only execute the if statement.
        im lost now :(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why are you calling keyPressEvent in keyReleaseEvent ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 3 Replies Last reply
          1
          • SGaistS SGaist

            Why are you calling keyPressEvent in keyReleaseEvent ?

            P Offline
            P Offline
            Pogi
            wrote on last edited by Pogi
            #5

            @SGaist i already edited it
            i pasted the wrong code sorry, i was trying keyReleaseEvent.

            1 Reply Last reply
            0
            • SGaistS SGaist

              Why are you calling keyPressEvent in keyReleaseEvent ?

              P Offline
              P Offline
              Pogi
              wrote on last edited by
              #6

              @SGaist update: I tried keyReleaseEvent() still the same

              1 Reply Last reply
              0
              • SGaistS SGaist

                Why are you calling keyPressEvent in keyReleaseEvent ?

                P Offline
                P Offline
                Pogi
                wrote on last edited by
                #7

                @SGaist its fixed. i was pressing the enter key of numpad all this time lmao.
                thanks :)

                i found it when i found that Qt::Key_Return = 16777220
                and event->key() is returning 16777220 which is Qt::Qt::Key_Enter (Enter of numpad)

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You're welcome !

                  Don't forget to either accept or ignore the event when you don't call the base class implementation of keyPressEvent.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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