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. Palette overriding style sheet
QtWS25 Last Chance

Palette overriding style sheet

Scheduled Pinned Locked Moved Solved General and Desktop
stylesheetpalettestyle
8 Posts 4 Posters 2.0k 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.
  • A Offline
    A Offline
    aljoachim
    wrote on 16 Oct 2022, 17:08 last edited by
    #1

    Hi,

    I am working on a desktop application. There is a QListWidget that contains custom widgets. These custom widgets are quite simple, they each contain just a few fields separated by vertical lines, each showing some text (QLabel, QLCDNumber, maybe a small flag icon).

    The point is, these widgets need to be created/changed quite quickly. They also need to change background/text color quite quickly. When I am changing color with setStyleSheet, it is not so fast (it is the bottleneck). When I use QPalette, it runs multiple times faster. The problem is, we are using style sheets in the app, so a style sheet gets inevitably propagated to these widgets. Is there any way these 2 techniques can be combined? Would using QStyle override app style sheet and run faster? Is there any other way to speed this up?

    J 1 Reply Last reply 16 Oct 2022, 17:55
    0
    • A aljoachim
      16 Oct 2022, 17:08

      Hi,

      I am working on a desktop application. There is a QListWidget that contains custom widgets. These custom widgets are quite simple, they each contain just a few fields separated by vertical lines, each showing some text (QLabel, QLCDNumber, maybe a small flag icon).

      The point is, these widgets need to be created/changed quite quickly. They also need to change background/text color quite quickly. When I am changing color with setStyleSheet, it is not so fast (it is the bottleneck). When I use QPalette, it runs multiple times faster. The problem is, we are using style sheets in the app, so a style sheet gets inevitably propagated to these widgets. Is there any way these 2 techniques can be combined? Would using QStyle override app style sheet and run faster? Is there any other way to speed this up?

      J Offline
      J Offline
      JonB
      wrote on 16 Oct 2022, 17:55 last edited by
      #2

      @aljoachim
      So far as I understand it, stylesheet always overrides palette and that's how it works.

      I cannot comment on whether/why stylesheet is not fast enough for you. Maybe code could be optimized. Maybe using dynamic properties would help.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 16 Oct 2022, 18:58 last edited by
        #3

        Hi,

        It seems that your widgets are essentially read-only stuff. If so (and even if not), you would get better performance (and customization) by implementing a custom QStyledItemDelegate.

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

        A 1 Reply Last reply 16 Oct 2022, 19:17
        0
        • S SGaist
          16 Oct 2022, 18:58

          Hi,

          It seems that your widgets are essentially read-only stuff. If so (and even if not), you would get better performance (and customization) by implementing a custom QStyledItemDelegate.

          A Offline
          A Offline
          aljoachim
          wrote on 16 Oct 2022, 19:17 last edited by
          #4

          @SGaist thank you, that seems fine.

          From what I understand, I have to reimplement QStyledItemDelegate::paint method, and draw the widget "manually", as for example here. Does that mean that I have to draw my widget "manually" or is there any simpler way to do this?

          S 1 Reply Last reply 17 Oct 2022, 09:36
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 16 Oct 2022, 19:36 last edited by
            #5

            That's a pretty good example.

            It might look complicate but less than you might think.

            You work within the rectangle and paint what you want. The possibly most complicated would be the LCD. For that one you could use a QLcdWidget as part of your delegate and render it after updating its value.

            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
            • A aljoachim
              16 Oct 2022, 19:17

              @SGaist thank you, that seems fine.

              From what I understand, I have to reimplement QStyledItemDelegate::paint method, and draw the widget "manually", as for example here. Does that mean that I have to draw my widget "manually" or is there any simpler way to do this?

              S Offline
              S Offline
              SimonSchroeder
              wrote on 17 Oct 2022, 09:36 last edited by
              #6

              @aljoachim said in Palette overriding style sheet:

              From what I understand, I have to reimplement QStyledItemDelegate::paint method, and draw the widget "manually", as for example here. Does that mean that I have to draw my widget "manually" or is there any simpler way to do this?

              I recently had a similar problem (using a QStyleItemDelegate myself). Inside the paint method I used this code for drawing:

                  void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
                  {
                      static QPushButton* dummyButton = new QPushButton(); // use this to force the correct style sheet
                  
                      QStyleOptionButton button;
                      button.rect = option.rect;
                      button.text = "Push Me";
                      button.state = QStyle::State_Enabled;
                  
                      const QWidget *widget = option.widget;
                      QStyle *style = widget ? widget->style() : QApplication::style();
                      style->drawControl(QStyle::CE_PushButton, &button, painter, dummyButton);
                  }
              

              This was specifically to draw a push button. You have to figure out how this translates to the things you are trying to draw.

              J 1 Reply Last reply 17 Oct 2022, 09:40
              0
              • S SimonSchroeder
                17 Oct 2022, 09:36

                @aljoachim said in Palette overriding style sheet:

                From what I understand, I have to reimplement QStyledItemDelegate::paint method, and draw the widget "manually", as for example here. Does that mean that I have to draw my widget "manually" or is there any simpler way to do this?

                I recently had a similar problem (using a QStyleItemDelegate myself). Inside the paint method I used this code for drawing:

                    void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
                    {
                        static QPushButton* dummyButton = new QPushButton(); // use this to force the correct style sheet
                    
                        QStyleOptionButton button;
                        button.rect = option.rect;
                        button.text = "Push Me";
                        button.state = QStyle::State_Enabled;
                    
                        const QWidget *widget = option.widget;
                        QStyle *style = widget ? widget->style() : QApplication::style();
                        style->drawControl(QStyle::CE_PushButton, &button, painter, dummyButton);
                    }
                

                This was specifically to draw a push button. You have to figure out how this translates to the things you are trying to draw.

                J Offline
                J Offline
                JonB
                wrote on 17 Oct 2022, 09:40 last edited by JonB
                #7

                @SimonSchroeder
                Hi Simon. Purely OoI, unlike you to "leak" a push button! Any reason why you didn't use, say, static QPushButton dummyButton; [not sure you need the static now] here?

                S 1 Reply Last reply 18 Oct 2022, 06:58
                0
                • J JonB
                  17 Oct 2022, 09:40

                  @SimonSchroeder
                  Hi Simon. Purely OoI, unlike you to "leak" a push button! Any reason why you didn't use, say, static QPushButton dummyButton; [not sure you need the static now] here?

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 18 Oct 2022, 06:58 last edited by
                  #8

                  @JonB said in Palette overriding style sheet:

                  Any reason why you didn't use, say, static QPushButton dummyButton; here?

                  I guess I fumbled to long to actually get the style sheet to apply. I tried many different things (because I didn't see this documented anywhere). Must have overlooked this when cleaning up. Thank you! Will fix it in my code.

                  1 Reply Last reply
                  1

                  1/8

                  16 Oct 2022, 17:08

                  • 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