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. [solved] How to set the focus to a new widget dynamically added to the main Dialog ?

[solved] How to set the focus to a new widget dynamically added to the main Dialog ?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 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.
  • X Offline
    X Offline
    xaviermartin
    wrote on last edited by
    #1

    Hello,

    I build a dialog box containing a custom widget which is added dynamically to the main dialog after clicked a push button. I would like that the new widget get the focus after it is added to the dialog box ? How to do it ? Thank

    My code main dialogbox

    @
    QDlg::QDlg(const QStringList &Mnemos, QDialog* parent): QDialog(parent),m_Mnemos(Mnemos)
    {
    setCaption("Input");
    setObjectName(QString("variantdiseasedlg"));
    lpmainVboxLayout = new QVBoxLayout(this);
    lpmainVboxLayout->setObjectName(QString("VerticalMainLayout"));

    lpWidgetVboxLayout = new QVBoxLayout(this);
    qCustomWdg* lpCustomWdg = new qCustomWdg(1, 1, 0, m_Mnemos, this);
    lpCustomWdg ->show();
    lpWidgetVboxLayout->addWidget(lpCustomWdg );

    lpmainVboxLayout->addLayout(lpWidgetVboxLayout);
    QHBoxLayout* lpButtonHboxLayout = new QHBoxLayout;
    //lpButtonHboxLayout->setObjectName(QString("horizontalButtonLayout"));

    lpNewButton = new QPushButton(this);
    lpNewButton ->setText("Add &new");
    lpButtonHboxLayout->addWidget(lpNewButton );
    QSpacerItem* lpSpacer = new QSpacerItem(100,20);
    lpButtonHboxLayout->addItem(lpSpacer);

    QPushButton* lpRemoveButton = new QPushButton(this);
    lpRemoveButton->setText("&Remove");
    lpButtonHboxLayout->addWidget(lpRemoveButton);

    QPushButton* lpCancelButton = new QPushButton(this);
    lpCancelButton->setText("&Cancel");
    lpButtonHboxLayout->addWidget(lpCancelButton);

    QPushButton* lpOkButton = new QPushButton(this);
    lpOkButton->setText("&Integrate");
    lpButtonHboxLayout->addWidget(lpOkButton);

    lpmainVboxLayout->addLayout(lpButtonHboxLayout);

    connect(lpNewButton ,SIGNAL(clicked()),this,SLOT(addNewWdg()));
    connect(lpCancelButton,SIGNAL(clicked()),this,SLOT(reject()));
    connect(lpOkButton,SIGNAL(clicked()),this,SLOT(accept()));

    m_newWdgList.append(lpCustomWdg );

    }
    @

    ....
    code adding the widget dynamically after pushing the "New" button.

    @void QDlg::addNewWdg()
    {
    if (! m_newWdgList.isEmpty())
    {
    //fill the custom widget with previous value;
    qCustomWdg* lpLastWidget = m_newWdgList.last();
    int iStartValue = lpLastWidget ->startValue();
    int iEndValue = lpLastWidget ->endValue();
    int iCurrentComboxIndex = lpLastWidget ->currentIndexComboBox();
    lpCustomWdg = new qCustomWdg(iStartValue, iEndValue, iCurrentComboxIndex, m_Mnemos,this);

    }
    else
    lpCustomWdg = new qCustomWdg(1, 1, 0, m_Mnemos,this);

    m_newWdgList.append(lpCustomWdg );
    lpWidgetVboxLayout->addWidget(lpCustomWdg );
    lpCustomWdg ->show();
    // I try this whithout effect , the focus stay on the main dialog box on the "New" push button
    lpCustomWdg ->setFocusProxy(lpCustomWdg->lpLineEditPositionStart);

    }@

    I suppose that may be the new custom widget can not get the focus because it is not completely build when I assign the focus ... but may be someone know how to solve this.

    Thank for your help.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, welcome to devnet

      You seem to be setting focus proxy but not actually giving it a focus?
      Try to call "setFocus()":http://qt-project.org/doc/qt-5/qwidget.html#setFocus on the lpCustomWdg.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xaviermartin
        wrote on last edited by
        #3

        Hi,

        Thank a lot, effectively giving the focus to the lpCustomWdg ( setfocus() ) and then setting focus proxy work properly !

        Xavier

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Great, please prepend [Solved] to the title of the thread to let others know there's an answer here.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xaviermartin
            wrote on last edited by
            #5

            Ok, this work in Qt4, unfortenately same code doesn't make job in Qt3 ! In Qt3 setting the new CustomWdg isn't effective. Yes probably not a lot of people still using Qt3, but me I do !

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Sorry, I can't help you with Qt3. I'm too young I guess ;)

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xaviermartin
                wrote on last edited by
                #7

                Hi

                So I'm looking for an old QT3 guru to solve my problem ;)

                Thank you

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

                  Hi,

                  AFAIR, this should work the same, what OS are you running on ?

                  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
                  • X Offline
                    X Offline
                    xaviermartin
                    wrote on last edited by
                    #9

                    Yes it should ! working on ... XP and with vs2005...don't laught ! ;)

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      xaviermartin
                      wrote on last edited by
                      #10

                      I post a solution :

                      I do not use setFocus() when using Qt3. After the addition of the new custom widget the focus goes (by magic) into the first QLineEdit of the new custom wdg.

                      Then I set the setTabOrder as follow :

                      @
                      setTabOrder(m_lpCustomWdg->lpCheckBoxLastWidgetOfTheCustom, lpNewButton);
                      setTabOrder(m_lpNewButton, m_lpRemoveButton);
                      setTabOrder(m_lpRemoveButton, m_lpCancelButton);
                      setTabOrder(m_lpCancelButton, m_lpOkButton);
                      @

                      so code is : @void QDlg::addNewWdg()
                      {
                      if (! m_newWdgList.isEmpty())
                      {
                      //fill the custom widget with previous value;
                      qCustomWdg* lpLastWidget = m_newWdgList.last();
                      int iStartValue = lpLastWidget ->startValue();
                      int iEndValue = lpLastWidget ->endValue();
                      int iCurrentComboxIndex = lpLastWidget ->currentIndexComboBox();
                      lpCustomWdg = new qCustomWdg(iStartValue, iEndValue, iCurrentComboxIndex, m_Mnemos,this);

                      }
                      else
                      lpCustomWdg = new qCustomWdg(1, 1, 0, m_Mnemos,this);

                      m_newWdgList.append(lpCustomWdg );
                      lpWidgetVboxLayout->addWidget(lpCustomWdg );
                      lpCustomWdg ->show();
                      //if I do not make a setTabOrder after the last widget of the new custom wdg //the focus goes to the first new widget inserted.
                      setTabOrder(m_lpCustomWdg->lpCheckBoxLastWidgetOfTheCustom, lpNewButton);
                      setTabOrder(m_lpNewButton, m_lpRemoveButton);
                      setTabOrder(m_lpRemoveButton, m_lpCancelButton);
                      setTabOrder(m_lpCancelButton, m_lpOkButton);

                      }@
                      A that's doing the job !

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

                        I won't had to work on that beast too ;)

                        Nice catch !

                        Since you found a solution, can you also please update the thread title prepending [solved] ? So other forums users may know :)

                        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