我正在尝试使用以下内容布局一个窗口(全部用代码表示)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
)以获得有弹性的柱)。
行本身扩展得很好。问题似乎是让每个单元格中的小部件扩展以占用其包含空间。我该怎么做呢? (另外,我的列表在垂直方向上正在正确扩展,但在水平方向上却没有)。