[英]How to prevent too aggressive text elide in QTableview?
I have an issue with text elide in Qt being too aggressive in a table, see picture.我有一个问题,Qt 中的文本省略在表格中过于激进,请参见图片。
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.完整数字 0.8888 的单元格......自从 QTableWidget 显示以来我已经编辑过,其他的与创建 QTableWidget 时一样。 It seems like the elide is calculated on a lesser width that is actually the case.
似乎elide是在实际情况下以较小的宽度计算的。 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.
会很复杂。
...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: ...解决方案可能就像在
QTableWidget
禁用 elide 一样简单,并直接将一个被省略的字符串设置为项目的文本,如下所示:
tableWidget->setTextElideMode(Qt::ElideNone);
tableWidget->setItem(0, 0, new QTableWidgetItem(tableWidget->fontMetrics().elidedText("0.234823489789234", Qt::ElideRight, tableWidget->columnWidth(0))));
Unfortunately, QTableWidget
(or maybe it is QStyledItemDelegate
) does not respect the QAbstractItemView::textElideMode
property.不幸的是,
QTableWidget
(或者可能是QStyledItemDelegate
)不尊重QAbstractItemView::textElideMode
属性。
Thus, you need to do it the hard way, ie using delegates.因此,您需要以艰难的方式来完成它,即使用委托。 Do not worry though, it is not much more complicated.
不过不要担心,它并不复杂。
Subclass QStyledItemDelegate
and reimplement its paint
method like this:子类
QStyledItemDelegate
并重新实现其paint
方法,如下所示:
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) return; QStyleOptionViewItem opt = option; initStyleOption(&opt, index); int padding = 3; painter->save(); painter->setClipRect(opt.rect); opt.rect = opt.rect.adjusted(padding, padding, -padding, -padding); painter->drawText(opt.rect, Qt::AlignLeft | Qt::AlignVCenter, opt.fontMetrics.elidedText(opt.text, Qt::ElideRight, opt.rect.width())); painter->restore(); opt.rect = option.rect; opt.textElideMode = Qt::ElideNone; opt.text = ""; QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter); }
Feel free to change the padding to a suitable value.随意将填充更改为合适的值。
Set the tableWidget
's delegate for the desired column like this:为所需的列设置
tableWidget
的委托,如下所示:
tableWidget->setItemDelegateForColumn(0, new Delegate(this));
where Delegate
is your QStyledItemDelegate
subclass.其中
Delegate
是您的QStyledItemDelegate
子类。
The result of this modification is a column with a properly elided text.此修改的结果是一列带有正确省略的文本。
I think the solution is just:我认为解决方案只是:
tableWidget->setWordWrap(false);
If you increase the row height, you'll probably find that the text (well, number) has been "word-wrapped" at the decimal point.如果增加行高,您可能会发现文本(嗯,数字)在小数点处已“自动换行”。
I figured this out while finding the answers to this question .我在寻找这个问题的答案时发现了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.