如何在 QTableWidget 的行和列中自动换行文本? [英] How to word wrap text in the rows and columns of a QTableWidget?

查看:945
本文介绍了如何在 QTableWidget 的行和列中自动换行文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

    QTableWidget *j = new QTableWidget (10000, 5, centralWidget);
    j->setColumnWidth (0, 500);
    j->setColumnWidth (1, 30);
    j->setColumnWidth (2, 30);
    j->setColumnWidth (3, 320);
    j->setColumnWidth (4, 310);

    j->setWordWrap (true);

也尝试了 resizeColumnsToContentsresizeRowsToContents,但都失败了.

Also tried resizeColumnsToContents and resizeRowsToContents, but failed.

如果文本比设置的宽度长,我想把句子分解.
目前,句子的冗长部分没有显示出来.

If the text is longer than the set width, I want the sentence to break down.
Currenty, the lengthy part of the sentence just doesn't get shown.

推荐答案

setWordWrap 定义文本的行为,而不改变列大小.如果您需要保持列宽固定,请在将项目插入单元格后调用 resizeRowsToContents (我假设您通过 QTableWidgetItem 向表格添加文本).

setWordWrap defines the behaviour of the text, without altering column size. If you need to keep column width fixed, call resizeRowsToContents after the insertion of the item to the cell (I assume you're adding text to the table via QTableWidgetItem).

请注意,如果项目中包含的任何字词比列宽,文本将从该点开始被忽略(默认情况下,您将看到省略号:...).如果你想改变这种行为,你需要重新实现项目的绘画功能或拉伸你的列.

Please notice that if any of the words contained in the item are wider than column size, text will be elided from that point on (by default you will see ellipses: ...). If you want to change such behaviour you need to reimplement item's painting function or stretch your columns.

这篇关于如何在 QTableWidget 的行和列中自动换行文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆