Results 1 to 6 of 6

Thread: Passing variable to stylesheet in runtime

  1. #1
    Join Date
    Mar 2013
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Passing variable to stylesheet in runtime

    Hi,
    is it possible to pass variable from application to stylesheet in runtime ?
    For example, set background image for button:
    QPushButton{
    background-image: buttonImage;
    background-repeat: no-repeat;
    background-position: left;
    }
    where buttonImage is set like setProperty("buttonImage", "url(:/down.png)");

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Passing variable to stylesheet in runtime

    There is some ability to use dynamic properties to change styles on-the-fly
    http://qt-project.org/wiki/DynamicPr...AndStylesheets

    This approach may not be suited to arbitrary changes like the one you are after. If you are trying to change the image on the button based on whether it is pushed/not-pushed on/off etc then you should look at the QIcon class ability to hold multiple images for different states.

  3. #3
    Join Date
    Mar 2013
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing variable to stylesheet in runtime

    I know about property selector, but I need more advanced control to use different images for buttons in runtime.
    QIcon doesn't fit because I'd like to place image on left button side, not on center. Also I need at least 2 states: default and hover.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing variable to stylesheet in runtime

    Have you considered using a custom style instead of stylesheets?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Passing variable to stylesheet in runtime

    Will this help
    Qt Code:
    1. QPushButton button;
    2.  
    3. QString styleSheetTemplate = "QPushButton {"
    4. "background-image: %1;"
    5. "background-repeat: no-repeat;"
    6. "background-position: left;"
    7. "}";
    8.  
    9. button.setProperty("buttonImage", "url(:/down)");
    10.  
    11. QString styleSheet = styleSheetTemplate.arg(button.property("buttonImage").toString());
    12.  
    13. button.setStyleSheet(styleSheet);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Mar 2013
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Passing variable to stylesheet in runtime

    Main idea was to have only one stylesheet for whole application with ability to change some values dynamically, but as I see currently this isn't possible.
    So, I've made workaround, by overriding stylesheet for button with custom background-image property.

Similar Threads

  1. Variable variable names
    By KeineAhnung in forum Newbie
    Replies: 2
    Last Post: 22nd June 2014, 19:00
  2. Passing variable from tab to tab in QTabWidget
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2014, 08:04
  3. connect() variable passing issue
    By hojoff79 in forum Newbie
    Replies: 6
    Last Post: 1st February 2011, 11:18
  4. Replies: 1
    Last Post: 25th September 2010, 08:20
  5. Replies: 3
    Last Post: 8th June 2007, 20:13

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.