Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt-为多个标签设置样式表_Qt - Fatal编程技术网

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

Qt-为多个标签设置样式表,qt,Qt,我的窗口中有大约20个标签。我想为其中10个设置相同的样式表,同时为其他的设置另一个样式。有没有更好的方法来做到这一点,而不必为每个标签单独设置样式表 以下是我的代码片段: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { // Some code this->setStyleSheet("background-color: rgb(

我的窗口中有大约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值ạ曼图ấn答复:


编写一个类
StyledLabel
,该类派生自
QLabel
,并在初始化期间设置自己的样式


然后,您可以使用Qt Creator/Designer中的升级小部件功能,使UI中的标签类型为
StyledLabel

您可以使用动态属性和样式表。参考:

您可以为整个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);
请记住在完成unproish和polish之后,样式表将应用于QLabel的每个“类型”
示例代码:

除了其他方法之外,您还可以轻松地迭代固定的小部件列表。它们只是指针,你完全有C++的能力。
const char stylesheet[] = "color: rgb(60, 60, 60); "
                   "background-color: rgb(0, 0, 0); "
                   "border: 1px solid rgb(60, 60, 60);"
                   "border-radius: 10px";

// C++11

for (auto w : {ui->label_id, ui->label_nickname, ui->label_name, ui->label_age})
  w->setStyleSheet(stylesheet);

// C++98

QWidget * const widgets[] = {ui->label_id, ui->label_nickname, ui->label_name, ui->label_age};
int const count = sizeof(widgets)/sizeof(widgets[0]);
for (int i = 0; i < count; ++i) 
  widgets[i]->setStyleSheet(stylesheet);
constchar样式表[]=“颜色:rgb(60,60,60);”
背景色:rgb(0,0,0)
边框:1px实心rgb(60,60,60)
“边界半径:10px”;
//C++11
用于(自动w:{ui->label\u id,ui->label\u昵称,ui->label\u名称,ui->label\u年龄})
w->setStyleSheet(样式表);
//C++98
QWidget*const widgets[]={ui->label\u id,ui->label\u昵称,ui->label\u name,ui->label\u age};
int const count=sizeof(widgets)/sizeof(widgets[0]);
对于(int i=0;isetStyleSheet(样式表);

至少,您可以使用qss定义创建
QString
,以避免代码重复。谢谢您的回答。我认为使用这种方法,我将无法在运行时更改label的样式,对吗?您可以在自定义小部件中实现方便的方法,允许触发样式设置。它也可以是一个连接到信号的插槽,因此只有一个信号可以让所有自定义小部件改变它们的样式。选项无穷无尽。谢谢@ypnos的回答。谢谢你的回答。我想这就是我要找的。我只是不确定应该在哪里定义
QLabel[type=x]
?如果所有QLabel都是“this”小部件的子项,那么可以将QLabel样式表附加到“this”样式表,例如:this->setStyleSheet(“QLabel[type=1]{…);我尝试过这个,但对目标标签没有影响。我用新代码更新了我的问题。怎么了?对不起,样式表需要更改为QLabel[type=“1”],QLabel[type=“2”]。我更新了我的答案。你可以在.ui文件中设置样式。我在回答中附加了我的sameple代码,因为样式表在“this”中小部件,因此您必须使用“this”示例调用unpolish和polish:this->style()->unpolish(ui->label\u id);this->style()->polish(ui->label\u id);
const char stylesheet[] = "color: rgb(60, 60, 60); "
                   "background-color: rgb(0, 0, 0); "
                   "border: 1px solid rgb(60, 60, 60);"
                   "border-radius: 10px";

// C++11

for (auto w : {ui->label_id, ui->label_nickname, ui->label_name, ui->label_age})
  w->setStyleSheet(stylesheet);

// C++98

QWidget * const widgets[] = {ui->label_id, ui->label_nickname, ui->label_name, ui->label_age};
int const count = sizeof(widgets)/sizeof(widgets[0]);
for (int i = 0; i < count; ++i) 
  widgets[i]->setStyleSheet(stylesheet);