调整列宽 QTableWidget [英] Resize Column Width QTableWidget
本文介绍了调整列宽 QTableWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用 QTableWidget 创建了这个小部件:
I have this widget created with QTableWidget:
并且我希望我的表格列调整大小以占据小部件的整个宽度,而行则可以.我知道有一个和我类似的问题,但我无法以这种方式解决它.这是我的代码
and I would like that the column of my table resize in order to occupy the entire width of the widget, while for the rows is ok as it is. I know there is a similar question like mine but I was not able to solve it in that way.. This is my code
void MainWindow::createTable(int rows, int columns) {
mainList = new QTableWidget;
QStringList headerLabels;
headerLabels << "Title" << "Director" << "Year" << "Counter" << "Rating";
mainList->setRowCount(rows);
mainList->setColumnCount(columns);
mainList->setHorizontalHeaderLabels(headerLabels);
mainList->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
mainList->setSelectionBehavior(QAbstractItemView::SelectRows);
mainList->resizeColumnsToContents();
mainList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setCentralWidget(mainList);
}
推荐答案
考虑到您使用的是 Qt5,请尝试
Considering that you are using Qt5, give a try to
QTableWidget* mainList = new QTableWidget;
QHeaderView* header = mainList ->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);
或
有一个标题标志以确保 QTableView 的最后一列在调整大小时填满其父列.
There is a header flag to ensure that the QTableView's last column fills up its parent if resized.
header->setStretchLastSection(true);
这篇关于调整列宽 QTableWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文