Search code examples
c++qtqwidget

Enumerable Qt widgets


I have a Qt dialog box with several controls that need more or less uniform processing. I want to store pointers to them in an array and enumerate over them.

In Windows, I'd use sequential control IDs and GetDlgItem(hDlg, IDC_BASEID + i) in a loop. In Cocoa, i'd use sequential view tags or include them in an invisible container. What's the Qt way?


Solution

  • Maybe QObject::findChildren(QRegExp) can do the trick if the widgets have similar names.

    QList<QLineEdit*> lineEdits = dialog->findChildren<QLineEdit*>(QRegExp("lineEdit[0-9]+"));
    foreach (QLineEdit* lineEdit, lineEdits) {
        lineEdit->clear();
    }