I am trying to do something that I thought it would be terribly easy : Adding QRadioButton into my QWizardPage without any layout.
The following code that adds QRadioButton is being called when user clicks button Next (signal currentIdChanged calls the code):
int newPositionY = 0;
for (vector<Events::VCS::PnPDevice>::const_iterator it=m_devices.begin(); it!=m_devices.end(); it++)
{
if (it->type == Events::VCS::HEADSET)
{
//add a radio button
stringstream text;
text << (it->name) << " " << (it->serialNumber);
QRadioButton* radioButton = new QRadioButton(ui.wpSINGLE_USER);
radioButton->setGeometry(50, 20 + newPositionY, 260, 40);
radioButton->setText(text.str().c_str());
newPositionY = newPositionY + 40
}
}
}
I added this little piece of code to see what is going on with my QRadioButton
QList<QRadioButton*> listButton = ui.wpSINGLE_USER->findChildren<QRadioButton*>();
int size = listButton.size();
QRect rect1 = listButton[0]->rect();
QRect rect2 = listButton[1]->rect();
I then realized that it seems that the problem might be QRect.
The value for rect1 and rect2 are erroneous. rect1 = (0, 0, 259, 39) and rect2 = (0, 0, 259, 39) Correct value for rect1 should be (50, 20, 260, 40) andn for rect2 (50, 60, 260, 40)
So, what is the problem, how to add QRadioButton into QWidget without layout?
EDIT
That is strange, if, instead of adding the QRadioButton into the QWizardPage when user clicks button next I am adding them into the QWizard contructor, it works.
Can somebody tell me why I am not able to add QRadioButton into my QWizardPage into my slot function?
Thanks
QRect
is behaving properly. You should check the geometry()
, not the rect()
.
http://doc-snapshot.qt-project.org/4.8/qwidget.html#geometry-prop
http://doc-snapshot.qt-project.org/4.8/qwidget.html#rect-prop
http://doc-snapshot.qt-project.org/4.8/application-windows.html#window-geometry
Here are some QWizard
examples that are worth studying...
http://qt-project.org/doc/qt-4.8/dialogs-trivialwizard.html
http://qt-project.org/doc/qt-4.8/dialogs-classwizard.html
http://qt-project.org/doc/qt-4.8/dialogs-licensewizard.html
Generally speaking...
Using Layouts, makes your life easier. There is a learning curve and the code looks funny at first, but you will be grateful that you did in the long run. If at all possible, I avoid using Qt Designer, and I use layouts, and nested layouts to position everything. Add in stretches and spacers and anything else you need. It's great. And then if you need something to show or hide later, you nest that in a QWidget
, and you show and hide that widget to adjust when it is visible.
http://qt-project.org/doc/qt-4.8/layout.html
http://qt-project.org/doc/qt-4.8/tutorials-widgets-windowlayout.html
It might be that when a QRadioButton
is added without a layout, it isn't updating your widget.
If you change what is in your widget, you need to post an update()
event for it to be reflected.
http://doc-snapshot.qt-project.org/4.8/qwidget.html#update
Also when you don't use layouts, the layering and positioning can go a little crazy. Make sure that you are not drawing something else on top of the location where the button is added.
Here is some more things to keep in mind. Though it looks like you may be taking care of some of this already...
Another issue that can happen is if you add a QRadioButton
not into a layout and forget to parent it, it probably won't get added at all. Parenting of QObject
s is handled nicely by layouts, too.
And finally, you need to make sure that your button exists for the scope of your parent widget. So if you declare it in a slot, and it has a local scope, by the time you leave the slot, it is gone. (You put it on the heap, so it should be okay).
So make it a member variable for your subclassed QWizardPage
, and make it a pointer. Qt handles the parenting of objects well, and the deletion correctly if they are on the object tree properly.
http://qt-project.org/doc/qt-4.8/objecttrees.html
Also be sure to look at using a 'QGroupBox' in your layout when you are using QRadioButton
s. This handles the exclusive attribute well.
http://doc-snapshot.qt-project.org/4.8/widgets-groupbox.html
http://doc-snapshot.qt-project.org/4.8/qradiobutton.html#details
Hope that helps.