TylerKehne
TylerKehne

Reputation: 361

How can I shrink QLabel images in a layout?

I'm new to Qt, and I'm trying to make a 10x24 grid of some .png files. It seems like using a QGridLayout and filling it with QLabel objects is the best way to go, but the QLabels don't seem to automatically shrink to fit inside the available space, so I end up with this:

enter image description here

Also, I should mention I'm using a subclass of QLabel I created, and that the objects are created as part of an array in a container object:

void ObjectSlotArray::initialize() {
first = FIRST; 
isCreated = false;
layout = new QGridLayout(this);
for (int c = 0; c < 10; c++) {
    array[c] = new ObjectSlot(c);
    layout->addWidget(array[c], (int)(c / 24), c % 24);
    array[c]->show();} }  //10x24 grid

Upvotes: 2

Views: 379

Answers (3)

TylerKehne
TylerKehne

Reputation: 361

Ok, this seems to work:

void ObjectSlot::paintEvent(QPaintEvent* event) {
    QPixmap* image = new QPixmap("://images/Box.png");
    setPixmap(image->scaled(width(),height(),Qt::KeepAspectRatio));
    QLabel::paintEvent(event);}

Upvotes: 0

galinette
galinette

Reputation: 9292

Use:

array[c]->setScaledContents(true);

Or in your ObjectSlot constructor:

setScaledContents(true);

Upvotes: 1

Hamish Moffatt
Hamish Moffatt

Reputation: 831

QLabel won't scale its image. You could add a resize event handler for your label class (or for the parent container) which would scale the images once the available space is known, then assign it to the labels.

Upvotes: 0

Related Questions