Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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
C++ 如何在QMessageBox中为特定标签设置样式表?_C++_Qt_Qtstylesheets_Qmessagebox - Fatal编程技术网

C++ 如何在QMessageBox中为特定标签设置样式表?

C++ 如何在QMessageBox中为特定标签设置样式表?,c++,qt,qtstylesheets,qmessagebox,C++,Qt,Qtstylesheets,Qmessagebox,我希望有一个更大的QMessageBox,并在其中居中显示文本,但当我通过stylsheet增加它的大小时,它将如下所示: 如果我能给它一些填充或空白,它会被修复,但我不能 void MainWindow::showMsg() { QMessageBox m_MsgBox; m_MsgBox.setWindowFlags(Qt::Window | Qt::FramelessWindowHint); m_MsgBox.setIcon(QMessageBox::Warnin

我希望有一个更大的QMessageBox,并在其中居中显示文本,但当我通过stylsheet增加它的大小时,它将如下所示:

如果我能给它一些填充或空白,它会被修复,但我不能

void MainWindow::showMsg()
{
    QMessageBox m_MsgBox;
    m_MsgBox.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    m_MsgBox.setIcon(QMessageBox::Warning);
    m_MsgBox.setText("Your Trial is finished! Please purachase a plan.");
    m_MsgBox.setStandardButtons(QMessageBox::Ok);
    m_MsgBox.setStyleSheet("QLabel{min-width:200 px; font-size: 13px;} QPushButton{ width:25px; font-size: 13px; }");
    if(m_MsgBox.exec() == QMessageBox::Ok)
        m_MsgBox.close();
}

我想给这个QMessageBox中的每个QLabel(QMessageBox::Warning&setText)赋予不同的css属性。

您可以通过对象名来寻址小部件中的特定子小部件:

QLabel#MyLabel{style…}

QWidget* p_widget = new QWidget;

QLabel* p_label1 = new QLabel;
p_label1->setText("I'm not cool.");

QLabel* p_label2 = new QLabel;
p_label2->setObjectName("coolLabel");
p_label2->setText("I'm VERY cool.");

QVBoxLayout* p_layout = new QVBoxLayout;
p_layout->addWidget(p_label1);
p_layout->addWidget(p_label2);

p_widget->setLayout(p_layout);

p_widget->setStyleSheet("QLabel {color: black;} QLabel#coolLabel {color: red;}");
这个例子应该有效

不过我想强调的是,通过QS设置大小和布局是一个坏主意。使用QLayouts轻松自如,它们功能强大,充满活力


顺便说一句:“您的试用期已经结束。请购买一份计划。”

您可以通过其对象名称对小部件中的特定子部件进行寻址:

QLabel#MyLabel{style…}

QWidget* p_widget = new QWidget;

QLabel* p_label1 = new QLabel;
p_label1->setText("I'm not cool.");

QLabel* p_label2 = new QLabel;
p_label2->setObjectName("coolLabel");
p_label2->setText("I'm VERY cool.");

QVBoxLayout* p_layout = new QVBoxLayout;
p_layout->addWidget(p_label1);
p_layout->addWidget(p_label2);

p_widget->setLayout(p_layout);

p_widget->setStyleSheet("QLabel {color: black;} QLabel#coolLabel {color: red;}");
这个例子应该有效

不过我想强调的是,通过QS设置大小和布局是一个坏主意。使用QLayouts轻松自如,它们功能强大,充满活力


顺便说一句:“您的试用期已经结束。请购买计划。”

我在qt论坛上得到了这个答案:

Only answering to your topic title, if you look into the source code of QMessageBox, every label has a object name, so that should be easy to set different style to them by using ID selector.

text: "qt_msgbox_label"

icon: "qt_msgboxex_icon_label"

informativeText: "qt_msgbox_informativelabel"

Note: These names may change in future versions.

我在qt论坛上得到了这个答案:

Only answering to your topic title, if you look into the source code of QMessageBox, every label has a object name, so that should be easy to set different style to them by using ID selector.

text: "qt_msgbox_label"

icon: "qt_msgboxex_icon_label"

informativeText: "qt_msgbox_informativelabel"

Note: These names may change in future versions.

非常感谢。但是我如何将它用于QWidget中的QLabel,它使用setText()谢谢!但我如何将其用于QLabel,它位于QWidget中,它使用setText()