superscript for QTableWidget header

2019-06-02 04:55发布

I'm learning QT and have to design a table like this

example

I need "m2" with "2" as superscript.

Here is my code:

ui.tableWidget->horizontalHeaderItem(0)->setText("Date");
ui.tableWidget->horizontalHeaderItem(0)->setBackgroundColor(QColor(217, 217, 217));
ui.tableWidget->horizontalHeaderItem(1)->setText("House address");
ui.tableWidget->horizontalHeaderItem(1)->setBackgroundColor(QColor(217, 217, 217));
ui.tableWidget->horizontalHeaderItem(2)->setText("Area \n [m\u00B2]");
ui.tableWidget->horizontalHeaderItem(2)->setBackgroundColor(QColor(217, 217, 217));
ui.tableWidget->horizontalHeaderItem(3)->setText("Price \n [USD]");
ui.tableWidget->horizontalHeaderItem(3)->setBackgroundColor(QColor(217, 217, 217));
ui.tableWidget->horizontalHeaderItem(4)->setText("Price/Area \n [USD/m\u00B2]");
ui.tableWidget->horizontalHeaderItem(4)->setBackgroundColor(QColor(217, 217, 217));

I used "\u00B2" for "2" as superscript but it doesn't work, the background color also does not change. Please help me, many thanks !

3条回答
Luminary・发光体
2楼-- · 2019-06-02 05:20

after searching I found a solution for this, I am not good enough to know why it works but it works fine for me

const char s[] = {
    0x6D,               // m
    0xC2, 0xB2,         // superscript two
    0x00                // NUL terminator
};
QString str = QString::fromUtf8(s);

and then

ui.tableWidget->horizontalHeaderItem(2)->setText("Area \n [m" + str + "]");
查看更多
Anthone
3楼-- · 2019-06-02 05:22

Try QString("Area \n [m%1]").arg(QChar(0x00B2)) or QString("Area \n [%1]").arg(QChar(0x33A1)). It should work with any source encoding.

If it doesn't work, maybe your font doesn't support this symbols to display. If there is no other way you may try to imitate headers by QLabel with HTML like this: "<B> Area <BR> [m<SUP>2</SUP>] </B>". Remember that setting QWidgets to QTableWidget is usually ugly and possibly slow. And you will have bad architecture.

查看更多
太酷不给撩
4楼-- · 2019-06-02 05:32

Try :

ui.tableWidget->horizontalHeaderItem(2)->setText(QString::fromUtf8("Area \n [m\u00B2]"));
查看更多
登录 后发表回答