前言
最终解决方法(通过设置表头属性解决)
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) | 表头将自动根据整个行或者列的内容去调整表头单元格到最佳的大小。用户或者程序员通过代码都不能改变它的大小。 |