[Qt-interest] having trouble creating custom Q_PROPERTY for use with stylesheets

Robert Douglas rwdougla at gmail.com
Thu Jul 22 16:19:40 CEST 2010


Sorry to reply to my own posting, but I wanted to share what I've since 
found.

Setting a stylesheet on the QApplication instance causes it to generate 
a new proxy style QStyleSheetStyle as the application' style. This means 
that overloaded properties will be, through the style system, dispatched 
to all QWidgets.

Thus, as far as I can tell, the current system does not allow the 
arbitrary setting of Q_PROPERTIES on direct derivatives of QObjects.


On 7/21/2010 8:45 PM, Robert Douglas wrote:
> I've been banging my head against a wall for this, and am hoping someone
> out there can help me out. Any help is appreciated.
>
> I have 2 files as such:
>
> File : MyObj.h
>
> #ifndef MY_OBJ_H
> #define MY_OBJ_H
>
> #include <QtCore/QObject>
>
> class MyObj : public QObject
> {
> Q_OBJECT
> Q_PROPERTY(int theInt READ getInt WRITE setInt DESIGNABLE true)
> public:
> int getInt() const { return x; }
> void setInt(int val)
> {
> x = val;
> }
> private:
> int x;
> };
>
> #endif
>
>
> File main.cpp:
>
> #include <QtGui/QApplication>
> #include <QtGui/QPushButton>
> #include <QtCore/QVariant>
> #include "MyObj.h"
>
> int main(int argc, char *argv[])
> {
> QApplication a(argc, argv);
> a.setStyleSheet("MyObj { qproperty-theInt: 5 }");
>
> MyObj mo;
> //mo.setProperty("theInt", 5);
>
> QPushButton pb;
> pb.show();
>
> return a.exec();
> }
>
>
> I don't get any errors in the application's output, and the debugger
> never gets to setInt(int). If I uncomment the "setProperty" line, that
> hits the setInt function just fine. Thus, I am sure the property system
> knows of the property, I just can't figure out how to get the stylesheet
> system to use it.
>
> Any thoughts?
>
> Thanks in advance,
> -Rob




More information about the Qt-interest-old mailing list