开发者

Image gets clipped while changing orientation using Qt

HI all,

I wnt to develop an ImageViewer using qt. I m trying to resize big images by scaling them. My problem is , when i change the screen orientation some part of the image gets clipped and also if i open the image in landscape mode, by default the size of image remains small even when i change back to portrait mode. What am i Doin wrong?

Please help me out. Heres the code dat i hv written

ImageViewer::ImageViewer() { setAttribute(Qt::WA_DeleteOnClose); QAction *back = new QAction(this); back->setText(QString("Back")); connect(back,SIGNAL(triggered()),this,SLOT(close())); back->setSoftKeyRole(QAction::PositiveSoftKey); addAction(back);

imageLabel = new QLabel();
imageLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
imageLabel->setAlignment(/*Qt::AlignLeft|*/Qt::AlignCenter);
QWidget *widget = new QWidget;
layout=new QStackedLayout();
layout->addWidget(imageLabel);
widget->setLayout(layout);
setCentralWidget(widget);

}

void ImageViewer::showImage(QString filePath) { QImageReader reader; reader.setFileName(filePath); QSize imageSize开发者_运维知识库 = reader.size(); imageSize.scale(size(), Qt::KeepAspectRatio); reader.setScaledSize(imageSize); QImage image = reader.read(); imageLabel->setPixmap(QPixmap::fromImage(image)); imageLabel->adjustSize(); }


You should re-implement QLabel's resizeEvent or install event filter to it and handle QResizeEvent there The content of showImage method should go to handler of a resize event. Currently you are using size() of ImageViewer widget (which seems to be derived from QMainWindow), it's better to use imageLabel.size(); or the best QResizeEvent::size() as this will prevent a problem if you will change UI layout in future.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜