I have an issue with text elide in Qt being too aggressive in a table, see picture.
The cell with the full figure 0.8888.... I've edited since the QTableWidget was shown, the others are are as the were when the QTableWidget was created. It seems like the elide is calculated on a lesser width that is actually the case. How can I prevent this and get proper text elide immediately when the widget becomes visible?
Simplifide code to reproduce:
QTableWidget *tableWidget;
tableWidget = new QTableWidget(Dialog);
tableWidget->setGeometry(QRect(40, 20, 256, 192));
tableWidget->setRowCount(4);
tableWidget->setColumnCount(1);
tableWidget->setItem(0, 0, new QTableWidgetItem("0.234823489789234"));
and result
Note that I would like to avoid doing the elide myself since the user can edit the value. Doing the elide myself would then mean putting back the real value when editing starts, perhaps via delegates. Would be complicated.
When everything just worked...
...the solution could have been as simple as disabling the elide in the
QTableWidget
and set directly an elided string as item's text like that:Unfortunately,
QTableWidget
(or maybe it isQStyledItemDelegate
) does not respect theQAbstractItemView::textElideMode
property.Thus, you need to do it the hard way, i.e. using delegates. Do not worry though, it is not much more complicated.
Solution
Subclass
QStyledItemDelegate
and reimplement itspaint
method like this:Feel free to change the padding to a suitable value.
Set the
tableWidget
's delegate for the desired column like this:where
Delegate
is yourQStyledItemDelegate
subclass.Result
The result of this modification is a column with a properly elided text.