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. QObject::setProperty() signalling solution?
Forum Updated to NodeBB v4.3 + New Features

QObject::setProperty() signalling solution?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 4.8k 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.
  • VRoninV VRonin
    1. The fastest way to force an update of the stylesheet is
    widget.style().unpolish(widget)
    widget.style().polish(widget)
    

    Source: https://wiki.qt.io/Dynamic_Properties_and_Stylesheets

    1. Are you defining the properties yourself? if so, you can just emit a signal when the property changes (usually marked as NOTIFY) and connect widgetPropertyChanged to that signal
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #3

    @VRonin
    Hmmm, food for thought (investigation), thanks!

    Those polish words, are they to do with Poland?

    From your doc link:

    myLineEdit->setProperty("urgent", true);
    myLineEdit->style()->unpolish(myLineEdit);
    myLineEdit->style()->polish(myLineEdit);

    Note that this must be done in the widget to which the style was applied. QStyle::polish accepts either a QWidget or a QApplication as a parameter.

    So I presume I must unpolish and then polish, right? But I do not understand the "Note" sentence at all: "this must be done in the widget to which the style was applied", pardon?

    Are you defining the properties yourself?

    Umm, at present I am particularly interested in all my calls to change the class property, because I do that a lot. class is a CSS/QSS property. My application-wide, common stylesheet will have rules like:

    .backgroundColor8 { background-color: rgba(191, 216, 193, 1); }
    .backgroundColor9 { background-color: rgba(243, 227, 235, 1); }
    

    Note these are . rules. I can just change property class as I want to swap between various ones.

    I don't know how this relates to your question "Are you defining the properties yourself?"?

    1 Reply Last reply
    1
    • VRoninV VRonin
      1. The fastest way to force an update of the stylesheet is
      widget.style().unpolish(widget)
      widget.style().polish(widget)
      

      Source: https://wiki.qt.io/Dynamic_Properties_and_Stylesheets

      1. Are you defining the properties yourself? if so, you can just emit a signal when the property changes (usually marked as NOTIFY) and connect widgetPropertyChanged to that signal
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @VRonin
      OOI, for your Poland suggestion I find http://dgovil.com/blog/2017/02/24/qt_stylesheets/ :

      This is very easy and there are two ways you can do this:

      # Option 1
      myWidget.setStyle(myWidget.style())
      
      #OR
      
      # Option 2
      myWidget.style().unpolish(myWidget)
      myWidget.style().polish(myWidget)
      myWidget.update()
      

      Personally I prefer the first one since it’s a one liner, but just presenting both options here for you in case you want more control over the process.

      I like the one-liner :)

      As for your:

      Are you defining the properties yourself?

      Not sure, but for that and the link about "NOTIFY notifySignal": in Python/PyQt, we don't have things like Q_PROPERTY macros, we just use QWidget.setProperty(name, value). I don't think there's any difference whether the property name is pre-defined or new...

      VRoninV 1 Reply Last reply
      0
      • JonBJ JonB

        @VRonin
        OOI, for your Poland suggestion I find http://dgovil.com/blog/2017/02/24/qt_stylesheets/ :

        This is very easy and there are two ways you can do this:

        # Option 1
        myWidget.setStyle(myWidget.style())
        
        #OR
        
        # Option 2
        myWidget.style().unpolish(myWidget)
        myWidget.style().polish(myWidget)
        myWidget.update()
        

        Personally I prefer the first one since it’s a one liner, but just presenting both options here for you in case you want more control over the process.

        I like the one-liner :)

        As for your:

        Are you defining the properties yourself?

        Not sure, but for that and the link about "NOTIFY notifySignal": in Python/PyQt, we don't have things like Q_PROPERTY macros, we just use QWidget.setProperty(name, value). I don't think there's any difference whether the property name is pre-defined or new...

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #5

        @JonB said in QObject::setProperty() signalling solution?:

        we just use QWidget.setProperty(name, value)

        So you are setting a dynamic property. In that case you can just filter for QDynamicPropertyChangeEvent to detect changes

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        JonBJ 1 Reply Last reply
        1
        • VRoninV VRonin

          @JonB said in QObject::setProperty() signalling solution?:

          we just use QWidget.setProperty(name, value)

          So you are setting a dynamic property. In that case you can just filter for QDynamicPropertyChangeEvent to detect changes

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #6

          @VRonin
          I should have made it explicit that these are dynamic properties, that's all I use.

          Yes, I started by saying I have come across QDynamicPropertyChangeEvent.

          I would like all my various widgets to automatically do the polish/setStyle() whenever their class dynamic property is changed by my code, so that I don't have to remember to call it explicitly every time. It's a design-approach-advice question. I can see two possibilities:

          • Set up QDynamicPropertyChangeEvent filter on every widget to do polish.

          • In my derived classes for each widget, offer a method which sets a property like class and then does polish.

          Any advantages/comments?

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #7

            I would subclass QWidget (sorry for C++):

            class PolishedWidget : public QWidget {
                Q_OBJECT
                Q_DISABLE_COPY(PolishedWidget)
            public:
                explicit PolishedWidget(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
                    : QWidget(parent, f)
                {}
            protected:
                bool event(QEvent* event) Q_DECL_OVERRIDE
                {
                    if (event->type() == QEvent::DynamicPropertyChange) {
                        style()->unpolish(this);
                        style()->polish(this);
                    }
                    return QWidget::event(event);
                }
            };
            

            Now all you need to do is change your other widgets to inherit from PolishedWidget rather than QWidget

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            JonBJ 1 Reply Last reply
            2
            • VRoninV VRonin

              I would subclass QWidget (sorry for C++):

              class PolishedWidget : public QWidget {
                  Q_OBJECT
                  Q_DISABLE_COPY(PolishedWidget)
              public:
                  explicit PolishedWidget(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
                      : QWidget(parent, f)
                  {}
              protected:
                  bool event(QEvent* event) Q_DECL_OVERRIDE
                  {
                      if (event->type() == QEvent::DynamicPropertyChange) {
                          style()->unpolish(this);
                          style()->polish(this);
                      }
                      return QWidget::event(event);
                  }
              };
              

              Now all you need to do is change your other widgets to inherit from PolishedWidget rather than QWidget

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #8

              @VRonin
              No, of course I would do this if I could!! :)

              I'm not defining my own widgets derived from QWidget. I'm using all the various Qt widgets, QLineEdit, QPushButton, etc., etc. They all of course already derive from QWidget, I'd like them to have derived from PolishedWidget but they don't!

              I'm saying I have gone through code and made it so I always create my own wrapper classes, one for each Qt widget class I use (like MyLineEdit just deriving from QLineEdit). So we have the luxury of a derived class for each widget class to put our code in. So we can do either:

              • The way above, filtering the event, in each derived class's definition. Outside world will call myLine.setProperty("class", ...).

              • In each derived class's definition, supply a method, like setProperty() or setPropertyClass() or whatever, which includes calling polish() code itself (no event filtering). Outside world will call myLine.setPropertyClass(...).

              Do we prefer one approach over the other?

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #9

                Do we prefer one approach over the other?

                I do prefer the first option but I wouldn't subclass every single Qt Class.

                In C++ You have 2 options (I'm 100% sure the second one is available to python too):

                template <class T>
                class PolishedWidget : public T{
                public:
                    explicit PolishedWidget(QWidget* parent = Q_NULLPTR)
                        : T(parent, f)
                    {}
                protected:
                    bool event(QEvent* event) Q_DECL_OVERRIDE
                    {
                        if (event->type() == QEvent::DynamicPropertyChange) {
                            style()->unpolish(this);
                            style()->polish(this);
                        }
                        return T::event(event);
                    }
                };
                

                Now you can create PolishedWidget<QLineEdit> myLine; and PolishedWidget<QPushButton> myButton;


                Install an event filter:

                class Polisher : public QObject {
                    Q_OBJECT
                    Q_DISABLE_COPY(Polisher)
                public:
                    Polisher(QObject* parent = Q_NULLPTR)
                        : QObject(parent)
                    { }
                protected:
                    bool eventFilter(QObject* obj, QEvent* event) Q_DECL_OVERRIDE
                    {
                        if (event->type() == QEvent::DynamicPropertyChange) {
                            QWidget* objWidget = qobject_cast<QWidget*>(obj);
                            if (objWidget) {
                                objWidget->style()->unpolish(objWidget);
                                objWidget->style()->polish(objWidget);
                            }
                        }
                        return QObject::eventFilter(obj, event);
                    }
                };
                

                Now you can use:

                Polisher* polisher = new Polisher(parent);
                QLineEdit* myLine =new QLineEdit(parent);
                QPushButton* myButton=new QPushButton(parent);
                myLine ->setEventFilter(polisher);
                myButton->setEventFilter(polisher);
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                JonBJ 1 Reply Last reply
                1
                • VRoninV VRonin

                  Do we prefer one approach over the other?

                  I do prefer the first option but I wouldn't subclass every single Qt Class.

                  In C++ You have 2 options (I'm 100% sure the second one is available to python too):

                  template <class T>
                  class PolishedWidget : public T{
                  public:
                      explicit PolishedWidget(QWidget* parent = Q_NULLPTR)
                          : T(parent, f)
                      {}
                  protected:
                      bool event(QEvent* event) Q_DECL_OVERRIDE
                      {
                          if (event->type() == QEvent::DynamicPropertyChange) {
                              style()->unpolish(this);
                              style()->polish(this);
                          }
                          return T::event(event);
                      }
                  };
                  

                  Now you can create PolishedWidget<QLineEdit> myLine; and PolishedWidget<QPushButton> myButton;


                  Install an event filter:

                  class Polisher : public QObject {
                      Q_OBJECT
                      Q_DISABLE_COPY(Polisher)
                  public:
                      Polisher(QObject* parent = Q_NULLPTR)
                          : QObject(parent)
                      { }
                  protected:
                      bool eventFilter(QObject* obj, QEvent* event) Q_DECL_OVERRIDE
                      {
                          if (event->type() == QEvent::DynamicPropertyChange) {
                              QWidget* objWidget = qobject_cast<QWidget*>(obj);
                              if (objWidget) {
                                  objWidget->style()->unpolish(objWidget);
                                  objWidget->style()->polish(objWidget);
                              }
                          }
                          return QObject::eventFilter(obj, event);
                      }
                  };
                  

                  Now you can use:

                  Polisher* polisher = new Polisher(parent);
                  QLineEdit* myLine =new QLineEdit(parent);
                  QPushButton* myButton=new QPushButton(parent);
                  myLine ->setEventFilter(polisher);
                  myButton->setEventFilter(polisher);
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #10

                  @VRonin
                  For #1: Yeah, now the question is I wonder how/whether I'm supposed to do template class in Python/PyQt... !

                  However, the point is you prefer event filter approach over explicit "set-property-with-call-to-polish" method. I'll go down that route.

                  For #2: I don't want to have to insert a line of a code to setEventFilter after each of thousands of lines of existing code, wherever any widget is created. Not to remember to do it in the future. Since I am now in a position where every QLineEdit or QPushButon is already a JLineEdit or JPushButon [code only uses, say, 10 Qt widget types, so that's the sub-classing I've done], I can move the polishing code into its event filter and not insert loads of lines across loads of files.

                  VRoninV 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @VRonin
                    For #1: Yeah, now the question is I wonder how/whether I'm supposed to do template class in Python/PyQt... !

                    However, the point is you prefer event filter approach over explicit "set-property-with-call-to-polish" method. I'll go down that route.

                    For #2: I don't want to have to insert a line of a code to setEventFilter after each of thousands of lines of existing code, wherever any widget is created. Not to remember to do it in the future. Since I am now in a position where every QLineEdit or QPushButon is already a JLineEdit or JPushButon [code only uses, say, 10 Qt widget types, so that's the sub-classing I've done], I can move the polishing code into its event filter and not insert loads of lines across loads of files.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #11

                    @JonB said in QObject::setProperty() signalling solution?:

                    now the question is I wonder how/whether I'm supposed to do template class in Python/PyQt

                    How: https://docs.python.org/3.6/library/typing.html#user-defined-generic-types
                    Whether: give it a try, and see if it works

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    JonBJ 1 Reply Last reply
                    2
                    • VRoninV VRonin

                      @JonB said in QObject::setProperty() signalling solution?:

                      now the question is I wonder how/whether I'm supposed to do template class in Python/PyQt

                      How: https://docs.python.org/3.6/library/typing.html#user-defined-generic-types
                      Whether: give it a try, and see if it works

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #12

                      @VRonin
                      I have made the change (in my own way!) so that the widgets I use have an event filter on dynamic property changes to cause required polishing. I examine QDynamicPropertyChangeEvent::propertyName() and do so only if it's one of mine (like class). There are other cases too (e.g. I make setting/clearing read-only affect line edit's background color).

                      How expensive do you think polish/unpolish() (or for that matter re-assigning setStyleSheet() if I chose to do it that way) are?

                      The approach we have chosen re-polishes every time I set some "property". However, somewhere in the docs it states we only need to do this when we change a property on something like an already-displayed widget.

                      Thus, if I set properties immediately after widget construction --- as I may well do --- I will do re-polishes for each one, even though none of them are necessary. OTOH, if I want to change the properties at a later date after widget has been shown, I would indeed need the re-polishing, but only then.

                      I haven't found all the circumstances, but I'm wondering whether my event filter could test something like QWidget::isVisible() before doing the re-polish, so that it does not do it when unnecessarily early in the widget's life-cycle. Any opinion/comment?

                      VRoninV 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @VRonin
                        I have made the change (in my own way!) so that the widgets I use have an event filter on dynamic property changes to cause required polishing. I examine QDynamicPropertyChangeEvent::propertyName() and do so only if it's one of mine (like class). There are other cases too (e.g. I make setting/clearing read-only affect line edit's background color).

                        How expensive do you think polish/unpolish() (or for that matter re-assigning setStyleSheet() if I chose to do it that way) are?

                        The approach we have chosen re-polishes every time I set some "property". However, somewhere in the docs it states we only need to do this when we change a property on something like an already-displayed widget.

                        Thus, if I set properties immediately after widget construction --- as I may well do --- I will do re-polishes for each one, even though none of them are necessary. OTOH, if I want to change the properties at a later date after widget has been shown, I would indeed need the re-polishing, but only then.

                        I haven't found all the circumstances, but I'm wondering whether my event filter could test something like QWidget::isVisible() before doing the re-polish, so that it does not do it when unnecessarily early in the widget's life-cycle. Any opinion/comment?

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #13

                        @JonB said in QObject::setProperty() signalling solution?:

                        could test something like QWidget::isVisible() before doing the re-polish

                        You certainly could

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        JonBJ 1 Reply Last reply
                        0
                        • VRoninV VRonin

                          @JonB said in QObject::setProperty() signalling solution?:

                          could test something like QWidget::isVisible() before doing the re-polish

                          You certainly could

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #14

                          @VRonin
                          Do you happen to know the "cost" of unpolish/polish? If it is "cheap", or perhaps "does nothing" on an unshown widget anyway, I won't worry my little head....

                          J.HilkJ 1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #15

                            It is style dependant so there's no unique answer. On windows it looks to be relatively expensive if your widget is not visible

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            2
                            • JonBJ JonB

                              @VRonin
                              Do you happen to know the "cost" of unpolish/polish? If it is "cheap", or perhaps "does nothing" on an unshown widget anyway, I won't worry my little head....

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #16

                              @JonB it should be at least less expensive than setStyleSheet


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              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