如何设置标题标题以包裹在 QTableWidget 中 [英] How to set header titles to wrap in QTableWidget
问题描述
我正在使用 Qt 在表格中显示一堆信息.我有一个 QTableWidget 对象,我想包含的一些标题标题比我想要的单元格大小要长.例如,我想要
I am using Qt to display a bunch of information in a table. I have a QTableWidget object and some of the header titles I want to include are longer than what I want the cell sizes to be. For example, I would like to have
<html> <br>Acceleration <br> (m/s^2) </html>
而不是将两者放在同一行上.我知道单元格中有自动换行功能,但我想将标题保留在标题中,以便更容易区分它们.字符串中的 HTML 似乎也不适用于标题,因此我无法使用 <br>
.有没有办法让文本换行,或者在标题中手动换行?
instead of having both on the same line. I know there is a word wrap capability in the cells, but I would like to keep the titles in the header so they can be distinguished more easily. HTML in the strings doesn't seem to work in the header either so I can't use a <br>
. Is there a way to get text to wrap, or manually break lines within the title?
推荐答案
你可以使用一个简单的 \n
来手动打破你的标题:
You could use a simple \n
to manually break your header title:
#include <QApplication>
#include <QTableWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableWidget w(2, 2);
w.setHorizontalHeaderLabels(QStringList() << "FOO\nBAR" << "FOOBAR");
w.show();
return app.exec();
}
这篇关于如何设置标题标题以包裹在 QTableWidget 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!