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.
  • Vinod KuntojiV Offline
    Vinod KuntojiV Offline
    Vinod Kuntoji
    wrote on last edited by
    #3

    @Qt_crazyer ,

    Hi,

    lineEdit2->setFocus();

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

    1 Reply Last reply
    0
    • Qt_crazyerQ Offline
      Qt_crazyerQ Offline
      Qt_crazyer
      wrote on 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?

      Taz742T V 2 Replies Last reply
      0
      • Qt_crazyerQ Qt_crazyer

        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?

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by Taz742
        #5

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

        Do what you want.

        Qt_crazyerQ 1 Reply Last reply
        0
        • Taz742T Taz742

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

          Qt_crazyerQ Offline
          Qt_crazyerQ Offline
          Qt_crazyer
          wrote on 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

          Taz742T 1 Reply Last reply
          0
          • Qt_crazyerQ Qt_crazyer

            @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

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on 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.

            Qt_crazyerQ 1 Reply Last reply
            1
            • Qt_crazyerQ Qt_crazyer

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

              Qt_crazyerQ 1 Reply Last reply
              1
              • Taz742T Taz742

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

                Qt_crazyerQ Offline
                Qt_crazyerQ Offline
                Qt_crazyer
                wrote on 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

                  @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();
                      });
                  
                  Qt_crazyerQ Offline
                  Qt_crazyerQ Offline
                  Qt_crazyer
                  wrote on last edited by
                  #10

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

                  Taz742T 1 Reply Last reply
                  0
                  • Qt_crazyerQ Qt_crazyer

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

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on 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 last edited by
                      #12

                      What about this ?

                      setTabOrder(l2,l1);
                      
                      Qt_crazyerQ 1 Reply Last reply
                      0
                      • M mpergand

                        What about this ?

                        setTabOrder(l2,l1);
                        
                        Qt_crazyerQ Offline
                        Qt_crazyerQ Offline
                        Qt_crazyer
                        wrote on 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 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

                          • Login

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