QTreeWidget: change font size
-
wrote on 30 Jul 2020, 17:04 last edited by
Hello!
Is it possible to change the font size while using QStyledItemDelegate?
In 'paint" section trying to do next:QStyleOptionViewItemV4 viewOption = option; initStyleOption(&viewOption, index); viewOption.font.setPixelSize(8);
But it is still not working...
-
Hi,
Please show the complete code of your custom item delegated.
Note that if it's just a question of font, you can set the Qt::FontRole of you model items.
-
Hi,
Please show the complete code of your custom item delegated.
Note that if it's just a question of font, you can set the Qt::FontRole of you model items.
wrote on 30 Jul 2020, 19:43 last edited by sitesvvoid TfDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 viewOption = option; initStyleOption(&viewOption, index); viewOption.font.setPixelSize(8); painter->save(); QBrush brush = QBrush(Qt::red); QRect rect = viewOption.rect; //make some drawing painter->fillRect(rect, brush); //draw text and frame QPen pen; pen.setColor(Qt::black); pen.setWidth(2); painter->setPen(pen); painter->drawText(rect, Qt::AlignCenter, "Cell"); painter->drawRect(rect); }
@SGaist said in QTreeWidget: change font size:
Note that if it's just a question of font, you can set the Qt::FontRole of you model items.
Could you please explain this?
Something like this?:QFont fff; fff.setPixelSize(8); index.model()->data(index, Qt::FontRole).setValue(fff);
-
wrote on 30 Jul 2020, 20:32 last edited by eyllanesc
Two options:
- override initStyleOption and don't modify the paint method
void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){ QStyledItemDelegate::initStyleOption(option, index); option->font.setPixelSize(8); }
Or
- Use the model
QFont fff; fff.setPixelSize(8); index.model()->setData(index, fff, Qt::FontRole);
-
Two options:
- override initStyleOption and don't modify the paint method
void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){ QStyledItemDelegate::initStyleOption(option, index); option->font.setPixelSize(8); }
Or
- Use the model
QFont fff; fff.setPixelSize(8); index.model()->setData(index, fff, Qt::FontRole);
wrote on 30 Jul 2020, 20:40 last edited by@eyllanesc said in QTreeWidget: change font size:
index.model()->setData(index, fff, Qt::FontRole);
Hi!
Trying to do this in 'paint' event of my delegate.
There is an error:form_board_delegate.cpp:40:5: error: 'this' argument to member function 'setData' has type 'const QAbstractItemModel', but function is not marked const qabstractitemmodel.h:192:30: note: 'setData' declared here
I'm supposed that this is valid only for QTreView, not for QTreeWidget. Right?
-
@eyllanesc said in QTreeWidget: change font size:
index.model()->setData(index, fff, Qt::FontRole);
Hi!
Trying to do this in 'paint' event of my delegate.
There is an error:form_board_delegate.cpp:40:5: error: 'this' argument to member function 'setData' has type 'const QAbstractItemModel', but function is not marked const qabstractitemmodel.h:192:30: note: 'setData' declared here
I'm supposed that this is valid only for QTreView, not for QTreeWidget. Right?
@sitesv
Hi
It also works with *Widget version.
Just set on the item and not model.auto item = new QTreeWidgetItem(ui->treeWidget); QFont font; font.setPixelSize(8); item->setData(0,Qt::FontRole,font); item->setText(0,"MUHA"); ui->treeWidget->addTopLevelItem(item);
-
Two options:
- override initStyleOption and don't modify the paint method
void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){ QStyledItemDelegate::initStyleOption(option, index); option->font.setPixelSize(8); }
Or
- Use the model
QFont fff; fff.setPixelSize(8); index.model()->setData(index, fff, Qt::FontRole);
wrote on 30 Jul 2020, 21:07 last edited by sitesv@eyllanesc said in QTreeWidget: change font size:
> Two options: > > 1. override initStyleOption and don't modify the paint method > > void TfDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index){ > QStyledItemDelegate::initStyleOption(option, index); > option->font.setPixelSize(8); > }
Not working for me,
@mrjj said in QTreeWidget: change font size:
auto item = new QTreeWidgetItem(ui->treeWidget);
QFont font;
font.setPixelSize(8);
item->setData(0,Qt::FontRole,font);
item->setText(0,"MUHA");
ui->treeWidget->addTopLevelItem(item);Sorry. My case is QTableWidget, not a QTreeWidget.
newItem = new QTableWidgetItem(""); newItem->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter); QFont font; font.setPixelSize(8); newItem->setText("TEXT"); newItem->setData(Qt::FontRole, font);
This is also not working in my case.
Maybe something with 'paint' method?
I have only one font size for all items... :(
-
wrote on 30 Jul 2020, 21:27 last edited by eyllanesc
-
You cannot and should not execute the setData method inside the paint.
-
have you modified the delegate's paint method? If so then you must set the font directly to the QPainter:
painter->setFont(fff)
.
My previous options assume that you are not doing a custom painting (by default the painting is done by QStyle using the attributes of "option").
-
-
-
You cannot and should not execute the setData method inside the paint.
-
have you modified the delegate's paint method? If so then you must set the font directly to the QPainter:
painter->setFont(fff)
.
My previous options assume that you are not doing a custom painting (by default the painting is done by QStyle using the attributes of "option").
wrote on 30 Jul 2020, 21:31 last edited by@eyllanesc Thank you! It works!
-
1/9