QTableView not expanding/reducing when QMainWindow is resized

强颜欢笑 提交于 2019-12-11 09:04:24

问题


I have a QMainWindow.

Inside the QMainWindow, I have a central widget.

For the central widget, I have a QVBoxLayout.

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.

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.

Here is the relevant code.

`

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

`


回答1:


You may consider the following:

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


来源:https://stackoverflow.com/questions/15297182/qtableview-not-expanding-reducing-when-qmainwindow-is-resized

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