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?
Forum Updated to NodeBB v4.3 + New Features

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.2k 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 30 Jun 2020, 06:37 last edited by Muhammad Mirab Br. 7 Jan 2020, 05:54
    #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!

    J 1 Reply Last reply 30 Jun 2020, 09:02
    1
    • M Muhammad Mirab Br.
      30 Jun 2020, 06:37

      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!

      J Offline
      J Offline
      JonB
      wrote on 30 Jun 2020, 09:02 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 1 Jul 2020, 05:59 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 1 Jul 2020, 06:29 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 1 Jul 2020, 06:45
          2
          • B Bonnie
            1 Jul 2020, 06:29

            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 1 Jul 2020, 06:45 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 1 Jul 2020, 08:17 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 1 Jul 2020, 08:40
              0
              • M Muhammad Mirab Br.
                1 Jul 2020, 08:17

                @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 1 Jul 2020, 08:40 last edited by Bonnie 7 Jan 2020, 08:41
                #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 1 Jul 2020, 09:17
                3
                • B Bonnie
                  1 Jul 2020, 08:40

                  @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 1 Jul 2020, 09:17 last edited by
                  #8

                  Thanks @Bonnie, You are AWESOME!

                  1 Reply Last reply
                  0

                  1/8

                  30 Jun 2020, 06:37

                  • Login

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