我正在学习QT,必须设计一个这样的表格
我需要“m2”,“2”作为上标。
这是我的代码:
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));
我用“\u00B2”代表“2”作为上标,但它不起作用,背景颜色也没有改变。请帮助我,非常感谢!
最佳答案
尝试QString("Area \n [m%1]").arg(QChar(0x00B2))
或QString("Area \n [%1]").arg(QChar(0x33A1))
。它应该适用于任何源编码。
如果不起作用,可能您的字体不支持此符号的显示。如果没有其他方法,您可以尝试通过 QLabel 使用 HTML 来模仿标题,如下所示:"<B> Area <BR> [m<SUP>2</SUP>] </B>"
。请记住,将 QWidgets 设置为 QTableWidget 通常很丑陋并且可能很慢。而且你会得到糟糕的架构。
关于qt - QTableWidget 标题的上标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966594/