Qt- 为多个标签设置样式表

标签 qt

我的窗口中有大约 20 个标签。我想为其中的 10 个设置相同的样式表,同时为其他设置另一种样式。有没有更好的方法来做到这一点而无需为每个标签单独设置样式表?

这是我的代码片段:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // Some code
    this->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(65, 193, 244)");

    ui->label_id->setStyleSheet("color: rgb(60, 60, 60); "
                                     "background-color: rgb(0, 0, 0); "
                                     "border: 1px solid rgb(60, 60, 60);"
                                     "border-radius: 10px");
    ui->label_nickname->setStyleSheet("color: rgb(60, 60, 60); "
                                     "background-color: rgb(0, 0, 0); "
                                     "border: 1px solid rgb(60, 60, 60);"
                                     "border-radius: 10px");
    ui->label_name->setStyleSheet("color: rgb(255, 255, 255); "
                                     "background-color: rgb(1, 153, 26); "
                                     "border: 1px solid rgb(26, 237, 61); "
                                     "border-radius: 10px");
    ui->label_age->setStyleSheet("color: rgb(255, 255, 255); "
                                     "background-color: rgb(1, 153, 26); "
                                     "border: 1px solid rgb(26, 237, 61); "
                                     "border-radius: 10px");
    // Some code
}

更新:基于Phạm Anh Tuấn回答:

this->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(65, 193, 244);"
                        "QLabel[type=1]"
                        "{"
                        "color: rgb(60, 60, 60);"
                        "background-color: rgb(0, 0, 0);"
                        "border: 1px solid rgb(60, 60, 60);"
                        "border-radius: 10px;"
                        "}"
                        );
ui->label_mstatus_yaw->setProperty("type", 1);
ui->label_mstatus_yaw->style()->unpolish(ui->label_mstatus_yaw);
ui->label_mstatus_yaw->style()->polish(ui->label_mstatus_yaw);

最佳答案

您可以使用动态属性和样式表。引用:https://wiki.qt.io/Dynamic_Properties_and_Stylesheets

您可以为整个 ui 设置样式表:

QLabel[type="1"]
{
    color: rgb(60, 60, 60);
    background-color: rgb(0, 0, 0);
    border: 1px solid rgb(60, 60, 60);
    border-radius: 10px;
}

QLabel[type="2"]
{
    color: rgb(60, 60, 60);
    background-color: rgb(0, 0, 0);
    border: 1px solid rgb(60, 60, 60);
    border-radius: 10px"
}

然后更改标签的“类型”属性

ui->label_id->setProperty("type", 1);
this->style()->unpolish(ui->label_id);
this->style()->polish(ui->label_id);

ui->label_nickname->setProperty("type", 2);
this->style()->unpolish(ui->label_nickname);
this->style()->polish(ui->label_nickname);

记得在之后进行取消抛光和抛光,样式表将应用于 QLabel 的每个“类型” 示例代码:https://drive.google.com/file/d/0Bw2t2i4cHmo_SmdMeGdfZ0VLb3M/view?usp=sharing

关于Qt- 为多个标签设置样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070524/

相关文章:

linux - QT 和 I/O 可能的错误

qt - 如何在布局中的项目上使用 ColorOverlay?

c++ - 检测标准输入是终端还是管道?

qt - 设置 Qt-QML CoverFlow 旋转角度时遇到问题

c++ - QT C++ - QNetworkAccessManager 需要帮助!类(Class)问题

cocoa - 相同的 QtOpenGL 代码在使用 Carbon 时运行速度大约慢 15 倍(与 Cocoa 相比)

c++ - Qt - 如何使用 QFile::copy 使用 QFileDialog 复制文件?

c++ - Qt,如何更改 QComboBox 中一项的文本颜色? (C++)

qt - 如何根据QML中的文本增加矩形的高度

c++ - 在 Qt Stickman 示例中将火柴人的头像更改为我教授的头像