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. Changing font color to font declared in Stylesheet on drawText in a paintEvent of a QTreeView Class
Forum Updated to NodeBB v4.3 + New Features

Changing font color to font declared in Stylesheet on drawText in a paintEvent of a QTreeView Class

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 407 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.
  • F Offline
    F Offline
    fontcolorhelp
    wrote on last edited by
    #1

    I inherited this code and would prefer not to rewrite everything that is why I am asking this question.

    I am trying to make the font match the global Stylesheet font I have declared for all text in a QTreeView.

    QTreeView
    {
        background-color: white;
        outline: none;
        color: red;
        font-size: 9pt;
        font-family: $FONT_FAMILY;
        show-decoration-selected: 0;
        selection-color: $SELECTION_TXT_COLOR;
        icon-size:16px;
    }
    

    FlatTreeView.h

    class ppFlatTreeView : public QtUIBase::AQTreeView
    {
        Q_OBJECT
    
    // If you need more information please ask some of it is sensitive
    }
    

    FlatTreeView.cpp

    void FlatTreeView::paintEvent(QPaintEvent* e)
    {
        if (!e || !this->Root || !this->HeaderView || !this->Model)
        {
            return;
        }
        QStyleOptionViewItem options;
        options.init(this);
        QPainter painter(this->viewport());
        style()->drawPrimitive(QStyle::PE_Widget, &options, &painter, this);
        if (!painter.isActive())
        {
            return;
        }
    
        // Translate the painter and paint area to contents coordinates.
        int px = this->horizontalOffset();
        int py = this->verticalOffset();
        painter.translate(QPoint(-px, -py));
        QRect area = e->rect();
        area.translate(QPoint(px, py));
    
        // Setup the view options.
        int i = 0;
        //
        painter.setFont(options.font);
        QColor branchColor(Qt::darkGray);
        QColor expandColor(Qt::black);
        FlatTreeViewItem* item = this->getNextVisibleItem(this->Root);
    
    
    //a lot of different code that I am omitting
    
    
        // Adjust the remaining column width for the text margin.
        columnWidth -= this->DoubleTextMargin;
        px += this->TextMargin;
    
       // Draw in the display data. THIS IS WHERE THE TEXT IS DRAWN
       this->drawData(painter, px, py, index, options, itemHeight, item->Cells[i].Width, columnWidth,
       item->RowSelected || this->Root->Cells[i].Selected || item->Cells[i].Selected);
    
    void FlatTreeView::drawData(QPainter& painter, int px, int py, const QModelIndex& index,
      const QStyleOptionViewItem& options, int itemHeight, int itemWidth, int columnWidth,
      bool selected)
    {
      QVariant indexData = this->Model->data(index);
      QString text = indexData.toString();
      int fontHeight = options.fontMetrics.height();
      int fontAscent = options.fontMetrics.ascent();
      painter.drawText(px, py + fontAscent, text);
      painter.restore();
    }
    

    I am hoping that the text would be styled with the Stylesheet but that isn't the case.

    Anyone have suggestions?

    MasterBLBM 1 Reply Last reply
    0
    • F fontcolorhelp

      I inherited this code and would prefer not to rewrite everything that is why I am asking this question.

      I am trying to make the font match the global Stylesheet font I have declared for all text in a QTreeView.

      QTreeView
      {
          background-color: white;
          outline: none;
          color: red;
          font-size: 9pt;
          font-family: $FONT_FAMILY;
          show-decoration-selected: 0;
          selection-color: $SELECTION_TXT_COLOR;
          icon-size:16px;
      }
      

      FlatTreeView.h

      class ppFlatTreeView : public QtUIBase::AQTreeView
      {
          Q_OBJECT
      
      // If you need more information please ask some of it is sensitive
      }
      

      FlatTreeView.cpp

      void FlatTreeView::paintEvent(QPaintEvent* e)
      {
          if (!e || !this->Root || !this->HeaderView || !this->Model)
          {
              return;
          }
          QStyleOptionViewItem options;
          options.init(this);
          QPainter painter(this->viewport());
          style()->drawPrimitive(QStyle::PE_Widget, &options, &painter, this);
          if (!painter.isActive())
          {
              return;
          }
      
          // Translate the painter and paint area to contents coordinates.
          int px = this->horizontalOffset();
          int py = this->verticalOffset();
          painter.translate(QPoint(-px, -py));
          QRect area = e->rect();
          area.translate(QPoint(px, py));
      
          // Setup the view options.
          int i = 0;
          //
          painter.setFont(options.font);
          QColor branchColor(Qt::darkGray);
          QColor expandColor(Qt::black);
          FlatTreeViewItem* item = this->getNextVisibleItem(this->Root);
      
      
      //a lot of different code that I am omitting
      
      
          // Adjust the remaining column width for the text margin.
          columnWidth -= this->DoubleTextMargin;
          px += this->TextMargin;
      
         // Draw in the display data. THIS IS WHERE THE TEXT IS DRAWN
         this->drawData(painter, px, py, index, options, itemHeight, item->Cells[i].Width, columnWidth,
         item->RowSelected || this->Root->Cells[i].Selected || item->Cells[i].Selected);
      
      void FlatTreeView::drawData(QPainter& painter, int px, int py, const QModelIndex& index,
        const QStyleOptionViewItem& options, int itemHeight, int itemWidth, int columnWidth,
        bool selected)
      {
        QVariant indexData = this->Model->data(index);
        QString text = indexData.toString();
        int fontHeight = options.fontMetrics.height();
        int fontAscent = options.fontMetrics.ascent();
        painter.drawText(px, py + fontAscent, text);
        painter.restore();
      }
      

      I am hoping that the text would be styled with the Stylesheet but that isn't the case.

      Anyone have suggestions?

      MasterBLBM Offline
      MasterBLBM Offline
      MasterBLB
      wrote on last edited by MasterBLB
      #2

      @fontcolorhelp
      Look at these examples mate - https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qtreeview
      For my eye, you need to specify style sheet for QTreeView::item { color : red; or text-color : red; you get the idea }
      Also, it should not be needed to reimplement paintEvent(), unless you want to do something more than drawing items in red.

      F 1 Reply Last reply
      0
      • MasterBLBM MasterBLB

        @fontcolorhelp
        Look at these examples mate - https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-qtreeview
        For my eye, you need to specify style sheet for QTreeView::item { color : red; or text-color : red; you get the idea }
        Also, it should not be needed to reimplement paintEvent(), unless you want to do something more than drawing items in red.

        F Offline
        F Offline
        fontcolorhelp
        wrote on last edited by
        #3

        @MasterBLB paintEvent also draws an pixmap icon and also a branch icon thats why we have it currently. We are thinking of removing it but it may be difficult.

        F 1 Reply Last reply
        0
        • JonBJ JonB referenced this topic on
        • F fontcolorhelp

          @MasterBLB paintEvent also draws an pixmap icon and also a branch icon thats why we have it currently. We are thinking of removing it but it may be difficult.

          F Offline
          F Offline
          fontcolorhelp
          wrote on last edited by
          #4

          @MasterBLB That solution didn't work. I need to somehow set the pen for the drawText to be the color in the StyleSheet. Can't find any great information on that anywhere.

          JonBJ 1 Reply Last reply
          0
          • F fontcolorhelp

            @MasterBLB That solution didn't work. I need to somehow set the pen for the drawText to be the color in the StyleSheet. Can't find any great information on that anywhere.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @fontcolorhelp
            If you mean: can you access the color from your code that Qt is using to implement a stylesheet rule then you cannot. You would have to parse the stylesheet to determine that information.

            F 1 Reply Last reply
            0
            • JonBJ JonB

              @fontcolorhelp
              If you mean: can you access the color from your code that Qt is using to implement a stylesheet rule then you cannot. You would have to parse the stylesheet to determine that information.

              F Offline
              F Offline
              fontcolorhelp
              wrote on last edited by
              #6

              @JonB Looks like I'm rewriting the whole class

              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