0

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

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

但我想要的是图像应该占据更多空间,但图像只显示在窗口的下半部分。请看图片 您可以看到按钮和图片之间的空间。我想删除空间。

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

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

4

1 回答 1

0

改变

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。

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

行拉伸正在工作,它的作用是第一行只获得所需的最小空间,第二行占据所有剩余空间,因此您的按钮被推到顶部: 您的布局

于 2016-03-11T20:20:42.290 回答