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
QtWS25 Last Chance

Expose custom parameters to QSS

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsscustomqwidgetqtreeview
8 Posts 3 Posters 2.1k Views
  • 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.
  • b2softB Offline
    b2softB Offline
    b2soft
    wrote on last edited by b2soft
    #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

    raven-worxR 1 Reply Last reply
    0
    • b2softB b2soft

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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
      • b2softB Offline
        b2softB Offline
        b2soft
        wrote on last edited by b2soft
        #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;
        }
        
        raven-worxR 2 Replies Last reply
        0
        • b2softB b2soft

          @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;
          }
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #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
          • b2softB Offline
            b2softB Offline
            b2soft
            wrote on last edited by b2soft
            #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
            • b2softB b2soft

              @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;
              }
              
              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #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

              b2softB 1 Reply Last reply
              0
              • raven-worxR raven-worx

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

                b2softB Offline
                b2softB Offline
                b2soft
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • b2softB b2soft

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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

                  • Login

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