如何将更改应用于适合特定名称模式的多个 ui 控件?

2024-01-09

ui->Pipe_1->setStyleSheet(ui->Pipe_1->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");
ui->Pipe_2->setStyleSheet(ui->Pipe_2->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");
ui->Pipe_3->setStyleSheet(ui->Pipe_3->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");
ui->Pipe_4->setStyleSheet(ui->Pipe_4->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");
ui->Pipe_5->setStyleSheet(ui->Pipe_5->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");
ui->Pipe_6->setStyleSheet(ui->Pipe_6->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");
ui->Pipe_7->setStyleSheet(ui->Pipe_7->property("defaultStyleSheet").toString() +
" QProgressBar::chunk { background: #D7DF01; }");

我有这种代码,我想采用 for 循环来减少我的代码。管道是以下实例QProgressBar.

我尝试过:

for(x=1; x<75; x++){
QString y="Pipe_&d,x";
ui->Pipe_1->setObjectName(y);
ui->Pipe_1->setStyleSheet(ui->Pipe_1->property("defaultStyleSheet").toString() +
        " QProgressBar::chunk { background: #D7DF01; }");
}

但它不起作用我该如何处理?谢谢您的回答...


您可以很容易地找到与特定名称模式匹配的所有子项:

// C++11
auto pipes = findChildren<QProgressBar*>(QRegExp("Pipe_[0-9]+"));
for (pipe : pipes) {
  pipe->setStyleSheet(pipe->property("defaultStyleSheet").toString() +
    " QProgressBar::chunk { background: #D7DF01; }");
}

// C++98
QList<QProgressBar*> pipes = findChildren<QProgressBar*>(QRegExp("Pipe_[0-9]+"));
foreach (pipe, pipes) {
  pipe->setStyleSheet(pipe->property("defaultStyleSheet").toString() +
    " QProgressBar::chunk { background: #D7DF01; }");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将更改应用于适合特定名称模式的多个 ui 控件? 的相关文章

随机推荐