Do you have a style sheet attached to the label? Does it add a margin or padding? If so, each time you scale the image you add the margin/padding to it size.
This, for example, does not change the size of the label every two seconds:
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
image.load("test.png");
label1->setPixmap(image);
qDebug() << image.size() << label1->size();
label2
= new QLabel("Label 2",
this);
layout->addWidget(label1);
layout->addWidget(label2);
central->setLayout(layout);
setCentralWidget(central);
connect(&t, SIGNAL(timeout()), SLOT(tick()));
t.start(2000);
}
public slots:
void tick() {
QPixmap temp
= image.
scaled(label1
->width
(), label1
->height
(), Qt
::KeepAspectRatio);
qDebug() << temp.size() << label1->size();
label1->setPixmap(temp);
}
private:
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this);
label1 = new QLabel(this);
image.load("test.png");
label1->setPixmap(image);
qDebug() << image.size() << label1->size();
label2 = new QLabel("Label 2", this);
QVBoxLayout *layout = new QVBoxLayout(central);
layout->addWidget(label1);
layout->addWidget(label2);
central->setLayout(layout);
setCentralWidget(central);
connect(&t, SIGNAL(timeout()), SLOT(tick()));
t.start(2000);
}
public slots:
void tick() {
QPixmap temp = image.scaled(label1->width(), label1->height(), Qt::KeepAspectRatio);
qDebug() << temp.size() << label1->size();
label1->setPixmap(temp);
}
private:
QPixmap image;
QLabel *label1 ;
QLabel *label2 ;
QTimer t;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
unless you add:
qApp->setStyleSheet("QLabel { margin: 5px; }");
qApp->setStyleSheet("QLabel { margin: 5px; }");
To copy to clipboard, switch view to plain text mode
in main().
Bookmarks