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

35 Views Asked by At

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?

0

There are 0 best solutions below