Resize Column Width QTableWidget

為{幸葍}努か 提交于 2019-12-07 20:08:22

问题


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);
}

回答1:


Considering that you are using Qt5, give a try to

QTableWidget* mainList = new QTableWidget;
QHeaderView* header = mainList ->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);

OR

There is a header flag to ensure that the QTableView's last column fills up its parent if resized.

header->setStretchLastSection(true);


来源:https://stackoverflow.com/questions/23215456/resize-column-width-qtablewidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!