How to apply changes to multiple ui controls fitting a certain name pattern?

前端 未结 2 1453
北海茫月
北海茫月 2021-01-24 03:18
ui->Pipe_1->setStyleSheet(ui->Pipe_1->property(\"defaultStyleSheet\").toString() +
\" QProgressBar::chunk { background: #D7DF01; }\");
ui->Pipe_2->setS         


        
相关标签:
2条回答
  • 2021-01-24 03:56

    You can very easily find all children matching a certain name pattern:

    // 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; }");
    }
    
    0 讨论(0)
  • 2021-01-24 03:56

    Why do you have 75 progress bars in your UI? Anyway, this is a prime example of why more seasoned Qt UI developers don't use Qt Designer. If you were hand coding your UI, you would just store the pointers in an array as you built them and iterated through later.

    As for your current problem you could set the objectName property in Qt Designer and then use QObject::findChild(..) to retrieve them dynamically, or use Boost::PreProcessor to build the statements for you, or derive from QProgressBar and set the stylesheet in the constructor.

    0 讨论(0)
提交回复
热议问题