前言
最終解決方法(通過設置表頭屬性解決)
tableView = createView(mySqlQueryModel, QObject::tr("可頌坊報表系統"));
//tableView->resizeColumnsToContents();
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
tableView->horizontalHeader()->setMinimumSectionSize(100);
//tableView->horizontalHeader()->setSectionsMovable(true);
本着愛折騰的精神,樓主又進一步思考了一些問題:是resizexxxxToContents()系列接口和表頭的setSectionResizeMode()接口的同事作用還是隻有表頭的setSectionResizeMode()接口就可以解決顯示不全問題?最後發現1.單獨調用resizexxxxToContents()系列接口解決不了;2.同時調用resizexxxxToContents()系列接口和表頭的setSectionResizeMode()接口解決了顯示不全問題;3.單獨調用表頭的setSectionResizeMode()接口就可以解決顯示不全問題。最終,樓主發現起作用的是表頭的setSectionResizeMode()接口!所以當使用QTableView的resizexxxxToContents()系列接口可以解決顯示不全的問題的時候就單獨使用這系列的接口,行不通的話就使用表頭的setSectionResizeMode()接口!附錄:表頭(QHeaderView)的setSectionResizeMode()接口枚舉參數
enum ResizeMode
{
Interactive,
Stretch,
Fixed,
ResizeToContents,
Custom = Fixed
};
枚舉常量 |
值 |
中文描述 |
英文描述 |
QHeaderView::Interactive |
0 |
The user can resize the section. The section can also be resized programmatically usingresizeSection(). The section size defaults todefaultSectionSize. (See alsocascadingSectionResizes.) |
用戶可以重新調整表頭的大小,也可以使用resizeSection()重新調整表頭的大小。 |
QHeaderView::Fixed |
2 |
The user cannot resize the section. The section can only be resized programmatically usingresizeSection(). The section size defaults todefaultSectionSize. |
用戶不可以重新調整表頭的大小,只可以使用resizeSection()重新調整表頭的大小。 |
QHeaderView::Stretch |
1 |
QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically. |
表頭將會調整單元格到可得的空間。用戶或者程序員通過代碼都不能改變它的大小。 |
QHeaderView::ResizeToContents |
3 |
QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically. (This value was introduced in 4.2) |
表頭將自動根據整個行或者列的內容去調整表頭單元格到最佳的大小。用戶或者程序員通過代碼都不能改變它的大小。 |