Solved How to change the height and text font size of qtreeview item
-
Hi all,
I have a treeview and I want to change the height of each item (QModelIndex) and also change the font size to fit into it well.
I found a way to change the size of each treeview item (50,as i needed), but unable to figure out how to change the font size.
Below is the code :class Delegate : public QStyledItemDelegate { Q_OBJECT public: QSize sizeHint(const QStyleOptionViewItem &p_option, const QModelIndex &p_index) const //this function to compensate the alignment { QSize size = QStyledItemDelegate::sizeHint(p_option, p_index); if (p_index.column() == 1 || p_index.column() == 0) { size.setHeight(100); } return size; } }; tree->setItemDelegate(new Delegate());
Also I know I can change the modelIndex heigth from model also, but again I am not sure how to set the font size.
Could anyone please let me a way to achieve this. -
Hi,
I guess you could reimplement QStyledItemDelegate::initStyleOption()void Delegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const { QStyledItemDelegate::initStyleOptions(option, index); if (p_index.column() == 1 || p_index.column() == 0) { option->font.setPointSize(20); //or setPixelSize(...); //or setPointSizeF(...); } }
font is a public variable from QStyleOptionViewItem
-
Hi,
I guess you could reimplement QStyledItemDelegate::initStyleOption()void Delegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const { QStyledItemDelegate::initStyleOptions(option, index); if (p_index.column() == 1 || p_index.column() == 0) { option->font.setPointSize(20); //or setPixelSize(...); //or setPointSizeF(...); } }
font is a public variable from QStyleOptionViewItem
-
@Gojir4 Thank you for you reply.
It tired this but the content of the treeitem is missing now.
I tried all the options :option->font.setPointSize(50); option->font.setPixelSize(50); option->font.setPointSizeF(50);
.
-
Do you also call the base implementation of initStyleOption()? I would guess no.
-
@NoumanYosuf @Christian-Ehrlicher My bad. I did correct the code it should work better now.
-
Hey @Christian-Ehrlicher and @Gojir4 yes its works fine. thank you both for the help.