Qt QGridLayout setRowStretch 不工作

Qt QGridLayout setRowStretch not working

我是 Qt 新手。我正在尝试创建具有以下布局的 window,

-------------------------
|按钮 1 |按钮2|
|------------------------|
|-------- 图片--------|
------------------------

我可以使用 QGridlayout 创建此布局。看下面代码,

  QPushButton *button = new QPushButton(this);
  QPushButton *nextButton = new QPushButton(this);
  button->setText("Select new Image");
  nextButton->setText("Next Image >>");
  button->setMinimumSize(100,50);
  nextButton->setMaximumSize(500,50);

  button->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  nextButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  this->centralWidget = new QWidget(this);
  this->centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
  this->centralWidget->setGeometry(0,0,800,800);
  QPalette Pal(palette());
  Pal.setColor(QPalette::Background, Qt::black);
  this->centralWidget->setAutoFillBackground(true);
  this->centralWidget->setPalette(Pal);

  this->layout = new QGridLayout (centralWidget);
  this->layout->addWidget(button,0,0,1,1);
  this->layout->addWidget(nextButton,0,1,1,1);
  this->layout->addWidget(this->imageLabel,1,0,1,1);
  this->layout->setRowStretch(0,1);
  this->layout->setRowStretch(1,10);

但我想要的是图像应该占据更多 space 但图像只显示了 window 的下半部分。 Please See the image 您可以看到按钮和图像之间的 space。我想删除 space.

我试过使用 setRowStretch 但这没有帮助。

PS: 我正在使用带有 Qt 插件的 Eclipse。还有一些我做出的选择可能很差。我是 Qt 的新手。非常感谢

改变

this->layout->addWidget(this->imageLabel,1,0,1,1);

this->layout->addWidget(this->imageLabel,1,0,1,2,Qt::AlignCenter);

第5个参数是列跨度,这意味着新布局项跨越单元格(1,0) 和(1,1)。 看看:addWidget。 您还希望图像居中,第 6 个参数是对齐参数,默认为 0。

如果您还想拥有更大的图像,您应该缩放它。 (我假设您的其余代码是正确的。)

行拉伸起作用了,它的作用是第一行只得到它需要的最小值 space,第二行得到所有剩余的 space,所以你的按钮是推到顶部: your layout