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



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


  • Moderators

    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.



  • Hi,

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

    Xavier


  • Moderators

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



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


  • Moderators

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



  • Hi

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

    Thank you


  • Lifetime Qt Champion

    Hi,

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



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



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


  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.