Changing font color to font declared in Stylesheet on drawText in a paintEvent of a QTreeView Class
-
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?
-
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?
@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. -
@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.@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.
-
J JonB referenced this topic on
-
@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.
@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.
-
@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.
@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. -
@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.@JonB Looks like I'm rewriting the whole class