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. Change style sheet on the fly
Forum Updated to NodeBB v4.3 + New Features

Change style sheet on the fly

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 896 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.
  • UnitScanU Offline
    UnitScanU Offline
    UnitScan
    wrote on last edited by UnitScan
    #1

    This is the code of my QImprovedSlider class:

    // —› QImprovedSlider methods ‹—
    
    #include <QWidget>
    #include <QDebug>
    #include <QMouseEvent>
    #include <QStyleOptionSlider>
    
    #include "qimprovedslider.h"
    
    class DirectJump : public QProxyStyle
    {
    public:
        using QProxyStyle::QProxyStyle;
        int styleHint(QStyle::StyleHint hint, const QStyleOption* option = 0,
                      const QWidget* widget = 0, QStyleHintReturn* returnData = 0) const
        {
            if (hint == SH_Slider_AbsoluteSetButtons)
                return (Qt::LeftButton);
            return QProxyStyle::styleHint(hint, option, widget, returnData);
        }
    
    };
    
    QImprovedSlider::QImprovedSlider(QWidget *parent) :
        QSlider(parent)
    {
        setStyleSheet(
                    "QSlider::groove:horizontal {"
                    "border: 1px solid #999999;"
                    "height: 5px;" /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
                    "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);"
                    "border-radius: 3px;"
                    "}"
                    "QSlider::handle:horizontal {"
                    "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #81C097);"
                    "border: 1px solid #5c5c5c;"
                    "width: 16px;"
                    "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                    "border-radius: 3px;"
                    "}"
                    "QSlider::handle:horizontal:hover {"
                    "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #FFFFFF, stop:1 #81C097);"
                    "border: 1px solid #808080;"
                    "width: 16px;"
                    "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                    "border-radius: 3px;"
                    "}"
                    "QSlider::add-page:horizontal {"
                    "background: #c0c0c0;"
                    "border-radius: 3px;"
                    "margin: 0 -1px;"
                    "}"
                    "QSlider::sub-page:horizontal {"
                    "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #81C097);"
                    "border-radius: 3px;"
                    "}"
                    );
        setStyle(new DirectJump);
    }
    
    void QImprovedSlider::mousePressEvent(QMouseEvent *event) {
        QStyleOptionSlider opt;
        initStyleOption(&opt);
        QRect sr = style()->subControlRect(QStyle::CC_Slider,
                                           &opt,
                                           QStyle::SC_SliderHandle,
                                           this);
        if (!sr.contains(event->pos()) && event->button() == Qt::LeftButton) {
            if (orientation() == Qt::Vertical)
                setValue(minimum() + ((maximum()-minimum()) * (height()-event->y())) / height() ) ;
            else
                setValue(minimum() + ((maximum()-minimum()) * event->x()) / width() ) ;
        }
        QSlider::mousePressEvent(event);
    }
    
    void QImprovedSlider::mouseReleaseEvent(QMouseEvent *event) {
        if (event->button() == Qt::LeftButton) {
            emit clicked(value());
            QSlider::mouseReleaseEvent(event);
        }
    }
    

    I would like to make sure that I can change its stylesheet on the fly: for that, I created a function using the QColorDialog. This is my attempt

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
        ui->setupUi(this);
    ...
    scrollbar = new QImprovedSlider(this);
    scrollbar->setGeometry(10,87,252,18);
    scrollbar->setOrientation(Qt::Horizontal);
    scrollbar->setMinimum(0);
    ...
    }
    
    void MainWindow::onTextColor()
    {
        QColor color = QColorDialog::getColor(Qt::blue, this );
            if( color.isValid() )
            {
              scrollbar->setStyleSheet( scrollbar->styleSheet().replace(QString("#81C097"), QString(color.name()), Qt::CaseInsensitive));
            }
    }
    

    Of course, this code doesn't work. Could you tell me where is wrong and if there are better methods to change the stylesheet on the fly?

    Thanks in advance

    Christian EhrlicherC 1 Reply Last reply
    0
    • UnitScanU UnitScan

      This is the code of my QImprovedSlider class:

      // —› QImprovedSlider methods ‹—
      
      #include <QWidget>
      #include <QDebug>
      #include <QMouseEvent>
      #include <QStyleOptionSlider>
      
      #include "qimprovedslider.h"
      
      class DirectJump : public QProxyStyle
      {
      public:
          using QProxyStyle::QProxyStyle;
          int styleHint(QStyle::StyleHint hint, const QStyleOption* option = 0,
                        const QWidget* widget = 0, QStyleHintReturn* returnData = 0) const
          {
              if (hint == SH_Slider_AbsoluteSetButtons)
                  return (Qt::LeftButton);
              return QProxyStyle::styleHint(hint, option, widget, returnData);
          }
      
      };
      
      QImprovedSlider::QImprovedSlider(QWidget *parent) :
          QSlider(parent)
      {
          setStyleSheet(
                      "QSlider::groove:horizontal {"
                      "border: 1px solid #999999;"
                      "height: 5px;" /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
                      "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);"
                      "border-radius: 3px;"
                      "}"
                      "QSlider::handle:horizontal {"
                      "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #81C097);"
                      "border: 1px solid #5c5c5c;"
                      "width: 16px;"
                      "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                      "border-radius: 3px;"
                      "}"
                      "QSlider::handle:horizontal:hover {"
                      "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #FFFFFF, stop:1 #81C097);"
                      "border: 1px solid #808080;"
                      "width: 16px;"
                      "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                      "border-radius: 3px;"
                      "}"
                      "QSlider::add-page:horizontal {"
                      "background: #c0c0c0;"
                      "border-radius: 3px;"
                      "margin: 0 -1px;"
                      "}"
                      "QSlider::sub-page:horizontal {"
                      "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #81C097);"
                      "border-radius: 3px;"
                      "}"
                      );
          setStyle(new DirectJump);
      }
      
      void QImprovedSlider::mousePressEvent(QMouseEvent *event) {
          QStyleOptionSlider opt;
          initStyleOption(&opt);
          QRect sr = style()->subControlRect(QStyle::CC_Slider,
                                             &opt,
                                             QStyle::SC_SliderHandle,
                                             this);
          if (!sr.contains(event->pos()) && event->button() == Qt::LeftButton) {
              if (orientation() == Qt::Vertical)
                  setValue(minimum() + ((maximum()-minimum()) * (height()-event->y())) / height() ) ;
              else
                  setValue(minimum() + ((maximum()-minimum()) * event->x()) / width() ) ;
          }
          QSlider::mousePressEvent(event);
      }
      
      void QImprovedSlider::mouseReleaseEvent(QMouseEvent *event) {
          if (event->button() == Qt::LeftButton) {
              emit clicked(value());
              QSlider::mouseReleaseEvent(event);
          }
      }
      

      I would like to make sure that I can change its stylesheet on the fly: for that, I created a function using the QColorDialog. This is my attempt

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
      
          ui->setupUi(this);
      ...
      scrollbar = new QImprovedSlider(this);
      scrollbar->setGeometry(10,87,252,18);
      scrollbar->setOrientation(Qt::Horizontal);
      scrollbar->setMinimum(0);
      ...
      }
      
      void MainWindow::onTextColor()
      {
          QColor color = QColorDialog::getColor(Qt::blue, this );
              if( color.isValid() )
              {
                scrollbar->setStyleSheet( scrollbar->styleSheet().replace(QString("#81C097"), QString(color.name()), Qt::CaseInsensitive));
              }
      }
      

      Of course, this code doesn't work. Could you tell me where is wrong and if there are better methods to change the stylesheet on the fly?

      Thanks in advance

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @UnitScan said in Change style sheet on the fly:

      Of course, this code doesn't work

      Why not?

      scrollbar->setGeometry(10,87,252,18);

      running away and crying out loud

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

      UnitScanU 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @UnitScan said in Change style sheet on the fly:

        Of course, this code doesn't work

        Why not?

        scrollbar->setGeometry(10,87,252,18);

        running away and crying out loud

        UnitScanU Offline
        UnitScanU Offline
        UnitScan
        wrote on last edited by UnitScan
        #3

        Please come back!
        @Christian-Ehrlicher said in Change style sheet on the fly:

        @UnitScan said in Change style sheet on the fly:

        Of course, this code doesn't work

        Why not?

        My scrollbar don't recolor after I pick up the new color via QColorDialog

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Are you sure your style sheet is correctly updated ?

          Note that with your techniqu, you will only be able to change it once.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          UnitScanU 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Are you sure your style sheet is correctly updated ?

            Note that with your techniqu, you will only be able to change it once.

            UnitScanU Offline
            UnitScanU Offline
            UnitScan
            wrote on last edited by
            #5

            @SGaist said in Change style sheet on the fly:

            Hi,

            Are you sure your style sheet is correctly updated ?

            Note that with your techniqu, you will only be able to change it once.

            That's what I'd like to know!
            Is there a better technique? If so, which one?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Print the new style sheet.

              That said if you want to have something that you can change your should rather store your style sheet in a string and use the QString::arg method to generate the final version that you set on your widget.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              UnitScanU 1 Reply Last reply
              1
              • SGaistS SGaist

                Print the new style sheet.

                That said if you want to have something that you can change your should rather store your style sheet in a string and use the QString::arg method to generate the final version that you set on your widget.

                UnitScanU Offline
                UnitScanU Offline
                UnitScan
                wrote on last edited by
                #7

                @SGaist said in Change style sheet on the fly:

                Print the new style sheet.

                The one I get after running my replace command, or how should it be?

                jsulmJ 1 Reply Last reply
                0
                • UnitScanU UnitScan

                  @SGaist said in Change style sheet on the fly:

                  Print the new style sheet.

                  The one I get after running my replace command, or how should it be?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @UnitScan said in Change style sheet on the fly:

                  The one I get after running my replace command

                  Yes, you can even print before and after...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • UnitScanU Offline
                    UnitScanU Offline
                    UnitScan
                    wrote on last edited by UnitScan
                    #9

                    Before:

                                ```
                                "QSlider::groove:horizontal {"
                                "border: 1px solid #999999;"
                                "height: 5px;" /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
                                "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);"
                                "border-radius: 3px;"
                                "}"
                                "QSlider::handle:horizontal {"
                                "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #81C097);"
                                "border: 1px solid #5c5c5c;"
                                "width: 16px;"
                                "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                                "border-radius: 3px;"
                                "}"
                                "QSlider::handle:horizontal:hover {"
                                "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #FFFFFF, stop:1 #81C097);"
                                "border: 1px solid #808080;"
                                "width: 16px;"
                                "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                                "border-radius: 3px;"
                                "}"
                                "QSlider::add-page:horizontal {"
                                "background: #c0c0c0;"
                                "border-radius: 3px;"
                                "margin: 0 -1px;"
                                "}"
                                "QSlider::sub-page:horizontal {"
                                "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #81C097);"
                                "border-radius: 3px;"
                                "}"
                    
                    
                    
                    
                    After
                    
                                    ```
                                     
                                    "QSlider::groove:horizontal {"
                                    "border: 1px solid #999999;"
                                    "height: 5px;" /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
                                    "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);"
                                    "border-radius: 3px;"
                                    "}"
                                    "QSlider::handle:horizontal {"
                                    "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #0000f0);"
                                    "border: 1px solid #5c5c5c;"
                                    "width: 16px;"
                                    "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                                    "border-radius: 3px;"
                                    "}"
                                    "QSlider::handle:horizontal:hover {"
                                    "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #FFFFFF, stop:1 #0000f0);"
                                    "border: 1px solid #808080;"
                                    "width: 16px;"
                                    "margin: -2px 0;" /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
                                    "border-radius: 3px;"
                                    "}"
                                    "QSlider::add-page:horizontal {"
                                    "background: #c0c0c0;"
                                    "border-radius: 3px;"
                                    "margin: 0 -1px;"
                                    "}"
                                    "QSlider::sub-page:horizontal {"
                                    "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #0000f0);"
                                    "border-radius: 3px;"
                                    "}"
                    

                    #81C097 is replaced with #0000f0, where #0000f0 is a color chosen via QColorDialog

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Then use the unpolish/polish QStyle trick.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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