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. QSS qproperty-sizePolicy??
Forum Updated to NodeBB v4.3 + New Features

QSS qproperty-sizePolicy??

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 7.9k 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.
  • K Offline
    K Offline
    klox
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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: ...;
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • K Offline
        K Offline
        klox
        wrote on last edited by
        #3

        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;
        }

        @

        1 Reply Last reply
        0
        • K Offline
          K Offline
          klox
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            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