c++ - QT GridLayout 添加 Stacked QLabel

标签 c++ qt

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

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

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

scrollWidgetLayout->addWidget(thumbNail);

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

enter image description here

最佳答案

您创建一个像容器一样工作的小部件,并将标签放入其中。为这个小部件设置布局,我使用了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/

相关文章:

c++ - Spirit V2 和 X3 的状态

c++ - 如何在没有 Qt 项目的情况下在 Visual Studio 中使用 Qt 资源文件?

python - 根据子项 'resizer' 项的位置调整 QGraphicsItem 的大小

c++ - OpenGL 和 Qt : display a label on the mouseover

qt - Qt 中的 ODBC 驱动程序使用

c# - 通过 COM 对象连接 x64 应用程序时出现问题

c++ - 指针不匹配和问题

c++ - 通过网络发送 CTRL-C

c++ - 关于使用 glBufferSubData 频繁更新实例化数组的困惑

c++ - 当我悬停在 qt 中的不同按钮上时如何更改标签文本?