user1767754
user1767754

Reputation: 25094

QT GridLayout add Stacked QLabel

I am creating an image gallery, i've implemented the reading in of Files and showing them in a resizable scroll-Area. We've decided to add meta-tags / Buttons and i am searching for a convenient way not to change too much but add this little features.

Any suggestion how i can achieve this? Can i add two Qlabels to each other? I tried to stuck two labels in a new layout and push this to the scrollWidgetLayout, but then i have only one Thumbnail.

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

scrollWidgetLayout->addWidget(thumbNail);

In the picture you can see what i have already and what i need (yellow).

enter image description here

Upvotes: 1

Views: 2706

Answers (1)

thuga
thuga

Reputation: 12901

You create a widget that acts like a container and put the labels inside it. Set a layout to this widget, I used QVBoxLayout. A better design would be to create a custom widget by subclassing QWidget, but I just used QFrame to make the example quick and simple.

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

Upvotes: 2

Related Questions