QGridLayout,3 个窗格,无法正确扩展

发布于 2024-12-09 20:19:02 字数 1939 浏览 4 评论 0原文

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

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

那些是我的窗口的 3 个“窗格”(所有三个都列出)。左侧和右侧的宽度应为静态宽度并紧贴各自的侧面,并且中心应随着窗口的增大(或缩小)而扩展以填充宽度。

一些代码:(

// 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) 具有弹性列)。

行本身扩展得很好。问题似乎是让每个单元格上的小部件扩展以占用其包含空间。我该怎么做? (另外,我的列表在垂直方向上正在正确扩展,但在水平方向上却没有)。

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

Some code:

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

(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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧笙踏歌 2024-12-16 20:19:02

查看 QGridLayout::AddWidget 上的文档:

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

但是您有以下内容:

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

因此,您已经明确告诉网格布局您希望您的小部件分别与单元格的左侧、中心、右侧和左侧对齐。在您的情况下,您可能希望使用默认对齐方式,允许小部件填充单元格并遵循各自的大小策略。

Look at the docs on QGridLayout::AddWidget:

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

But you have the following:

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.

水染的天色ゝ 2024-12-16 20:19:02

这应该只是在您的小部件上设置适当的大小策略的问题。因此,左侧和右侧的按钮:

button->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

对于中间的按钮

button->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );

,第一个参数定义水平尺寸策略,第二个参数定义垂直尺寸策略。

另请参阅尺寸政策文档

It should just be a matter of setting the proper size policy on your widgets. So the buttons on the left and right:

button->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

and for the middle button

button->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );

where the first argument defines the horizontal size policy and the second one defines the vertical size policy.

See the size policy documentation as well

谁的新欢旧爱 2024-12-16 20:19:02

这里有很多变量(不同的小部件、小部件上的不同大小策略以及布局本身的大小策略,因此很难知道哪里出了问题。有几个建议:

1)首先尝试仅使用一种类型的小部件即可完成您想要的事情。例如,具有所有扩展尺寸策略的所有 QLabel。
2)仅调整小部件的大小策略,我发现在网格布局中添加内容开始使事情变得混乱。小部件大小策略的拉伸因子应该可以正常工作。
3) 不要害怕尝试不同的拉伸因子比率。

我发现这是 Qt Designer(Creator)有助于解决这些问题的事情之一。在 Designer 中调整内容并调整大小比运行编译周期要快得多。解决问题后,您可以将找到的属性放入代码中,而不是编辑、编译、运行。

You have a lot of variables going here (different widgets, different size policies on widgets, and size policies working on the layout itself, so it's a bit difficult to know where things are going wrong. A couple of suggestions:

1) First try to accomplish what you want with just one type of widget. For example, all QLabels with all Expanding size policies.
2) Only adjust the size policies of the widgets, I found that adding stuff in the grid layout starts making things confusing. The stretch factor for the widget's size policy should work fine.
3) Don't be afraid to try different ratios for stretch factors.

I've found that this is one of the things where Qt Designer (Creator) is helpful for puzzling these things out. It's much faster to adjust things in Designer and resize then the run compile cycle. After you have solved the issue there, you can take the properties you have found and put it in your code instead of a edit, compile, run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文