【问题标题】:QT GridLayout add Stacked QLabelQT GridLayout 添加 Stacked QLabel
【发布时间】:2014-11-11 22:19:39
【问题描述】:

我正在创建一个图片库,我已经实现了文件的读取并将它们显示在一个可调整大小的滚动区域中。我们决定添加元标记/按钮,我正在寻找一种方便的方法,不要进行太多更改,而是添加这些小功能。

有什么建议可以实现吗?我可以相互添加两个 Qlabels 吗?我试图在一个新布局中粘贴两个标签并将其推送到 scrollWidgetLayout,但是我只有一个缩略图。

//Create new ThumbNail-Object
thumbNail = new Thumbnail(ui->scrollArea);

scrollWidgetLayout->addWidget(thumbNail);

在图片中你可以看到我已经拥有的和需要的(黄色)。

【问题讨论】:

  • 创建一个包含两个标签的QVBoxLayout 小部件。
  • 新建的Widgets在新窗口打开正常吗?
  • 如果你不把它们放在一个布局中或者给它们一个父级,那么是的,如果你打电话给QWidget::show
  • 你能提供一个最小的例子吗?我以某种方式只在滚动区域“尝试将 QLayout”添加到 QScrollArea “scrollArea”中得到一个元素,它已经有一个布局

标签: c++ qt


【解决方案1】:

您创建一个类似于容器的小部件并将标签放入其中。为这个小部件设置一个布局,我使用了QVBoxLayout。更好的设计是通过子类化QWidget 创建一个自定义小部件,但我只是使用QFrame 使示例快速简单。

centralWidget()->setLayout(new QVBoxLayout);
QScrollArea *area = new QScrollArea(this);
area->setWidgetResizable(true);
area->setWidget(new QWidget);
QGridLayout *grid = new QGridLayout;
area->widget()->setLayout(grid);
centralWidget()->layout()->addWidget(area);

for(int row = 0; row < 2; row++)
{
    for(int column = 0; column < 5; column++)
    {
        QFrame *container = new QFrame; // this is your widget.. you can also subclass QWidget to make a custom widget.. might be better design
        container->setStyleSheet("QFrame{border: 1px solid black;}"); // just to see the shapes better.. you don't need this
        container->setLayout(new QVBoxLayout); // a layout for your widget.. again, if you subclass QWidget do this in its constructor
        container->layout()->addWidget(new QLabel("TOP")); // the top label.. in your case where you show the icon
        container->layout()->addWidget(new QLabel("BOTTOM")); // the bottom label.. in your case where you show the tag
        grid->addWidget(container, row, column); // add the widget to the grid
    }
}

【讨论】:

  • 感谢分享代码,我现在就看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 2014-12-05
  • 2023-01-09
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多