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. Accessing Q_PROPERTY property from stylesheet seems not work...

Accessing Q_PROPERTY property from stylesheet seems not work...

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

    Hi everyone,
    i m trying to assign a css style to a QPushButton, everything goes fine when i define property with setProperty method, for example:

    QPushButton * myPB = new QPushButton;
    myPB->setProperty("propState","unset");
    

    then in css i have:

    QPushButton[propState="unset"] {
        border-radius: 20px;
        background: qradialgradient(
            cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
            radius: 1.1, stop: 0 #d9d9d9, stop: 1 #949494
            );
    

    this works perfectly. Then i wanted to use Q_PROPERTY, so i subclassed QPushButton, and added a property this way:

    /* header.h */
    class customPushButton : public QPushButton
    {
      Q_OBJECT
      Q_ENUMS(PBSTATE)
      Q_PROPERTY(PBSTATE propState READ getPState WRITE setPState NOTIFY PStateChanged)
    
    public:
      explicit customPushButton(QWidget *parent);
      ~customPushButton();
      enum PBSTATE {
        passed,
        failed,
        unset
      };
    
      void setPState(PBSTATE propState) {
        state = propState;
        emit PStateChanged(propState);
      }
      PBSTATE getPState() const {return state;}
    
    signals:
      void PStateChanged(PBSTATE);
    
    private:
      PBSTATE state;
    };
    

    then in file custom.cpp:

    customPushButton::customPushButton(QWidget * parent) {
    
     this->setParent(parent);
     ...
     this->setPState(PBSTATE::unset);
    }
    

    then i declared an instance of customPushButton in my main class, and after i applyedand stylesheet. In css i have:

    QPushButton[propState="unset"] {
        border-radius: 20px;
        background: qradialgradient(
            cx: 0.3, cy: -0.4, fx: 0.3, fy: -0.4,
            radius: 1.1, stop: 0 #d9d9d9, stop: 1 #949494
            );
    

    this won t work, seems css has difficuty to recognize "propState" as property... someone can tell me if i m doing things correctly?

    Thanks
    Emanuele Peruzzi

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

      Your property is an enum, not a string, the value of QVariant::toString() would never be "unset". You should only be able to use number in that case, like

      QPushButton[propState="2"]
      
      E 1 Reply Last reply
      2
      • B Bonnie

        Your property is an enum, not a string, the value of QVariant::toString() would never be "unset". You should only be able to use number in that case, like

        QPushButton[propState="2"]
        
        E Offline
        E Offline
        emazed
        wrote on last edited by
        #3

        @Bonnie Thanks, works perfectly.

        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