繁体   English   中英

Qt QGridLayout setRowStretch不起作用

[英]Qt QGridLayout setRowStretch not working

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

--------------------------
| Button1 | Button2 |
| ------------------------ |
| -------图片-------- |
-------------------------

我可以使用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的新手。 非常感谢

更改

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

这- >布局- > addWidget(这- > imageLabel,1,0,1,2,Qt的:: AlignCenter);

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

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

行拉伸有效,其作用是第一行仅获得所需的最小空间,第二行占据所有剩余空间,因此将按钮推到顶部: 布局

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM