Reputation: 361
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:
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
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
Reputation: 9292
Use:
array[c]->setScaledContents(true);
Or in your ObjectSlot constructor:
setScaledContents(true);
Upvotes: 1
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