Important: Please read the Qt Code of Conduct -

Retrieving all input fields

  • I have a Dialog with a basic, but repeating structure. The basic structure is: QComboBox1, QComboBox2, QLineEdit1, QComboBox3. This basic structure (in a horizontal layout) is repeated 10 times in the Dialog window.

    What is a smart way to retrieve the input from all fields once a Submit button is clicked. Right now my approach is to find all the children with the QComboBox1 (QComboBox1-1 through QComboBox1-10) name and store the input, find all the children with the QComboBox2 and store the input, etc etc. This approach depends on the the findChildren function retrieving the widgets in the same order each time (which I'm not sure is the case).

    Thanks for any insight.

  • You cannot rely on the order that findChildren returns, so don't go there.

    I'd considder two approaches, that you can use in tandem:

    • First, considder if it makes sense to wrap your row of combo - combo - lineedit - combo into a widget of it's own, and put that widget on the form ten times. If these belong together, the widget can optionally provide a nice, common API that you can directly use, but even if you don't you'll have the widgets that belong together grouped.
    • You should simply manually build up a simple list of your widgets in your constructor. If you decide to use the approach above, that would be only ten widgets in one group, otherwise you probably end up with four groups of ten widgets. You can then simply iterate over this list when you need the values.

    Considder if you can't just create the widgets completely dynamically, from a simple loop in your constructor. That would make building the list trivial, and make it very easy to scale your form to have more or less of these rows when needed. I'm all for using Designer where it makes sense, but sometimes it just makes more sense to build up a (part of the) GUI from manually crafted code.

Log in to reply