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?
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();
}
The dupe answer tells you how to do this - but you probably shouldn't
The Qt way is to have the same slot (ie handler) registered for all the widgets - then in the event handler use the field in the event to tell you which widget fired and get a pointer to it.
精彩评论