Originally Posted by
ChrisW67
Make up your mind. One moment you list all children, then actually want all descendants, then complain that findChildren() requires an object name, and when told it does not complain that you want to extract a widget by object name.
Locating a single object using the generic findChildren() has nothing to do with displaying the dynamically loaded form. Your code above does nothing like displaying the form. If you want to do that you follow the simple pattern in the documentation for
QUiLoader. Load the form and insert the resulting widget into the layout of your program.
Ok. May be this is because I did not explain my requirement.
First I wanted to create a generic loader program which will load any form. Till this I was done. I used the basic example in the QUiLoader example and did it.
Now I want to take user input from this dynamically loaded form. (So if user types something into the line edit then take that input, etc.) But the QUiLoader class does not give direct access to the pointers of the widgets it creates while loading the form (these are private variables plus it doesnt store them.) We can create instances in our program by using object names which my function, as it is generic, wont have it. So first I get the object names, then the base classes of all widgets to create the references of to widgets.
So I tried to get the object names as follows as u suggested:
QList<QWidget *> list = form->findChildren<QWidget *>()
for(i = 0; i < list.size(); i++)
{
QString name
= list.
at(i
)->objectName
();
qDebug() <<"object"<< i <<" = "<< qPrintable(name);
}
QList<QWidget *> list = form->findChildren<QWidget *>()
for(i = 0; i < list.size(); i++)
{
QString name = list.at(i)->objectName();
qDebug() <<"object"<< i <<" = "<< qPrintable(name);
}
To copy to clipboard, switch view to plain text mode
It was good for many widgets. But for some widgets (I think the containers) it gives even the widgets inside them like follows:
object 96 = textEdit
object 97 = qt_scrollarea_viewport
object 98 = qt_scrollarea_hcontainer
object 99 =
object 100 = qt_scrollarea_vcontainer
object 101 =
object 102 = plainTextEdit
object 103 = qt_scrollarea_viewport
object 104 = qt_scrollarea_hcontainer
object 105 =
object 106 = qt_scrollarea_vcontainer
object 107 =
object 108 = spinBox
object 109 = qt_spinbox_lineedit
object 110 = doubleSpinBox
object 111 = qt_spinbox_lineedit
object 112 = timeEdit
object 113 = qt_spinbox_lineedit
object 96 = textEdit
object 97 = qt_scrollarea_viewport
object 98 = qt_scrollarea_hcontainer
object 99 =
object 100 = qt_scrollarea_vcontainer
object 101 =
object 102 = plainTextEdit
object 103 = qt_scrollarea_viewport
object 104 = qt_scrollarea_hcontainer
object 105 =
object 106 = qt_scrollarea_vcontainer
object 107 =
object 108 = spinBox
object 109 = qt_spinbox_lineedit
object 110 = doubleSpinBox
object 111 = qt_spinbox_lineedit
object 112 = timeEdit
object 113 = qt_spinbox_lineedit
To copy to clipboard, switch view to plain text mode
This is a problem.
Once I find a way out of this I can use widget.inherits("") to find the base class and then create the reference.
That is what the form tab order, set in the Designer, is for. The order the focus moves "naturally" around the form is how the user
might enter the data. It has no bearing on the order they actually enter the data or the order in which you can access the widget content.
Also after taking the user input I want to send this to some other machine in form of packet. This is the sole reason I am worried about the order of widgets in the form because I will send the values in that order only. So no comples protocol needs to be established.
Well this is bad. Sorry if I annoyed you.
Here is a complete example.
Thank you very much for the code. It is useful.
Bookmarks