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. Expose custom parameters to QSS
Forum Updated to NodeBB v4.3 + New Features

Expose custom parameters to QSS

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsscustomqwidgetqtreeview
8 Posts 3 Posters 2.2k 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.
  • B Offline
    B Offline
    b2soft
    wrote on 6 Dec 2018, 22:08 last edited by b2soft 12 Jun 2018, 22:59
    #1

    Hi. I have custom widget, like CustomWidget : public QWidget

    I have QTreeView inside in this CustomWidget (as a part of my custom widget), QTreeView's item delegate invokes data() method, where I return one of 2 colors: black for regular text, and red color, if there is an error.

    Question is, how can I expose such colors to QSS in some way?
    like CustomWidget::default { color: black; } and CustomWidget::error { color: red; }?

    ...
    else if (role == Qt::TextColorRole)
    {
    	QColor color(Qt::red); //this will produce red text in my widget's tree items
    	if (item->GetParent()->GetType() == TreeItem::k_typeGroup)
    		color = Qt::blue;
    
    	bool visible = IsVisible();
            QColor c = visible ? Qt::gray : Qt::lightGray;
    
    	return color;
    }
    

    Thanks in advance

    R 1 Reply Last reply 7 Dec 2018, 06:56
    0
    • B b2soft
      6 Dec 2018, 22:08

      Hi. I have custom widget, like CustomWidget : public QWidget

      I have QTreeView inside in this CustomWidget (as a part of my custom widget), QTreeView's item delegate invokes data() method, where I return one of 2 colors: black for regular text, and red color, if there is an error.

      Question is, how can I expose such colors to QSS in some way?
      like CustomWidget::default { color: black; } and CustomWidget::error { color: red; }?

      ...
      else if (role == Qt::TextColorRole)
      {
      	QColor color(Qt::red); //this will produce red text in my widget's tree items
      	if (item->GetParent()->GetType() == TreeItem::k_typeGroup)
      		color = Qt::blue;
      
      	bool visible = IsVisible();
              QColor c = visible ? Qt::gray : Qt::lightGray;
      
      	return color;
      }
      

      Thanks in advance

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 7 Dec 2018, 06:56 last edited by
      #2

      @b2soft
      create 2 QColor properties for your custom widget. Then in your model retun those properties and use this QSS:

      CustomWidget {
           qproperty-default: "#FFFFFF";
          qproperty-error: "red";
      }
      

      --- 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
      • B Offline
        B Offline
        b2soft
        wrote on 7 Dec 2018, 12:53 last edited by b2soft 12 Jul 2018, 15:12
        #3

        @raven-worx ,
        I create Q_PROPERTY for such colors in this way:

        class BrowserModel : public QAbstractItemModel
        {
        Q_OBJECT
        
        public:
        Q_PROPERTY( QColor textColor MEMBER m_textColor );
        QColor m_textColor{ Qt::black };
        ...
        }
        

        Note, that BrowserModel is a member of BrowserWnd QDockWidget, but such strings have no effect:

        BrowserModel
        {
        	qproperty-textColor: #ff0000;
        }
        
        R 2 Replies Last reply 7 Dec 2018, 13:08
        0
        • B b2soft
          7 Dec 2018, 12:53

          @raven-worx ,
          I create Q_PROPERTY for such colors in this way:

          class BrowserModel : public QAbstractItemModel
          {
          Q_OBJECT
          
          public:
          Q_PROPERTY( QColor textColor MEMBER m_textColor );
          QColor m_textColor{ Qt::black };
          ...
          }
          

          Note, that BrowserModel is a member of BrowserWnd QDockWidget, but such strings have no effect:

          BrowserModel
          {
          	qproperty-textColor: #ff0000;
          }
          
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 7 Dec 2018, 13:08 last edited by raven-worx 12 Jul 2018, 13:09
          #4

          @b2soft
          as in my example: pass the value as string or rgb(255, 0, 0)

          --- 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
          • B Offline
            B Offline
            b2soft
            wrote on 7 Dec 2018, 13:40 last edited by b2soft 12 Jul 2018, 13:41
            #5

            @raven-worx
            Did not help. I have no idea why. Assume ,that code is kinda similar to this question's code: https://stackoverflow.com/questions/30170922/how-can-i-get-the-background-color-returned-by-model-take-precedence-over-the-st

            I have a custom widget. That contains QTreeView and for this QTreeView BrowserModel is set.
            I want to customize BrowserModel ::data() returning colors using QSS

            1 Reply Last reply
            0
            • B b2soft
              7 Dec 2018, 12:53

              @raven-worx ,
              I create Q_PROPERTY for such colors in this way:

              class BrowserModel : public QAbstractItemModel
              {
              Q_OBJECT
              
              public:
              Q_PROPERTY( QColor textColor MEMBER m_textColor );
              QColor m_textColor{ Qt::black };
              ...
              }
              

              Note, that BrowserModel is a member of BrowserWnd QDockWidget, but such strings have no effect:

              BrowserModel
              {
              	qproperty-textColor: #ff0000;
              }
              
              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 7 Dec 2018, 13:43 last edited by raven-worx 12 Jul 2018, 13:43
              #6

              @b2soft said in Expose custom parameters to QSS:
              in order to make something like this to work:

              BrowserModel
              {
              qproperty-textColor: #ff0000;
              }

              you need to specify Q_OBJECT in your custom class. At least in the code snippet you've posted this is not the case.

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

              B 1 Reply Last reply 7 Dec 2018, 15:12
              0
              • R raven-worx
                7 Dec 2018, 13:43

                @b2soft said in Expose custom parameters to QSS:
                in order to make something like this to work:

                BrowserModel
                {
                qproperty-textColor: #ff0000;
                }

                you need to specify Q_OBJECT in your custom class. At least in the code snippet you've posted this is not the case.

                B Offline
                B Offline
                b2soft
                wrote on 7 Dec 2018, 15:12 last edited by
                #7

                @raven-worx edited, sorry (actual class is pretty big, missed the Q_OBJECT in the topic)

                J 1 Reply Last reply 7 Dec 2018, 20:36
                0
                • B b2soft
                  7 Dec 2018, 15:12

                  @raven-worx edited, sorry (actual class is pretty big, missed the Q_OBJECT in the topic)

                  J Offline
                  J Offline
                  JonB
                  wrote on 7 Dec 2018, 20:36 last edited by
                  #8

                  @b2soft

                  I want to customize BrowserModel ::data() returning colors using QSS

                  @raven-worx knows much more than I, but I don't get this. role == Qt::TextColorRole will want to return a QBrush (http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum). How is that linked to returning a string to use for a name in a stylesheet? :confused:

                  1 Reply Last reply
                  0

                  2/8

                  7 Dec 2018, 06:56

                  topic:navigator.unread, 6
                  • Login

                  • Login or register to search.
                  2 out of 8
                  • First post
                    2/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved