QSS qproperty-sizePolicy??
-
Hi,
I am trying to move some of my skin styling to QStylesheets. I'm having trouble specifically with some of the Q_PROPERTY items.
For instance, if I make a .ui using designer with a vertical layout and add two Push Buttons. I right click one of the buttons and edit the style sheet. If I put in:
@qproperty-sizePolicy: QSizePolicy(Policy::Expanding, Policy::Expanding);@
nothing changes. I've tried a million variations without success. I went hunting through the source code to learn more about Q_PROPERTY and QSizePolicy. In qwidget.h there is the line:
@Q_PROPERTY(QSizePolicy sizePolicy READ sizePolicy WRITE setSizePolicy)@
It seems like it fits the bill for a valid qproperty item in a qss. What do i need to pass to qproperty-sizePolicy to get it working?
@qproperty-flat : true@
works fine for example.
Thanks!
-
you set a Q_PROPERTY via css when it is DESIGNABLE (which it is by default, if it is not defined otherwise):
@
Q_PROPERTY(QSizePolicy sizePolicy READ sizePolicy WRITE setSizePolicy DESIGNABLE false) //not designable
@
So the sizePolicy is designable via stylesheets.But you are mixing css and C++, thus the style wont get applied.
And since the QSizePolicy needs 2 parameters i don't think that you can set it via stylesheet directly.Some types in Qt are able to set, for example a QSize property can set via stylesheet like this:
@
qproperty-mysize: 100 200;
@
But you can overcome this problem by subclassing the widget and define a custom property (int) for each QSizePolicy parameters of the widget.@
//allowed values see decimal representations of QSizePolicy::PolicyFlag
qproperty-verticalpolicy: ...;
qproperty-horizontalalpolicy: ...;
@ -
Ah I see, thanks for the info. I was able to specify a few properties like the following, in case it helps anyone else:
@
#include <QtGui/QPushButton>
#include <QtGui/QSizePolicy>class FlexiStretchyButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(QSizePolicy::Policy vertSizePolicy READ verticalPolicy WRITE setVerticalPolicy)
Q_PROPERTY(QSizePolicy::Policy horizSizePolicy READ horizontalPolicy WRITE setHorizontalPolicy)
Q_PROPERTY(bool resizeFont READ resizeFont WRITE setResizeFont)public:
FlexiStretchyButton(QWidget* parent = 0);
FlexiStretchyButton(const QString& text, QWidget* parent = 0 );
FlexiStretchyButton(const QIcon& icon, const QString& text, QWidget* parent = 0 );
~FlexiStretchyButton();void setResizeFont(bool resize) { bResizeFont = resize; };
bool resizeFont() const { return bResizeFont; };protected: // Overloaded operations
void resizeEvent(QResizeEvent *event);
void setVerticalPolicy(QSizePolicy::Policy policy)
{
QSizePolicy mPolicy = sizePolicy();
setSizePolicy(mPolicy.horizontalPolicy(), policy);
};
void setHorizontalPolicy(QSizePolicy::Policy policy)
{
QSizePolicy mPolicy = sizePolicy();
setSizePolicy(policy, mPolicy.verticalPolicy());
};
QSizePolicy::Policy verticalPolicy() { return sizePolicy().verticalPolicy(); };
QSizePolicy::Policy horizontalPolicy() { return sizePolicy().horizontalPolicy(); };private:
bool bResizeFont;
};
@
and modify in a .qss like so:
@
FlexiStretchyButton
{
qproperty-vertSizePolicy: Expanding;
qproperty-resizeFont: true;
}@
-
Running into another problem: In Qt's QLayout code it says:
@
Q_PROPERTY(int margin READ margin WRITE setMargin)
Q_PROPERTY(int spacing READ spacing WRITE setSpacing)
@However this QSS doesn't work:
@
QLayout
{
qproperty-margin: 0;
qproperty-spacing: 0;
}
@I have tried subclassing QGridLayout and QBoxLayout and adding both of those Q_PROPERTY's. If I call the methods those properties use it works fine, however I would really like to do this in QSS.
Is there a way of achieving this?
Thanks!
-
only QWidgets get polished by stylesheets. So you can't apply it to other classes which don't derive from QWidget.
You need to define those properties on a widget class and forward the value to it's layout.