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. Different Font Color and Style, QStandardItem
QtWS25 Last Chance

Different Font Color and Style, QStandardItem

Scheduled Pinned Locked Moved Solved General and Desktop
qstylesheetqstandarditemm
11 Posts 3 Posters 2.3k 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.
  • Q qblacky
    16 Aug 2021, 14:00

    Is it possible to set different Color and Style for QStandardItem into QTreeView dependent on data from Model?
    with QStyleSheet or other Methods?

    E Offline
    E Offline
    eyllanesc
    wrote on 16 Aug 2021, 15:09 last edited by
    #2

    @qblacky what is the meaning of custom style?

    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

    1 Reply Last reply
    0
    • Q qblacky
      16 Aug 2021, 14:00

      Is it possible to set different Color and Style for QStandardItem into QTreeView dependent on data from Model?
      with QStyleSheet or other Methods?

      J Offline
      J Offline
      JoeCFD
      wrote on 16 Aug 2021, 17:10 last edited by
      #3

      @qblacky said in Different Font Color and Style, QStandardItem:

      QStandardItem

      https://forum.qt.io/topic/63715/how-to-style-qtreeview-items-by-role-with-css/9

      1 Reply Last reply
      0
      • Q qblacky
        16 Aug 2021, 14:00

        Is it possible to set different Color and Style for QStandardItem into QTreeView dependent on data from Model?
        with QStyleSheet or other Methods?

        E Offline
        E Offline
        eyllanesc
        wrote on 16 Aug 2021, 17:36 last edited by eyllanesc
        #4

        @qblacky QStandardItem is not a visual element but a container for information (texts, colors, etc.) so the question makes little sense, if you want to change the view then use a delegate:

        class Delegate: public QStyledItemDelegate{
        public: 
            using QStyledItemDelegate::QStyledItemDelegate;
        protected:
            void initStyleOption(QStyleOptionViewItem * option, const QModelIndex & index) const{
                QVariant data = index.data();
                qDebug() << data;
                QStyledItemDelegate::initStyleOption(option, index);
                if(/*some conditions*/){
                    QFont font(option->font);
                    /*modify fonts*/
                    option->font = font;
                    option->fontMetrics = QFontMetrics(font);
                }
            }
        }
        
        treview->setItemDelegate(new Delegate);
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qblacky
          wrote on 17 Aug 2021, 05:50 last edited by
          #5

          Hello,

          thanks for answer, i have same solution,
          but FontColor , Foreground or color of QPalette::WindowText
          have no effects for Item.

          void ItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const 
          {
          
              QStyleOptionViewItem localOpt(*option);
              bool ok = false;
          // value from item 
              double data = index.data(Qt::DisplayRole).toDouble(&ok);
          // data range from item 
              ItemDisclosure *id = new ItemDisclosure(index.data(QtExtUserRoles::LastRole + 1).value<ItemDisclosure>());
              if (id != nullptr)
              {
                if (ok && (id->maxValue() <= data || id->minValue() >= data))
                {
                  //option = id->itemOption();
                  if (id->itemOption() != nullptr)
                  {
          
                    option->font = id->itemOption()->font;
                    option->palette = id->itemOption()->palette;
                    option->palette.setBrush(QPalette::Text, id->itemOption()->palette.brush(QPalette::Text));
                    option->palette.setBrush(QPalette::WindowText, id->itemOption()->palette.brush(QPalette::WindowText));
                    option->palette.setColor(QPalette::Text, id->itemOption()->palette.color(QPalette::Text));
                    option->palette.setColor(QPalette::WindowText, id->itemOption()->palette.color(QPalette::WindowText));
          
                  }
                }
                
              }
              QStyledItemDelegate::initStyleOption(option, index);
            }
          

          only font style and font size are change but not color.

          Thank you.

          E 1 Reply Last reply 17 Aug 2021, 05:55
          0
          • Q qblacky
            17 Aug 2021, 05:50

            Hello,

            thanks for answer, i have same solution,
            but FontColor , Foreground or color of QPalette::WindowText
            have no effects for Item.

            void ItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const 
            {
            
                QStyleOptionViewItem localOpt(*option);
                bool ok = false;
            // value from item 
                double data = index.data(Qt::DisplayRole).toDouble(&ok);
            // data range from item 
                ItemDisclosure *id = new ItemDisclosure(index.data(QtExtUserRoles::LastRole + 1).value<ItemDisclosure>());
                if (id != nullptr)
                {
                  if (ok && (id->maxValue() <= data || id->minValue() >= data))
                  {
                    //option = id->itemOption();
                    if (id->itemOption() != nullptr)
                    {
            
                      option->font = id->itemOption()->font;
                      option->palette = id->itemOption()->palette;
                      option->palette.setBrush(QPalette::Text, id->itemOption()->palette.brush(QPalette::Text));
                      option->palette.setBrush(QPalette::WindowText, id->itemOption()->palette.brush(QPalette::WindowText));
                      option->palette.setColor(QPalette::Text, id->itemOption()->palette.color(QPalette::Text));
                      option->palette.setColor(QPalette::WindowText, id->itemOption()->palette.color(QPalette::WindowText));
            
                    }
                  }
                  
                }
                QStyledItemDelegate::initStyleOption(option, index);
              }
            

            only font style and font size are change but not color.

            Thank you.

            E Offline
            E Offline
            eyllanesc
            wrote on 17 Aug 2021, 05:55 last edited by eyllanesc
            #6

            @qblacky You have to make the changes after QStyledItemDelegate::initStyleOption(option, index);:

                void initStyleOption(QStyleOptionViewItem * option, const QModelIndex & index) const{
                    QVariant data = index.data();
                    qDebug() << data;
                    QStyledItemDelegate::initStyleOption(option, index);
                    if(/*some conditions*/){
                        QFont font(option->font);
                        /*modify fonts*/
                        option->font = font;
                        option->fontMetrics = QFontMetrics(font);
                        option->palette.setBrush(QPalette::Text, QBrush(QColor("red")));
                    }
                }
            

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qblacky
              wrote on 17 Aug 2021, 06:10 last edited by
              #7
              void ItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const 
              {
              
                  QStyleOptionViewItem localOpt(*option);
                  bool ok = false;
              // value from item 
                  double data = index.data(Qt::DisplayRole).toDouble(&ok);
              // data range from item 
                  ItemDisclosure *id = new ItemDisclosure(index.data(QtExtUserRoles::LastRole + 1).value<ItemDisclosure>());
                  if (id != nullptr)
                  {
                    QStyledItemDelegate::initStyleOption(option, index);
                    if (ok && (id->maxValue() <= data || id->minValue() >= data))
                    {
                      //option = id->itemOption();
                      if (id->itemOption() != nullptr)
                      {
                         option->font = id->itemOption()->font;          
                         option->palette = id->itemOption()->palette; 
                         option->palette.setBrush(QPalette::Text, QBrush(QColor("red")));
                         option->palette.setBrush(QPalette::WindowText, QBrush(QColor("red")));      }
                    }
                    
                  }
              
                }
              

              This has no effects on the color of the item.

              E 1 Reply Last reply 17 Aug 2021, 06:19
              0
              • Q qblacky
                17 Aug 2021, 06:10
                void ItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const 
                {
                
                    QStyleOptionViewItem localOpt(*option);
                    bool ok = false;
                // value from item 
                    double data = index.data(Qt::DisplayRole).toDouble(&ok);
                // data range from item 
                    ItemDisclosure *id = new ItemDisclosure(index.data(QtExtUserRoles::LastRole + 1).value<ItemDisclosure>());
                    if (id != nullptr)
                    {
                      QStyledItemDelegate::initStyleOption(option, index);
                      if (ok && (id->maxValue() <= data || id->minValue() >= data))
                      {
                        //option = id->itemOption();
                        if (id->itemOption() != nullptr)
                        {
                           option->font = id->itemOption()->font;          
                           option->palette = id->itemOption()->palette; 
                           option->palette.setBrush(QPalette::Text, QBrush(QColor("red")));
                           option->palette.setBrush(QPalette::WindowText, QBrush(QColor("red")));      }
                      }
                      
                    }
                
                  }
                

                This has no effects on the color of the item.

                E Offline
                E Offline
                eyllanesc
                wrote on 17 Aug 2021, 06:19 last edited by
                #8

                @qblacky I do not reproduce what you indicate:

                #include <QApplication>
                #include <QStandardItemModel>
                #include <QStyledItemDelegate>
                #include <QTreeView>
                
                class StyledItemDelegate: public QStyledItemDelegate{
                public:
                    using QStyledItemDelegate::QStyledItemDelegate;
                protected:
                    void initStyleOption(QStyleOptionViewItem * option, const QModelIndex & index) const{
                
                        QStyledItemDelegate::initStyleOption(option, index);
                        QFont font(option->font);
                        // change font
                        font.setBold(true);
                        option->font = font;
                        option->fontMetrics = QFontMetrics(font);
                        //change text color
                        option->palette.setBrush(QPalette::Text, QBrush(QColor("red")));
                    }
                
                };
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    a.setStyle("fusion");
                    QTreeView w;
                    w.setItemDelegate(new StyledItemDelegate);
                    QStandardItemModel model;
                    model.appendRow(new QStandardItem("FOO"));
                    w.setModel(&model);
                    w.show();
                    return a.exec();
                }
                

                Screenshot_20210817_011846.png

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qblacky
                  wrote on 17 Aug 2021, 06:40 last edited by
                  #9

                  We have a StyleSheet behind of QTreeView.
                  I want change only from one Item the Color.
                  Not all.

                  Without StyleSheet:
                  5621c54c-ceed-4ca1-9ed3-ffaebac18af8-image.png

                  With StyleSheet:
                  34177bc0-00ee-41d8-9803-f8212d627202-image.png

                  E 1 Reply Last reply 17 Aug 2021, 06:43
                  0
                  • Q qblacky
                    17 Aug 2021, 06:40

                    We have a StyleSheet behind of QTreeView.
                    I want change only from one Item the Color.
                    Not all.

                    Without StyleSheet:
                    5621c54c-ceed-4ca1-9ed3-ffaebac18af8-image.png

                    With StyleSheet:
                    34177bc0-00ee-41d8-9803-f8212d627202-image.png

                    E Offline
                    E Offline
                    eyllanesc
                    wrote on 17 Aug 2021, 06:43 last edited by
                    #10

                    @qblacky You cannot use both technologies: Either you use the delegate OR you use the stylesheet but not both. What happens is that when you use QtStyleSheet a private QStyle is used that interprets the stylesheet and sometimes takes some information but mostly discards the information from QStyleOption.

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qblacky
                      wrote on 17 Aug 2021, 13:26 last edited by
                      #11

                      If i'm not define the StyleSheet for item,
                      then work initStyleOption() from delegate.

                      Thank you.

                      1 Reply Last reply
                      0

                      11/11

                      17 Aug 2021, 13:26

                      • Login

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