Results 1 to 5 of 5

Thread: Change Stylesheet Using Dynamic Property

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Change Stylesheet Using Dynamic Property

    I'm trying to use a dynamic property to change a style for a QPushButton.

    I have something like the following:

    C++
    Qt Code:
    1. // Create button with dynamic property
    2. button = new QPushButton(this);
    3. button->setObjectName(QString::fromUtf8("MyButton"));
    4. button->setProperty("myProperty", "rank1");
    5. // Button background should be red now
    6. ...
    7. // Change the property
    8. button->setProperty("myProperty", "rank2");
    9. // The button's background colour should be blue now
    To copy to clipboard, switch view to plain text mode 

    QSS
    Qt Code:
    1. QPushButton#MyButton[myProperty="rank1"] { background: red; }
    2. QPushButton#MyButton[myProperty="rank2"] { background: blue; }
    To copy to clipboard, switch view to plain text mode 

    This does work to a certain degree. If I call QApplication::setStyleSheet() after updating myProperty, the style does update correctly. But the style is not updating correctly when I change the property. Is it incorrect to expect the button's background colour to change dynamically when the "myProperty" dynamic property is changed?

  2. #2
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change Stylesheet Using Dynamic Property

    I found this post from a while back: http://www.qtcentre.org/archive/index.php/t-25747.html. It seems that the stylesheet needs to be reapplied after the dynamic property is changed.

    Is there a way to tell a QWidget to reapply its stylesheet rather than calling QWidget::setStyleSheet()? It would be inefficient to call QApplication::setStyleSheet() each time a property on some QWidget changes, particularly seeing that the stylesheet itself has not actually changed. Ideally I would just like the particular QWidget in question to adjust (repaint) itself based on its current stylesheet.

  3. #3
    Join Date
    Jun 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Change Stylesheet Using Dynamic Property

    I ran into the same problem. The documentation somehow suggests to use dynamic properties to style your widgets but this only works at initialization time or when you call setStyleSheet explicitly after you have altered the property's value. I'm actually doing a

    Qt Code:
    1. button->setStyleSheet(button->styleSheet())
    To copy to clipboard, switch view to plain text mode 

    after each property change.

  4. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change Stylesheet Using Dynamic Property

    This is exactly what I found. It does somewhat defeat the purpose of dynamic style properties, but it's still better than having to recreate the stylesheet every time!

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change Stylesheet Using Dynamic Property

    It's just a wild guess: Does it help if you define a NOTIFY signal for your property?

Similar Threads

  1. Replies: 5
    Last Post: 10th October 2012, 21:05
  2. How to mark Property name in Q_PROPERTY for language change??
    By Ankitha Varsha in forum Qt Programming
    Replies: 7
    Last Post: 14th April 2010, 13:22
  3. Dynamic property and stylesheets
    By aamer4yu in forum Qt Programming
    Replies: 10
    Last Post: 18th November 2009, 18:26
  4. Replies: 1
    Last Post: 18th February 2009, 08:34
  5. Difficult:Dynamic Icon size change?!?
    By nupul in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2006, 09:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.