I'm learning QT and have to design a table like this
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 !
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.
Try :
ui.tableWidget->horizontalHeaderItem(2)->setText(QString::fromUtf8("Area \n [m\u00B2]"));
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 + "]");
来源:https://stackoverflow.com/questions/37966594/superscript-for-qtablewidget-header