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. Dynamic Property for stylesheet

Dynamic Property for stylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 822 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.
  • B Offline
    B Offline
    baechlju
    wrote on last edited by baechlju
    #1

    I need to change the color of a button dynamically. I tried this based on a property "orderstatus".
    Another approach didn't work for me too: https://stackoverflow.com/questions/21685414/qt5-setting-background-color-to-qpushbutton-and-qcheckbox.
    But i would prefer a way, which allows me to set the specific color in the stylesheet.

    My code for the property approach:

    //Code in UIOrder.cpp
    void UIOrder::statChanged(OrderStatus::Enum stat)
    {
      m_ui.btnStatus->setProperty("orderstatus", stat);
      int status = m_ui.btnStatus->property("orderstatus").toInt();
      m_ui.btnStatus->repaint();
    }
    
    /*Code in stylsheet*/
    *[orderstatus="1"]{	background: rgb(   0,   0, 255, 255); }
    *[orderstatus="2"]{	background: rgb(   0, 255,   0, 255); }
    /*...*/
    

    int status has the right value after executing, so i guess, the property was set sucessfully.
    But the button doesn't change the color and keeps the first set color (first called when OrderStatus::Enum stat == Sent(1) )
    The button stays blue, but it should be green.

    Thanks for your help : )

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums

      Properties need some extra code when used with stylesheets:
      Please see
      https://wiki.qt.io/Dynamic_Properties_and_Stylesheets

      Try the last part
      myLineEdit->setProperty("urgent", true);
      myLineEdit->style()->unpolish(myLineEdit);
      myLineEdit->style()->polish(myLineEdit);

      m_ui.btnStatus as myLineEdit. ofc :)

      B 1 Reply Last reply
      4
      • mrjjM mrjj

        Hi and welcome to the forums

        Properties need some extra code when used with stylesheets:
        Please see
        https://wiki.qt.io/Dynamic_Properties_and_Stylesheets

        Try the last part
        myLineEdit->setProperty("urgent", true);
        myLineEdit->style()->unpolish(myLineEdit);
        myLineEdit->style()->polish(myLineEdit);

        m_ui.btnStatus as myLineEdit. ofc :)

        B Offline
        B Offline
        baechlju
        wrote on last edited by
        #3

        @mrjj Thank you very much. I've tried to solve this problem for hours, but the solution was so easy.

        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