Quote Originally Posted by Charvi View Post
I have gone through this but this is not desirable in my case. I want to be able to reference the individual widgets by their object names
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. If you know the object name then:
Qt Code:
  1. QWidget *widget = form->findChildren<QWidget*>("someobjectname");
To copy to clipboard, switch view to plain text mode 
If you don't know the object names ahead of time then they should follow some useful pattern to allow you to pick them from the list of all descendent objects: then you know their names.
so that it will be specific
If an object of the name "someobjectname" name exists and it can be cast to a QWidget* (or QLineEdit* or whatever) then this pointer will specifically identify it.
and want to follow the particular order in which they are on the form when I use getter functions to get the user input.
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.
But my other contradicting requirement is that my loader function has to be generic so that it can load any form which may be created well after the loader project is compiled and deployed.
There's no contradiction here. You are expecting a QMindReadingFormHandler class so you do not have to do the work. There is no such beast. The forms will have to follow some rules you define and your code will access them using those rules. For example, fields to be extracted might be named "fld_{seq}" and limited to text edits and check boxes: any other widget is ignored.

I understand the find() and findChildren() methods are for finding and creating references to widgets with the given object name from the form or for that matter all the widgets of a particular base class. But then the form.children() method should be able to display all the widgets on the form which is loaded through loader.load() (as in my code above) but that is not happening.
These two sentences are not connected. The first is (mostly) correct. The second is confused. 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.

Here is a complete example. It will dump the content of any line edit on your form when you click Dump.
Qt Code:
  1. #include <QtGui>
  2. #include <QUiLoader>
  3. #include <QDebug>
  4.  
  5. class MainWindow: public QMainWindow {
  6. Q_OBJECT
  7. public:
  8. MainWindow(const QString &fileName, QWidget *p = 0): QMainWindow(p), form(0) {
  9. QWidget *central = new QWidget(this);
  10. QVBoxLayout *layout = new QVBoxLayout(central);
  11. QPushButton *button = new QPushButton("Dump", central);
  12. layout->addWidget(button);
  13. central->setLayout(layout);
  14. setCentralWidget(central);
  15.  
  16. connect(button, SIGNAL(clicked()), SLOT(dump()));
  17.  
  18. QUiLoader loader;
  19. QFile file(fileName);
  20. if (file.open(QIODevice::ReadOnly)) {
  21. form = loader.load(&file, this);
  22. layout->insertWidget(0, form);
  23. file.close();
  24. }
  25. }
  26. private slots:
  27. void dump() {
  28. Q_ASSERT(form);
  29. // In "natural" order
  30. QList<QLineEdit*> edits = form->findChildren<QLineEdit*>();
  31. foreach(QLineEdit* edit, edits)
  32. qDebug() << edit->objectName() << edit->text();
  33.  
  34. // Alphabetical order of object name
  35. QMap<QString,QLineEdit*> map;
  36. foreach(QLineEdit* edit, edits)
  37. map.insert(edit->objectName(), edit);
  38. QMapIterator<QString,QLineEdit*> i(map);
  39. while (i.hasNext()) {
  40. i.next();
  41. qDebug() << i.key() << i.value()->text();
  42. }
  43. }
  44.  
  45. private:
  46. QWidget *form;
  47. };
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51. QApplication app(argc, argv);
  52.  
  53. MainWindow m("test.ui");
  54. m.show();
  55. return app.exec();
  56. }
  57. #include "main.moc"
To copy to clipboard, switch view to plain text mode