QT Skills Series (10)
Batch processing or traverse the page control sample code
Qt 's UI when programming interface, encounter this situation, the distribution of a large number of identical controls, such as the interface SpinBox _1 , SpinBox _2 , SpinBox _3 and a series of controls. For so many controls are set unified operation or to find a control, no doubt using traversed way is the best way to approach the situation in several code examples are given below for reference purposes only.
1, page traversing a certain type of control
QList<QLabel *> lblhws = ui->page2->findChildren<QLabel *>();
foreach (QLabel *lbl, lblhws) {
if (lbl != nullptr){
QString lblobj = lbl->objectName();
if (lblobj.indexOf("labelT1") > -1)
{
lbl->setText(tmpList.at(1)+"℃");
}
if (lblobj.indexOf("labelT2") > -1)
{
lbl->setText(tmpList.at(0)+"%RH");
}
}
}
2, iterate through a similar set of controls name
page controls established in accordance with the database name, batch processing operation is based on data Name:
for (int i=0;i < countSensor; i++)
{
QPushButton *btn = this->findChild<QPushButton *>
("pushButton_SW"+QString::number(query.value("SNO").toInt()));
if (btn != nullptr){
btn->setVisible(true);
btn->setToolTip(dat1);
btn->setStyleSheet("background:green;");
}
}
3, bulk provisioning process control event page:
As an example, the bulk valueChanged unified set of events for all SinBox:
QList<QSpinBox *> SpinBox = this->findChildren<QSpinBox *>();
qDebug() << SpinBox.count();
for(int i=0; i < SpinBox.count(); i++)
{
connect(SpinBox.at(i),SIGNAL(valueChanged(int)),this,SLOT(on_valueChanged(int)));
}
Note: The actual example, eliminating A puzzled.
- not thinking between the end, the real-time critique your work!