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. Qt stylesheet bug!!
Qt 6.11 is out! See what's new in the release blog

Qt stylesheet bug!!

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 1.9k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Jodie
    wrote on last edited by
    #3

    i had attached it at the end of my post

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jodie
      wrote on last edited by
      #4

      !32131.PNG
      those two group share a same "y",the height of each "border-top" should be same!!!

      Christian EhrlicherC 1 Reply Last reply
      0
      • J Jodie

        !32131.PNG
        those two group share a same "y",the height of each "border-top" should be same!!!

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @Jodie said in Qt stylesheet bug!!:

        those two group share a same "y",the height of each "border-top" should be same!!!

        Why? Also it does not help to adding more '!' to your sentences - a proper problem description including a testcase is much more helpful.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jodie
          wrote on last edited by
          #6

          sorry,here is the code

          #include <QApplication>
          #include <QFile>
          #include <QTextStream>
          #include <QtCore/QVariant>
          #include <QtWidgets/QApplication>
          #include <QtWidgets/QDialog>
          #include <QtWidgets/QGroupBox>
          #include <QtWidgets/QStackedWidget>
          #include <QtWidgets/QWidget>
          #include <QDialog>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QFile f(":/zhang.qss");
              if (f.open(QFile::ReadOnly | QFile::Text))
              {
                  QTextStream ts(&f);
                  qApp->setStyleSheet(ts.readAll());
                  f.close();
              }
              QGroupBox *groupBox_a,*groupBox_b;
              QStackedWidget *stackedWidget;
              QWidget *page;
              QDialog *dlg=new QDialog();
          
              groupBox_a = new QGroupBox("group_a",dlg);
              groupBox_a->setGeometry(QRect(0, 0, 100,100));
          
          
              stackedWidget = new QStackedWidget(dlg);
              stackedWidget->setGeometry(QRect(110, 0, 110, 80));
          
              page = new QWidget();
              groupBox_b = new QGroupBox(page);
              groupBox_b->setGeometry(QRect(0, 0, 100, 80));
              groupBox_b->setTitle("group_b");   //if "groupBox_b->setTitle" here,the margin-top is 10px; showing in picture a
              stackedWidget->addWidget(page);
              //groupBox_b->setTitle("group_b");    //if "groupBox_b->setTitle" here,the margin-top is 0px; showing in picture b
              dlg->show();
          
              return a.exec();
          }
          

          stylesheet file zhang.qss

          QGroupBox{
             border:1px solid  rgb(255,0,0);
             border-radius:6px;
           }
          QGroupBox[title]{
              margin-top: 10px;
              border:1px solid  rgb(0,0,255);
          }
          QGroupBox::title {
          subcontrol-origin: margin;
          }
          

          picture a
          a.PNG

          picture b
          b.PNG

          group_b should look the same as group_a. Because their Stylesheet is the same, but the test results confuse me

          JoeCFDJ 1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #7
            QString group_style_sheet = QString("QGroupBox{border: 2px solid gray;border-radius: 5px;margin-top: %1px;}" )
                                               .arg( font_height * 1.6 );
            

            Set proper margin top in the stylesheet. You can adjust 1.6 to fit your use.

            int getReferenceFontHeight( const QFont & font )
            {
            QRect bounding_rect = QFontMetrics( font ).tightBoundingRect( QString( "MWLPGHXYZmwlpghxyz" ) );
            return bounding_rect.height();
            }

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jodie
              wrote on last edited by
              #8

              Re: Qt stylesheet bug!!
              there are 3 diffdrence between picture a and b;
              1 group_b font size
              2 group_b border color
              3 group_b margin top
              accoding to the stylesheet group b should be blue,because its title is not empty

              QGroupBox[title]{
                  margin-top: 10px;
                  border:1px solid  rgb(0,0,255);
              }
              
              JoeCFDJ 1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #9

                The problem here is that dynamic properties, like title, get cached and don't update automatically. It's not a real dynamic binding.

                When you call setTitle before you add the widget to the stacked widget the style gets cached with the title applied. When you call setTitle after, the style is already cached with no title applied so there's no change.

                To workaround it, after you set the title you can force the style to be re-evaluated for that widget:

                qApp->style()->unpolish(groupBox_b);
                qApp->style()->polish(groupBox_b);
                
                JoeCFDJ 1 Reply Last reply
                4
                • Chris KawaC Chris Kawa

                  The problem here is that dynamic properties, like title, get cached and don't update automatically. It's not a real dynamic binding.

                  When you call setTitle before you add the widget to the stacked widget the style gets cached with the title applied. When you call setTitle after, the style is already cached with no title applied so there's no change.

                  To workaround it, after you set the title you can force the style to be re-evaluated for that widget:

                  qApp->style()->unpolish(groupBox_b);
                  qApp->style()->polish(groupBox_b);
                  
                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #10

                  @Chris-Kawa said in Qt stylesheet bug!!:

                  The problem here is that dynamic properties, like title, get cached and don't update automatically. It's not a real dynamic binding.
                  When you call setTitle before you add the widget to the stacked widget the style gets cached with the title applied. When you call setTitle after, the style is already cached with no title applied so there's no change.
                  To workaround it, after you set the title you can force the style to be re-evaluated for that widget:
                  qApp->style()->unpolish(groupBox_b);
                  qApp->style()->polish(groupBox_b);

                  This may be the better way for it. My way works fine as well and sometimes I want to adjust the gap between the title and border of the group a bit.

                  1 Reply Last reply
                  0
                  • J Jodie

                    Re: Qt stylesheet bug!!
                    there are 3 diffdrence between picture a and b;
                    1 group_b font size
                    2 group_b border color
                    3 group_b margin top
                    accoding to the stylesheet group b should be blue,because its title is not empty

                    QGroupBox[title]{
                        margin-top: 10px;
                        border:1px solid  rgb(0,0,255);
                    }
                    
                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #11

                    @Jodie setting fixed margin-top is not a good idea. If your font changes, this will not work.

                    1 Reply Last reply
                    0
                    • J Jodie

                      sorry,here is the code

                      #include <QApplication>
                      #include <QFile>
                      #include <QTextStream>
                      #include <QtCore/QVariant>
                      #include <QtWidgets/QApplication>
                      #include <QtWidgets/QDialog>
                      #include <QtWidgets/QGroupBox>
                      #include <QtWidgets/QStackedWidget>
                      #include <QtWidgets/QWidget>
                      #include <QDialog>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          QFile f(":/zhang.qss");
                          if (f.open(QFile::ReadOnly | QFile::Text))
                          {
                              QTextStream ts(&f);
                              qApp->setStyleSheet(ts.readAll());
                              f.close();
                          }
                          QGroupBox *groupBox_a,*groupBox_b;
                          QStackedWidget *stackedWidget;
                          QWidget *page;
                          QDialog *dlg=new QDialog();
                      
                          groupBox_a = new QGroupBox("group_a",dlg);
                          groupBox_a->setGeometry(QRect(0, 0, 100,100));
                      
                      
                          stackedWidget = new QStackedWidget(dlg);
                          stackedWidget->setGeometry(QRect(110, 0, 110, 80));
                      
                          page = new QWidget();
                          groupBox_b = new QGroupBox(page);
                          groupBox_b->setGeometry(QRect(0, 0, 100, 80));
                          groupBox_b->setTitle("group_b");   //if "groupBox_b->setTitle" here,the margin-top is 10px; showing in picture a
                          stackedWidget->addWidget(page);
                          //groupBox_b->setTitle("group_b");    //if "groupBox_b->setTitle" here,the margin-top is 0px; showing in picture b
                          dlg->show();
                      
                          return a.exec();
                      }
                      

                      stylesheet file zhang.qss

                      QGroupBox{
                         border:1px solid  rgb(255,0,0);
                         border-radius:6px;
                       }
                      QGroupBox[title]{
                          margin-top: 10px;
                          border:1px solid  rgb(0,0,255);
                      }
                      QGroupBox::title {
                      subcontrol-origin: margin;
                      }
                      

                      picture a
                      a.PNG

                      picture b
                      b.PNG

                      group_b should look the same as group_a. Because their Stylesheet is the same, but the test results confuse me

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #12

                      @Jodie group_b and group_a are different since their font sizes are different.
                      for group box there is default font, you can get it by calling font() with group box pointer or inside the constructor of your own group box class.

                      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