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. How to set the widget has the default focus properties
Forum Updated to NodeBB v4.3 + New Features

How to set the widget has the default focus properties

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 11.1k 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.
  • V Offline
    V Offline
    vivaladav
    wrote on 30 Nov 2017, 08:53 last edited by
    #2

    Probably what you need is setFocusProxy.

    If that doesn't work you can set the focus "manually" after the widget has been shown.

    Davide Coppola
    blog | Linkedin | Twitter

    1 Reply Last reply
    1
    • V Offline
      V Offline
      Vinod Kuntoji
      wrote on 30 Nov 2017, 09:37 last edited by
      #3

      @Qt_crazyer ,

      Hi,

      lineEdit2->setFocus();

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt_crazyer
        wrote on 30 Nov 2017, 10:21 last edited by Qt_crazyer
        #4

        I have used this function. But it doesn't work.

        study::study(QWidget *parent)
            : QMainWindow(parent)
        {
            widget = new QWidget;
            l1 = new QLineEdit;
            l2 = new QLineEdit;
            m1 = new QLabel(tr("Label1"));
            m2 = new QLabel(tr("Lable2"));
            lay = new QHBoxLayout(widget);
            lay->addWidget(m1);
            lay->addWidget(l1);
            lay->addWidget(m2);
            lay->addWidget(l2);
            lay->setSpacing(10);
            lay->setMargin(10);
            l2->setFocus();
            this->setCentralWidget(widget);
        }
        

        Should this function be used here?

        T V 2 Replies Last reply 30 Nov 2017, 10:35
        0
        • Q Qt_crazyer
          30 Nov 2017, 10:21

          I have used this function. But it doesn't work.

          study::study(QWidget *parent)
              : QMainWindow(parent)
          {
              widget = new QWidget;
              l1 = new QLineEdit;
              l2 = new QLineEdit;
              m1 = new QLabel(tr("Label1"));
              m2 = new QLabel(tr("Lable2"));
              lay = new QHBoxLayout(widget);
              lay->addWidget(m1);
              lay->addWidget(l1);
              lay->addWidget(m2);
              lay->addWidget(l2);
              lay->setSpacing(10);
              lay->setMargin(10);
              l2->setFocus();
              this->setCentralWidget(widget);
          }
          

          Should this function be used here?

          T Offline
          T Offline
          Taz742
          wrote on 30 Nov 2017, 10:35 last edited by Taz742
          #5

          @Qt_crazyer
          Please try l2->setFocus(Qt::otherFocusReason);

          Do what you want.

          Q 1 Reply Last reply 30 Nov 2017, 10:42
          0
          • T Taz742
            30 Nov 2017, 10:35

            @Qt_crazyer
            Please try l2->setFocus(Qt::otherFocusReason);

            Q Offline
            Q Offline
            Qt_crazyer
            wrote on 30 Nov 2017, 10:42 last edited by Qt_crazyer
            #6

            @Taz742
            Yes, I have also tried this function, but it doesn't work. This is just a sample program, it is very simple. I do not understand why?...
            0_1512038660188_cd874806-0e47-4208-a215-6842c3404795-image.png

            T 1 Reply Last reply 30 Nov 2017, 10:45
            0
            • Q Qt_crazyer
              30 Nov 2017, 10:42

              @Taz742
              Yes, I have also tried this function, but it doesn't work. This is just a sample program, it is very simple. I do not understand why?...
              0_1512038660188_cd874806-0e47-4208-a215-6842c3404795-image.png

              T Offline
              T Offline
              Taz742
              wrote on 30 Nov 2017, 10:45 last edited by
              #7

              @Qt_crazyer
              An other solution is to use a singleShot timer :

              
              QTimer::singleShot(0,lineEdit,SLOT(setFocus()));
              

              The focus will then be set once the application is free.

              See solution follow the link..

              Do what you want.

              Q 1 Reply Last reply 30 Nov 2017, 10:56
              1
              • Q Qt_crazyer
                30 Nov 2017, 10:21

                I have used this function. But it doesn't work.

                study::study(QWidget *parent)
                    : QMainWindow(parent)
                {
                    widget = new QWidget;
                    l1 = new QLineEdit;
                    l2 = new QLineEdit;
                    m1 = new QLabel(tr("Label1"));
                    m2 = new QLabel(tr("Lable2"));
                    lay = new QHBoxLayout(widget);
                    lay->addWidget(m1);
                    lay->addWidget(l1);
                    lay->addWidget(m2);
                    lay->addWidget(l2);
                    lay->setSpacing(10);
                    lay->setMargin(10);
                    l2->setFocus();
                    this->setCentralWidget(widget);
                }
                

                Should this function be used here?

                V Offline
                V Offline
                vivaladav
                wrote on 30 Nov 2017, 10:54 last edited by
                #8

                @Qt_crazyer I am assuming l2 is a member (using "m" as prefix for members would help).

                Just set the focus with a delay, in your case:

                    QTimer::singleShot(0, this, [this]
                    {
                        l2->setFocus();
                    });
                

                Davide Coppola
                blog | Linkedin | Twitter

                Q 1 Reply Last reply 30 Nov 2017, 10:58
                1
                • T Taz742
                  30 Nov 2017, 10:45

                  @Qt_crazyer
                  An other solution is to use a singleShot timer :

                  
                  QTimer::singleShot(0,lineEdit,SLOT(setFocus()));
                  

                  The focus will then be set once the application is free.

                  See solution follow the link..

                  Q Offline
                  Q Offline
                  Qt_crazyer
                  wrote on 30 Nov 2017, 10:56 last edited by
                  #9

                  @Taz742
                  Thank you so much. The problem has been solved. Thank you for your patient guidance.

                  1 Reply Last reply
                  0
                  • V vivaladav
                    30 Nov 2017, 10:54

                    @Qt_crazyer I am assuming l2 is a member (using "m" as prefix for members would help).

                    Just set the focus with a delay, in your case:

                        QTimer::singleShot(0, this, [this]
                        {
                            l2->setFocus();
                        });
                    
                    Q Offline
                    Q Offline
                    Qt_crazyer
                    wrote on 30 Nov 2017, 10:58 last edited by
                    #10

                    @vivaladav
                    Thank you for your reply. This method is really effective.

                    T 1 Reply Last reply 30 Nov 2017, 11:04
                    0
                    • Q Qt_crazyer
                      30 Nov 2017, 10:58

                      @vivaladav
                      Thank you for your reply. This method is really effective.

                      T Offline
                      T Offline
                      Taz742
                      wrote on 30 Nov 2017, 11:04 last edited by Taz742
                      #11

                      @Qt_crazyer
                      Nope. Do not forget mark this topic as SOLVED.

                      Do what you want.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mpergand
                        wrote on 30 Nov 2017, 12:05 last edited by
                        #12

                        What about this ?

                        setTabOrder(l2,l1);
                        
                        Q 1 Reply Last reply 30 Nov 2017, 12:13
                        0
                        • M mpergand
                          30 Nov 2017, 12:05

                          What about this ?

                          setTabOrder(l2,l1);
                          
                          Q Offline
                          Q Offline
                          Qt_crazyer
                          wrote on 30 Nov 2017, 12:13 last edited by
                          #13

                          @mpergand
                          Thank you for your advice. I have tried this function. This is indeed an effective way.

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            vivaladav
                            wrote on 30 Nov 2017, 14:08 last edited by
                            #14

                            Keep in mind that setTabOrder might not be the right solution if you introduce more widgets (as in any normal working application).

                            Davide Coppola
                            blog | Linkedin | Twitter

                            1 Reply Last reply
                            2

                            11/14

                            30 Nov 2017, 11:04

                            • Login

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