2016-06-22 24 views
0

我正在學習QT,並要設計一個表像這樣爲標頭QTableWidget的

example

我需要「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」爲上標,但它不工作,背景顏色也不會變化。請幫助我,非常感謝!

回答

0

嘗試QString("Area \n [m%1]").arg(QChar(0x00B2))QString("Area \n [%1]").arg(QChar(0x33A1))。它應該與任何源編碼一起工作。

如果它不起作用,也許你的字體不支持這個符號來顯示。如果沒有其他方式,您可以嘗試使用HTML來模仿QLabel的標題,如下所示:"<B> Area <BR> [m<SUP>2</SUP>] </B>"。請記住,將QWidgets設置爲QTableWidget通常很醜,可能很慢。你會有不好的建築。

+0

謝謝,你的解決方案是偉大的:)。我還爲下面找到了另一個解決方案。順便說一句,你有任何想法與背景顏色的問題?我不知道爲什麼我的命令不起作用。 – htmlamateur

+0

當然,QChar的解決方案?不知道標題顏色。嘗試谷歌搜索。至少Qt [有一些麻煩](https://forum.qt.io/topic/40002/qtablewidget-s-header-background-color-doesn-t-work-in-win7)。 – ilotXXI

+0

今天面對桌子的景色,看着背景。 QHeaderView在Windows 10中使用默認樣式錯誤地處理背景。如果應用程序樣式更改爲Fusion或其他樣式,它將起作用。有一個有趣的解決方法:它處理QSS。所以,解決方案1:'table-> setStyle(QStyleFactory :: create(「Fusion」))';解決方案2:[應用QSS](http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qheaderview)到表格的標題視圖。 – ilotXXI

0

嘗試:

ui.tableWidget->horizontalHeaderItem(2)->setText(QString::fromUtf8("Area \n [m\u00B2]")); 
+0

謝謝你的提示。但它仍然不工作:( – htmlamateur

0

搜索後,我發現了一個解決方案,我不夠好知道爲什麼它的工作原理,但它工作正常,我

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

然後

ui.tableWidget->horizontalHeaderItem(2)->setText("Area \n [m" + str + "]");