首页 文章

QT GridLayout添加Stacked QLabel

提问于
浏览
0

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

有什么建议我怎么能做到这一点?我可以互相添加两个 Qlabels 吗?我试图在新布局中粘贴两个标签并将其推送到scrollWidgetLayout,但之后我只有一个Thumbnail .

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

scrollWidgetLayout->addWidget(thumbNail);

在图片中,您可以看到我已经拥有的和我需要的东西(黄色) .

enter image description here

1 回答

  • 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
        }
    }
    

相关问题