调整 QMainWindow 大小时,QTableView 不会扩展/减少 [英] QTableView not expanding/reducing when QMainWindow is resized
问题描述
我有一个 QMainWindow.
I have a QMainWindow.
在 QMainWindow 内,我有一个中央小部件.
Inside the QMainWindow, I have a central widget.
对于中央小部件,我有一个 QVBoxLayout.
For the central widget, I have a QVBoxLayout.
对于这个 QVBoxLayout,我添加了 3 个小部件.一个是带有一些标签的小部件,它有一个 QGridLayout.另一个是水平线.
To this QVBoxLayout, I've added 3 widgets. One is a widget with some labels, which has a QGridLayout. The other is a horizontal line.
第三个是QTableView.问题是,当我尝试扩展 QMainWwindow 时,无论我设置(或不设置)什么调整大小策略,TableView 都不会扩展,并且在扩展的 QMainWindow 的其余部分留下了一个难看的空白区域.
The third is a QTableView. The problem is that when I try and expand the QMainWwindow, despite whatever resize policy I set ( or not), the TableView does not expand, and it leaves an ungainly blank area in the rest of the expanded QMainWindow.
谁能告诉我如何在调整窗口大小时使表格扩展/调整大小.
Could anyone tell me how I can make the table expand/resize when the Window is resized.
这是相关的代码.
`
statInfoWidget = new Static_Info(TagN);
QWidget *widget = new QWidget;
setCentralWidget(widget);
QFrame *hor_line = new QFrame();
hor_line->setFrameShape( QFrame::HLine );
QVBoxLayout *layout = new QVBoxLayout();
layout->setMargin(2);
layout->addWidget(statInfoWidget);
layout->addWidget( hor_line );
Table = new QTableView(this);
temp = Table;
t = new TableLayout(statInfoWidget);
Table->setModel(t);
Table->verticalHeader()->hide();
Table->horizontalHeader()->hide();
Table->setShowGrid(false);
Table->setContextMenuPolicy(Qt::CustomContextMenu);
//Table->setColumnWidth(2,290);
// Table->setColumnWidth(0,25);
// if(num_version == 1)
// Table->setColumnWidth(1,0);
// else
// Table->setColumnWidth(1,43);
// Table->setColumnWidth(3,210);
// Table->setColumnWidth(4,170);
// Table->setColumnWidth(5,50);
statInfoWidget->setStyleSheet("background: rgb(153,185,193);color:rgb(0,0,0); font-family:Tahoma;font-size:19px; border: 0px outset rgb(255,255,255);gridline-color: #669933;"
"selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #486909, stop: 1 white);");
Table->setStyleSheet("background: rgb(153,185,193);color:rgb(0,0,0); font-family:Tahoma;font-size:15px; font-weight:bold; border: 0px outset rgb(255,255,255);gridline-color: #669933;"
"selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 transparent, stop: 1 white);");
// layout->addWidget(button1);
QSizePolicy policy = Table->sizePolicy();
policy.setVerticalStretch(1);
policy.setHorizontalStretch(1);
Table->setSizePolicy(policy);
layout->addWidget(Table);
widget->setLayout(layout);
`
推荐答案
您可以考虑以下几点:
Table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
您可能还想调整表中的单个列的大小,这可以通过传递列索引来完成:
You may also want to resize just a single column in your table, this can be done by passing column index:
Table->horizontalHeader()->setResizeMode(columnIndex, QHeaderView::Stretch);
这篇关于调整 QMainWindow 大小时,QTableView 不会扩展/减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!