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. [Solved] Applying style on derived widget with custom property failes.
QtWS25 Last Chance

[Solved] Applying style on derived widget with custom property failes.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 7.8k 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.
  • W Offline
    W Offline
    Woomla
    wrote on last edited by
    #1

    I've created a MyWidget derived from QWidget with a custom property "selected".
    I've set the stylesheet to
    @
    *[selected="true"]
    {
    border: 5px solid #FFF7BC;
    background-color: #BCF7FF;
    }
    @

    When I check the property in Qt-creator, the style is applied.

    When I create a MyWidget in the constructor of MainWindow and set the property, the style is not applied.
    I've added the same style to the MainWindow, reverted the colors, but this style is also not applied.
    When I create a QWidget in the constructor of MainWindow and set the property, the style is applied.

    @
    DpsMainWindow::DpsMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DpsMainWindow)
    {
    ui->setupUi(this);

    MyWidget* widget = new MyWidget(this);
    widget->setProperty("selected", true);
    widget->style()->unpolish(widget);
    widget->style()->polish(widget);
    widget->update(); //style is not applied !!

    QWidget* qwidget = new QWidget(this);
    qwidget->setProperty("selected", true); //style is applied !!!
    }
    @

    Using QT 5.0.1 on windows 7 64-bit with Qt-Creator 2.6.2.

    Something is missing I quess, but I don't know what that is.

    project files:
    "DynamicPropStyle.pro":http://pastebin.com/FeLXRHNg
    "dspmainwindow.cpp":http://pastebin.com/YsKzj6MC
    "dspmainwindow.h":http://pastebin.com/R7jZWNzm
    "dspmainwindow.ui":http://pastebin.com/8kaekxJ1
    "mywidget.cpp":http://pastebin.com/8aRgP5Wc
    "mywidget.h":http://pastebin.com/8ReXM8wr
    "mywidget.ui":http://pastebin.com/FgH9Tz45

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      You must reimplement paintEvent for your custom widget. Nothing too drastic just this:

      @
      void DPSMainWindow::paintEvent(QPaintEvent *)
      {
      QStyleOption opt;
      opt.init(this);
      QPainter p(this);
      style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
      }
      @

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        -You do not need to reimplement the paintEvent for your custom widget.-

        -However, you do need to realize that unfortunately, style renderings are not automatically updated on the change of custom properties. To make that work, you will need to unpolish and then repolish your widget, or re-set the style sheet on it. Otherwise, the style sheet is not re-evaluated, and your property change has no effect. I have filed a bug on that a long time ago already...-

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          [quote author="Andre" date="1363171690"]You do not need to reimplement the paintEvent for your custom widget.

          [/quote]

          But in "Qt Style Sheets Reference":http://qt-project.org/doc/qt-4.8/stylesheet-reference.html document it is specified that

          bq. QWidget
          Supports only the background, background-clip and background-origin properties.
          If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

          @void CustomWidget::paintEvent(QPaintEvent *)
          {
          QStyleOption opt;
          opt.init(this);
          QPainter p(this);
          style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
          } @

          bq. The above code is a no-operation if there is no stylesheet set.

          So for a custom widget should we reimplement the painEvent or not ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Actually, I think I might be wrong. Sam gave a detailed explanation why. Thanks for correcting me.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

              @Andre

              QWidgets + StyleSheets is always confusing to me thats why I added a note in "How to Change the Background Color of QWidget":http://qt-project.org/wiki/How_to_Change_the_Background_Color_of_QWidget and "Trigger an update for the widget while using dynamic properties.":http://qt-project.org/doc/qt-5.0/qtwidgets/stylesheet-syntax.html#note-238.

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Woomla
                wrote on last edited by
                #7

                Great, it works.

                I've added the paintEvent and I've created a setter for this property where I update the style:

                @
                void MyWidget::setSelected(bool value)
                {
                setProperty("selected", value);
                style()->unpolish(this);
                style()->polish(this);
                update();
                }
                @

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  elveatles
                  wrote on last edited by
                  #8

                  Can someone explain why painEvent needs to be reimplemented? This seems very unintuitive when all that's needed is the default stylesheet functionality.

                  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