【发布时间】:2011-12-07 05:03:11
【问题描述】:
我正在尝试使用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) 拥有可伸缩的列)。
行本身扩展得很好。问题似乎是让每个单元格上的小部件展开以占用它们的包含空间。我该怎么做呢? (另外,我的列表在垂直方向上正确扩展,但不是水平方向)。
【问题讨论】:
标签: qt qgridlayout qlayout