QGridLayout,3个窗格,无法正确扩展 [英] QGridLayout, 3 panes, not expanding properly

查看:381
本文介绍了QGridLayout,3个窗格,无法正确扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用QGridLayout布局窗口(全部使用代码).我可以将小部件添加到布局中,并且它们会显示在我的窗口中,但是我不知道如何正确调整它们的大小.这就是我想要的

I'm trying to layout a window (all in code) with a QGridLayout. I can add widgets to the layout and they display in my window, but I can't figure out how to resize them properly. Here's what I'd like

[Leftmost][--------Center---------][Rightmost]

这些是我窗口的三个窗格"(所有三个窗格).左侧和右侧的宽度应为静态,并分别拥抱其两侧,并且中心应随着窗口的增大(或缩小)而扩展以填充该宽度.

Those are the 3 "panes" of my window (all three of them lists). The left and right ones should be of a static width and hug their respective sides, and the center should expand to fill the width as the window grows (or shrinks).

一些代码:

// Create the subviews, add them to a grid layout, and set the layout to the window.
QTableView *list = new QTableView(0);
list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QTableView *flashList = new QTableView(0);
flashList->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

QPushButton *infoButton = new QPushButton("Info!");
QPushButton *flashFeedsButton = new QPushButton("Flashfeeds");

QGridLayout *gridLayout = new QGridLayout;



// Set the minimum widths for all three columns of the grid
gridLayout->setColumnMinimumWidth(GridColumnFirst, 300);
gridLayout->setColumnMinimumWidth(GridColumnSecond, 300);
gridLayout->setColumnMinimumWidth(GridColumnThird, 300);

// Set the minimum heights for all rows of the grid
int headerFooterHeight = 44;
gridLayout->setRowMinimumHeight(GridRowFirst, headerFooterHeight);
gridLayout->setRowMinimumHeight(GridRowSecond, 200);
gridLayout->setRowMinimumHeight(GridRowThird, headerFooterHeight);


// Set the stretch factors
gridLayout->setColumnStretch(GridColumnFirst, 1);
gridLayout->setColumnStretch(GridColumnFirst, 2);
gridLayout->setColumnStretch(GridColumnThird, 1);

gridLayout->addWidget(list, 1, 0, Qt::AlignLeft);
gridLayout->addWidget(flashList, 1, 1, Qt::AlignCenter);
gridLayout->addWidget(infoButton, 0, 3, Qt::AlignRight);
gridLayout->addWidget(flashFeedsButton, 0, 1, Qt::AlignLeft);

_mainWindow->setLayout(gridLayout);

(您可能会说,这最终将是9x9的网格,但重点仍然是,我试图让中间行(GridRowSecond)具有可伸缩的列).

(As you might be able to tell, this is going to eventually be a 9x9 grid, but the point remains, I'm trying to get my middle row (GridRowSecond) to have the stretchy columns).

行本身可以很好地扩展.问题似乎是使每个单元中的小部件扩展以占用其容纳空间.我该怎么做呢? (此外,我的列表在垂直方向上正确展开,但在水平方向上未展开).

The rows themselves expand just fine. The problem seems to be getting the widgets at each cell to expand to take up their containing space. How do I do this? (also, vertically, my list is expanding properly, but not horizontally).

推荐答案

查看 QGridLayout :: AddWidget :

默认对齐方式为0,这意味着小部件将填充整个单元格.

The default alignment is 0, which means that the widget fills the entire cell.

但是您具有以下条件:

gridLayout->addWidget(list, 1, 0, Qt::AlignLeft);
gridLayout->addWidget(flashList, 1, 1, Qt::AlignCenter);
gridLayout->addWidget(infoButton, 0, 3, Qt::AlignRight);
gridLayout->addWidget(flashFeedsButton, 0, 1, Qt::AlignLeft);

因此,您已明确告知网格布局,您希望窗口小部件分别对齐到其单元格的左,中,右和左.在您的情况下,您可能希望使用默认对齐方式,以允许小部件填充单元格并遵循其各自的大小策略.

Thus, you've specifically told the grid layout that you want your widget aligned to the left, center, right, and left of their cells, respectively. In your case you probably want to use the default alignment allowing the widgets to fill the cells and follow their own respective size policies.

这篇关于QGridLayout,3个窗格,无法正确扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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