Originally Posted by
ZB
I tried having the QLabel scale to fit the QScrollArea but could not make the QPixmap retain its natural size and aspect ratio, having it scale is not an option. I also tried fitting an additional frame and layout inside QScrollArea to try centering my image but could not get it to work. Can Qt do this automatically for me or is there another manual solution that I have not found yet?
All you need is two lines of code:
#include <QApplication>
#include <QLabel>
#include <QPixmap>
#include <QScrollArea>
{
public:
{
l->setAlignment( Qt::AlignCenter ); // <---
l
->setPixmap
( QPixmap( "b.jpg" ) );
setWidget( l );
setWidgetResizable( true ); // <---
}
};
int main( int argc, char **argv )
{
Test t;
t.show();
return app.exec();
}
#include <QApplication>
#include <QLabel>
#include <QPixmap>
#include <QScrollArea>
class Test : public QScrollArea
{
public:
Test() : QScrollArea( 0 )
{
QLabel *l = new QLabel();
l->setAlignment( Qt::AlignCenter ); // <---
l->setPixmap( QPixmap( "b.jpg" ) );
setWidget( l );
setWidgetResizable( true ); // <---
}
};
int main( int argc, char **argv )
{
QApplication app( argc, argv );
Test t;
t.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks