[Qt-interest] radio button selection

Denis Akhmetzyanov dakhmetzyanov at smartlabs.tv
Fri Apr 23 17:38:47 CEST 2010


Hi,
It seems to me that reset of the focus occurs on QWidget::setLayout() call.
In the body of this method you could see the lines (Qt 4.6.2):

*void QWidget::setLayout(QLayout *l)*
*{*
*...*
*QObject *oldParent = l->parent();*
*...*
* if (oldParent != this) {*
*        l->setParent(this);*
*        l->d_func()->reparentChildWidgets(this);*

reparentChildWidgets() calls to QWidget::setParent() which contains:

*    if (newParent && isAncestorOf(focusWidget()))*
*        focusWidget()->clearFocus();*

Thus focus is clearing on change the parent. The simple example:

*    QRadioButton *radioBtn1 = new QRadioButton("Radio 1");*
*    QRadioButton *radioBtn2 = new QRadioButton("Radio 2");*
*
*
*    *// second radio button has focus now
*    radioBtn2->setChecked(true);*
*    radioBtn2->setFocus();*
*
*
*    QVBoxLayout *vLayout = new QVBoxLayout(this);*
*    vLayout->addWidget(radioBtn1);*
*    vLayout->addWidget(radioBtn2);*
*
*
*   * // focus resets to first radio button
*    setLayout(vLayout);*
*
*
If you run this example you'd see that the first radio button has focus. It
is because the parent of the radio buttons has been changed. We can fix
that:

   * *// we set the target parent before calling setLayout()
*    QRadioButton *radioBtn1 = new QRadioButton("Radio 1", this);*
*    QRadioButton *radioBtn2 = new QRadioButton("Radio 2", this);*
*
*
*    // second radio button has focus now*
*    radioBtn2->setChecked(true);*
*    radioBtn2->setFocus();*
*
*
*    QVBoxLayout *vLayout = new QVBoxLayout(this);*
*    vLayout->addWidget(radioBtn1);*
*    vLayout->addWidget(radioBtn2);*
*
*
*    *// all right. second radio button has focus
*    setLayout(vLayout);*

So to deal with QStackedWidget we should set the target parents also.
Example:

  *  QStackedWidget *stackedWidget = new QStackedWidget(this);*
*    QWidget *firstPageWidget = new QWidget(stackedWidget);*
*    QVBoxLayout *vLayout = new QVBoxLayout(firstPageWidget);*
*
*
*    QRadioButton *radioBtn1 = new QRadioButton("Radio 1", firstPageWidget);
*
*    QRadioButton *radioBtn2 = new QRadioButton("Radio 2", firstPageWidget);
*
*
*
*    // second radio button has focus now*
*    radioBtn2->setChecked(true);*
*    radioBtn2->setFocus();*
*
*
*    vLayout->addWidget(radioBtn1);*
*    vLayout->addWidget(radioBtn2);*
*
*
*    firstPageWidget->setLayout(vLayout);*
*    stackedWidget->addWidget(firstPageWidget);*

Its OK, second radio button has focus as expected. If we change any parent
then focus resets to first radio button.

In conclusion, IMHO, call to setFocus() after widgets creation and layout is
much easier :-)


2010/4/23 Ramesh <ramesh.bs at robosoftin.com>

>  Ya stackwidget only..
>
>
>
> I created a stack, then I added widget for it..
>
>
>
> In widget  constructor while creating the radio button I am giving focus…
>
> I am putting tat radio button to layout.. adding to stack, giving focus..
>
>
>
> Its perfectly right? Or wat?.. it should work also right?
>
>
>
> *From:* Denis Akhmetzyanov [mailto:dakhmetzyanov at smartlabs.tv]
> *Sent:* Friday, April 23, 2010 6:16 PM
>
> *To:* Ramesh
> *Cc:* Qt-interest at trolltech.com; QtS60-feedback at trolltech.com
> *Subject:* Re: [Qt-interest] radio button selection
>
>
>
> Probably focus transfered to another widget. What's the stack?
> QStackWidget?
>
> Could you show a piece of code which you are using for radio buttons?
>
>
>
> 2010/4/23 Ramesh <ramesh.bs at robosoftin.com>
>
> Hey, sorry denis..
>
> It worked..
>
>
>
> But one doubt.. I did focus after putting the widget to stack..
>
> I don’t know what is the problem L
>
>
>
> *From:* Denis Akhmetzyanov [mailto:dakhmetzyanov at smartlabs.tv]
> *Sent:* Friday, April 23, 2010 5:30 PM
> *To:* Ramesh
> *Cc:* Qt-interest at trolltech.com; QtS60-feedback at trolltech.com
> *Subject:* Re: [Qt-interest] radio button selection
>
>
>
> Hi,
>
> Try to use mOnbutton->setFocus()
>
>
>
> 2010/4/23 Ramesh <ramesh.bs at robosoftin.com>
>
> Hi,
>
>
>
> I have a radio button in my application,
>
> If I do “mOnbutton->setChecked(true)” radio button gets checked, but its
> not getting selected, I mean selection rect is  not appearing but button is
> getting selected..
>
> How can I make button to show the selection rect
>
>
>
> -----------------------------------------------
>
> Robosoft Technologies - Come home to Technology
>
> Disclaimer: This email may contain confidential material. If you were not
> an intended recipient, please notify the sender and delete all copies.
> Emails to and from our network may be logged and monitored. This email and
> its attachments are scanned for virus by our scanners and are believed to be
> safe. However, no warranty is given that this email is free of malicious
> content or virus.
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
>
>
> --
> Best regards,
> Denis Akhmetzyanov
>
>
>
> -----------------------------------------------
>
> Robosoft Technologies - Come home to Technology
>
> Disclaimer: This email may contain confidential material. If you were not
> an intended recipient, please notify the sender and delete all copies.
> Emails to and from our network may be logged and monitored. This email and
> its attachments are scanned for virus by our scanners and are believed to be
> safe. However, no warranty is given that this email is free of malicious
> content or virus.
>
>
>
>
>
>
> --
> Best regards,
> Denis Akhmetzyanov
>
>  -----------------------------------------------
>
> Robosoft Technologies - Come home to Technology
>
> Disclaimer: This email may contain confidential material. If you were not
> an intended recipient, please notify the sender and delete all copies.
> Emails to and from our network may be logged and monitored. This email and
> its attachments are scanned for virus by our scanners and are believed to be
> safe. However, no warranty is given that this email is free of malicious
> content or virus.
>
>


-- 
Best regards,
Denis Akhmetzyanov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100423/9e92c40b/attachment.html 


More information about the Qt-interest-old mailing list