turn off elide in column of qtablewidget
-
Hello,
i have a table widget derived from QTableWidget and first i tried the following:
setTextElideMode(Qt::ElideNone) in the constructor and this did not work. so i setup a delegate as:class elideNoneItemC : public QStyledItemDelegate
{
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.textElideMode=Qt::ElideNone;
QStyledItemDelegate::paint(painter, opt, index);
}
};elideNoneItemC *elidenone = new elideNoneItemC;
and then setItemDelegateForColumn(1,elidenone); for the second column but the text in the second column (if sufficiently long) still displays the ellipses.
any idea what may be wrong.
-
@sawarsi You have to override the initStyleOption method
class StyledItemDelegate: public QStyledItemDelegate { public: using QStyledItemDelegate::QStyledItemDelegate; protected: void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override { QStyledItemDelegate::initStyleOption(option, index); option->textElideMode = Qt::ElideNone; } };
-
@eyllanesc
How do we know whichQStyleOptionViewItem
things to do ininitStyleOption()
versus what @sawarsi tried to do in thepaint()
override?Ah, I see it says:
When reimplementing paint in a subclass. Use the initStyleOption() to set up the option in the same way as the QStyledItemDelegate.
but not sure I understand "in the same way as"?
Given that
paint()
takes aconst QStyleOptionViewItem &option
, are you not supposed to alter it and pass it on as he did? Is theoption
intended to be read-only at this stage? -
paint receives the option but then modifies it by invoking the initStyleOption. If you want to know how a class works then it is better to check the source code: https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/itemviews/qstyleditemdelegate.cpp?h=dev#n377
void QStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_ASSERT(index.isValid()); QStyleOptionViewItem opt = option; initStyleOption(&opt, index); // <--- update option const QWidget *widget = QStyledItemDelegatePrivate::widget(option); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); }
@sawarsi you should not modify the paint method
-
-
-
@eyllanesc I admire you a lot. This is one of the best ways for an informed person to respond to a question. Anyone asking question is usually confused and an illustrative answer is the best. I notice you always go out of your way to give a practical example. Honestly if you have a class where you teach students, I would love to be one of your students.
Thank you very much.