2

I Want to have a bigger QMessageBox and centered texts in it but when I increase the size of it by stylsheet it'll be like this:

Text is not centered

if I could give it Some Padding or margin it would be fixed but I can't.

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();
}

I want to give different css properties to each QLabel(QMessageBox::Warning & setText) in this QMessageBox.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Muhammad Mirab Br.
  • 133
  • 1
  • 4
  • 13

2 Answers2

3

You can address specific sub-widgets within a widget by their object name:

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;}");

This example should work.

I want to stress though, that setting sizes and layouts through QSS is a bad idea. Get comfortable with QLayouts, they are very powerful and dynamic!

btw: "Your trial has ended. Please purchase a plan."

i know nothing
  • 951
  • 1
  • 10
  • 27
1

I got this answer in qt forum:

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.
Dharman
  • 30,962
  • 25
  • 85
  • 135
Muhammad Mirab Br.
  • 133
  • 1
  • 4
  • 13