我正在创建一个图片库,我已经实现了文件的读入并在可调整大小的滚动区域中显示它们。我们已经决定添加元标记/按钮,我正在寻找一种方便的方法,不要改变太多,但要添加这个小功能。
有什么建议可以实现吗?我可以将两个 Qlabels 添加到彼此吗?我试图在新布局中插入两个标签并将其推送到 scrollWidgetLayout,但我只有一个缩略图。
//Create new ThumbNail-Object
thumbNail = new Thumbnail(ui->scrollArea);
scrollWidgetLayout->addWidget(thumbNail);
在图片中你可以看到我已经拥有的和我需要的(黄色)。
最佳答案
您创建一个像容器一样工作的小部件,并将标签放入其中。为这个小部件设置布局,我使用了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
}
}
关于c++ - QT GridLayout 添加 Stacked QLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26853421/