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. Calling setFocus() in the constructor doesn't work

Calling setFocus() in the constructor doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 972 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.
  • LorenDBL Offline
    LorenDBL Offline
    LorenDB
    wrote on last edited by
    #1

    I have a widget, derived from QWidget, that has code similar to this in its constructor:

    m_someWidget->setFocus();
    

    The problem is that the widget doesn't actually get the focus. I experimented with using a timer that calls setFocus() 1 second after the constructor runs. That works. However, I'd like to be able to set the focus from the constructor, without adding another signal/slot/both to my class.

    Pl45m4P 1 Reply Last reply
    0
    • LorenDBL LorenDB

      @Pl45m4 Thanks for the advice. I may just leave my setup as-is for simplicity, but what you said makes sense.

      P.S. I like your signature!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #6

      @LorenDB
      @Pl45m4 is correct: "focus" is only implemented when widget is shown, it doesn't have any meaning during, say, construction. showEvent() is the earliest you should set your focus.

      1 Reply Last reply
      3
      • LorenDBL LorenDB

        I have a widget, derived from QWidget, that has code similar to this in its constructor:

        m_someWidget->setFocus();
        

        The problem is that the widget doesn't actually get the focus. I experimented with using a timer that calls setFocus() 1 second after the constructor runs. That works. However, I'd like to be able to set the focus from the constructor, without adding another signal/slot/both to my class.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #2

        @LorenDB

        I guess while constructing your widget is not ready to receive focus.

        @LorenDB said in Calling setFocus() in the constructor doesn't work:

        without adding another signal/slot

        Why?

        Also, you don't need to add a slot to your subclass, you can connect your widget, where you create it and give focus since setFocus() is already a slot.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • LorenDBL Offline
          LorenDBL Offline
          LorenDB
          wrote on last edited by
          #3

          @Pl45m4 said in Calling setFocus() in the constructor doesn't work:

          you can connect your widget, where you create it and give focus since setFocus() is already a slot.

          Could you elaborate on how to do that?

          I currently am using a function in the widget's parent to focus after widget construction. A diagram:

          someWidget
           |__m_someOtherWidget
                   |__m_widgetToFocus
          

          m_someOtherWidget has a function to focus m_widgetToFocus. This function is called by someWidget right after m_someOtherWidget is constructed.

          Pl45m4P 1 Reply Last reply
          0
          • LorenDBL LorenDB

            @Pl45m4 said in Calling setFocus() in the constructor doesn't work:

            you can connect your widget, where you create it and give focus since setFocus() is already a slot.

            Could you elaborate on how to do that?

            I currently am using a function in the widget's parent to focus after widget construction. A diagram:

            someWidget
             |__m_someOtherWidget
                     |__m_widgetToFocus
            

            m_someOtherWidget has a function to focus m_widgetToFocus. This function is called by someWidget right after m_someOtherWidget is constructed.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #4

            @LorenDB

            Something like

            // #### Parent widget ####
            m_widget = new MyWidget;
            
            // #### widget constructor ####
            connect(this, &MyWidget::createdSignal, this, &MyWidget::setFocus);
            
            

            Then emit this signal either in your widget's c'tor or inside its showEvent, which should work better.
            After thinking about it, just moving setFocus() inside the reimplemented showEvent should also work (showEvent happens, when widget is fully constructed and about to get painted on your screen)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            LorenDBL 1 Reply Last reply
            2
            • Pl45m4P Pl45m4

              @LorenDB

              Something like

              // #### Parent widget ####
              m_widget = new MyWidget;
              
              // #### widget constructor ####
              connect(this, &MyWidget::createdSignal, this, &MyWidget::setFocus);
              
              

              Then emit this signal either in your widget's c'tor or inside its showEvent, which should work better.
              After thinking about it, just moving setFocus() inside the reimplemented showEvent should also work (showEvent happens, when widget is fully constructed and about to get painted on your screen)

              LorenDBL Offline
              LorenDBL Offline
              LorenDB
              wrote on last edited by
              #5

              @Pl45m4 Thanks for the advice. I may just leave my setup as-is for simplicity, but what you said makes sense.

              P.S. I like your signature!

              JonBJ 1 Reply Last reply
              0
              • LorenDBL LorenDB

                @Pl45m4 Thanks for the advice. I may just leave my setup as-is for simplicity, but what you said makes sense.

                P.S. I like your signature!

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #6

                @LorenDB
                @Pl45m4 is correct: "focus" is only implemented when widget is shown, it doesn't have any meaning during, say, construction. showEvent() is the earliest you should set your focus.

                1 Reply Last reply
                3

                • Login

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