Different Font Color and Style, QStandardItem
-
-
@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);
-
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.
-
@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"))); } }
-
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.
-
@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(); }
-
@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.