Results 1 to 8 of 8

Thread: Using custom Q_PROPERTY with Stylesheet

  1. #1
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Using custom Q_PROPERTY with Stylesheet

    Hi

    I have added a custom Q_PROPERTY like this:

    Q_PROPERTY( bool isTalking READ isTalking WRITE setIsTalking )

    bool _isTalking;

    public:
    MyClass(QWidget *parent );
    ~MyClass();

    bool isTalking() const {return _isTalking;}
    void setIsTalking(bool newIsTalking){_isTalking = newIsTalking;}

    Then from my style sheet file I want to change the background color of a QTreeWidgetItem based on this Q_PROPERTY like this:

    QTreeWidget#activeCalls::item:isTalking{
    background: green;
    }

    However this has no effect. Using a normal property on the QTreeWidget like this works though:
    QTreeWidget#activeCalls::item:selected{
    background: grey;
    }

    So the Widget is named correctly but I do not think the item is updated based on the value of my cusomt Q_PROPERTY. How can I make sure the Widget is updated when my property changes so the correct stylesheet is applied dynamically.

  2. #2
    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: Using custom Q_PROPERTY with Stylesheet

    Quote Originally Posted by hubbobubbo View Post
    QTreeWidget#activeCalls::item:selected{
    background: grey;
    }
    You can not access all properties like that. It's only a bunch of properties you can use that way (called pseudo states). You can try to use something like that:
    css Code:
    1. QTreeWidget#activeCalls[isTalking="true"]
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    hubbobubbo (22nd April 2010)

  4. #3
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: Using custom Q_PROPERTY with Stylesheet

    You must know, what Style Sheets added to QWidget only once and if you change QMetaProperty value later this do not change style with other rule.

  5. #4
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using custom Q_PROPERTY with Stylesheet

    Hi

    I tried like this (changed to int to make it even simpler)

    QTreeWidget#activeCalls::item[isTalking="0"]{
    background: green;
    }

    QTreeWidget#activeCalls::item[isTalking="1"]{
    background: yellow;
    }

    in the code I simple call setIsTalking(0) or setIsTalking(1)

    Still no action at all though.

  6. #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: Using custom Q_PROPERTY with Stylesheet

    SABROG gave you the answer: you have to reaply the style sheet every time you change this property. So your approach doesn't seem to be the best. Without knowing exactely what you are doing, I would recomend you to use a custom item delegate where you can paint different backgrounds depending on the items "property". Or if you add widgets, use the widgets paint methode.

  7. The following user says thank you to Lykurg for this useful post:

    hubbobubbo (22nd April 2010)

  8. #6
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using custom Q_PROPERTY with Stylesheet

    Ok thanks I will give up on that approach.

  9. #7
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: Using custom Q_PROPERTY with Stylesheet

    Quote Originally Posted by hubbobubbo View Post
    Still no action at all though.
    It's standard behaviour. Trolls don't want change this. I created bugreport about one year ago and he's been rejected.

  10. The following user says thank you to SABROG for this useful post:

    hubbobubbo (22nd April 2010)

  11. #8
    Join Date
    Sep 2010
    Location
    Norway
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using custom Q_PROPERTY with Stylesheet

    I used this approach:


    Qt Code:
    1. Q_PROPERTY( bool isTalking
    2. READ isTalking
    3. WRITE setIsTalking
    4. DESGNABLE true
    5. SCRIPTABLE true)
    6.  
    7. bool _isTalking;
    8.  
    9. public:
    10. MyClass(QWidget *parent ) :
    11. QWidget(parent)
    12. {
    13. this->setObjectName("MyClass");
    14. _watch->addPath("xxxx.css");
    15. connect(_watch, SIGNAL(fileChanged(QString),
    16. this,SLOT(loadStyleSheet(QString)));
    17.  
    18. }
    19. ~MyClass();
    20.  
    21. bool isTalking() const
    22. {return _isTalking;}
    23. void setIsTalking(bool newIsTalking)
    24. {_isTalking = newIsTalking;}
    25.  
    26. public slots:
    27. void loadStyleSheet(const QString& css)
    28. {
    29. QFile cssfile(css);
    30.  
    31. if(cssfile.open(QIODevice::ReadOnly
    32. | IODevice::Text))
    33. {
    34. QTextStream in(&cssFile);
    35. QString cssssss(in.readAll());
    36. this->setStyleSheet(csssss);
    37. }
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    In the css:

    Qt Code:
    1. MyClass{qproperty-isTalking: true;}
    To copy to clipboard, switch view to plain text mode 

    However - changing properties from css file should not be used for all purposes (at least >5 pr second). it will be slooow.

  12. The following user says thank you to mrandreas for this useful post:

    mfords (14th May 2011)

Similar Threads

  1. Usability of StyleSheet in Pure Custom Widget Plugin?
    By umituzun84 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 15:22
  2. Why and when to use Q_PROPERTY
    By Vanir in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2007, 10:25
  3. How to Use Q_PROPERTY
    By mitesh_modi in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2006, 15:49
  4. enlighten me on the use of Q_PROPERTY!!
    By nupul in forum Newbie
    Replies: 4
    Last Post: 3rd April 2006, 11:02
  5. Pointers and Q_PROPERTY
    By andersin in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 15:05

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.