Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set StyleSheet for an specific label in QMessageBox?
QtWS25 Last Chance

How to set StyleSheet for an specific label in QMessageBox?

Scheduled Pinned Locked Moved Solved General and Desktop
c++qmessageboxcssstylesheet
8 Posts 3 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muhammad Mirab Br.
    wrote on last edited by Muhammad Mirab Br.
    #1

    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:

    alt text

    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.

    Any help will be appreciated. Thanks!

    JonBJ 1 Reply Last reply
    1
    • M Muhammad Mirab Br.

      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:

      alt text

      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.

      Any help will be appreciated. Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Muhammad-Mirab-Br said in How to set StyleSheet for an specific label in QMessageBox?:

      min-width:200 px;

      Probably not an issue, but if you are having a problem why do you put a space into 200px in case that is not acceptable to QSS? Why make it different from all your other XXpx directives?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Muhammad Mirab Br.
        wrote on last edited by
        #3

        Sorry, I forgot to share the photo of it. I have problem with that the QMessageBox::Warning is having 200px width and It's not what I expected, I want to specify different QSS for these labels(QMessageBox::Warning, QLabel).
        alt text

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          2
          • B Bonnie

            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.

            M Offline
            M Offline
            Muhammad Mirab Br.
            wrote on last edited by
            #5

            @Bonnie Thanks! This really helped. This is what I was searching for.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Muhammad Mirab Br.
              wrote on last edited by
              #6

              @Bonnie How can I Make the button center align in QMessagebox? I tried: move(), setGeometry() and etc.

              At all, I want to make the button center. Thanks.

              B 1 Reply Last reply
              0
              • M Muhammad Mirab Br.

                @Bonnie How can I Make the button center align in QMessagebox? I tried: move(), setGeometry() and etc.

                At all, I want to make the button center. Thanks.

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #7

                @Muhammad-Mirab-Br
                Option 1: find QDialogButtonBox and set property

                m_MsgBox.findChild<QDialogButtonBox*>(QString(), Qt::FindDirectChildrenOnly)->setCenterButtons(true);
                

                Option 2: set property by stylesheet:

                QDialogButtonBox { qproperty-centerButtons: true; }
                
                M 1 Reply Last reply
                3
                • B Bonnie

                  @Muhammad-Mirab-Br
                  Option 1: find QDialogButtonBox and set property

                  m_MsgBox.findChild<QDialogButtonBox*>(QString(), Qt::FindDirectChildrenOnly)->setCenterButtons(true);
                  

                  Option 2: set property by stylesheet:

                  QDialogButtonBox { qproperty-centerButtons: true; }
                  
                  M Offline
                  M Offline
                  Muhammad Mirab Br.
                  wrote on last edited by
                  #8

                  Thanks @Bonnie, You are AWESOME!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved