Qt 表的最后一列未拉伸以填充父级 [英] Qt table last column not stretching to fill in parent

查看:26
本文介绍了Qt 表的最后一列未拉伸以填充父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QTableWidget(我也尝试了 QTableView)来制表一些数据.我预计最后一列会拉伸并填充父列,但事实并非如此.我在 Stackoverflow 上找到了几个答案,要求我执行以下操作:

I am trying to use a QTableWidget (I also tried a QTableView) to tabulate some data. I expected the last column to stretch and fill the parent, but it isn't. I found a couple of answer on Stackoverflow which asked me to do the following:

QTableWidget* table = new QTableWidget();
QHeaderView* headerView = new QHeaderView(Qt::Horizontal);
table->setWindowTitle(QString::fromUtf8("QTableWidget Header Stretch"));
table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
table->setColumnCount(2);
table->setRowCount(1);
//Set Header Label Texts Here
table->setHorizontalHeaderLabels(QString("HEADER 1;HEADER 2").split(";"));
table->setItem(0,0,new QTableWidgetItem("CELL 1"));
table->setItem(0,1,new QTableWidgetItem("CELL 2"));
headerView->sectionResizeMode(QHeaderView::Stretch);
//add the stretch here
headerView->stretchLastSection();
table->setHorizontalHeader(headerView);
table->show();

然而,它仍然没有拉伸到填充,如下图所示:

However, it is still not stretching to fill, as evident from the following picture:

知道我该怎么做吗?我想大多数答案都与 Qt 4 有关,我使用的是 Qt 5.

Any idea how do I do it? I guess most of the answers concerned Qt 4, I am using Qt 5.

推荐答案

您可能想要:

headerView->setStretchLastSection(true);

这篇关于Qt 表的最后一列未拉伸以填充父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆