問題描述
我創建了一個名為 ImageLabel 的類,它擴展了 QLabel.我希望它保持它顯示的圖像的大小比例,無論它有多拉伸.當您使窗口變大時,它工作正常.當您嘗試使窗口變小時,問題就出現了:它不會調整高度的大小,而是將其拉伸.我該如何解決這個問題?
I made a class called ImageLabel which extends QLabel. I want it to keep the size ratio of the image that it is displaying no matter how stretched out it is. It works fine when you make the window larger. The problem comes in when you try to make the window smaller: it doesnt resize the height, it leaves it stretched out. How do I fix this?
int ImageLabel::heightForWidth(int width) const {
int height = (this->size.height()*width)/this->size.width();
return height;
}
QSize ImageLabel::sizeHint() const {
return this->size;
}
QSize ImageLabel::minimumSizeHint() const {
return QSize(0, 0);
}
void ImageLabel::setSizePolicy(){
QSizePolicy policy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
policy.setHeightForWidth(true);
QLabel::setSizePolicy(policy);
QLabel::setScaledContents(true);
}
void ImageLabel::setPixmap ( const QPixmap &pixmap ){
this->size = pixmap.size();
QLabel::setPixmap(pixmap);
}
int main(int argc, char *argv[]){
QApplication a(argc, argv);
QFrame *frame = new QFrame;
QVBoxLayout *layout = new QVBoxLayout;
frame->setLayout(layout);
QPixmap map;
map.load("test.png");
ImageLabel *label = new ImageLabel;
label->setSizePolicy();
label->setPixmap(map);
layout->addWidget(label);
frame->show();
return a.exec();
}
推薦答案
有幾種方法可以做到這一點,但最重要的是,我建議不要通過嘗試暗示方面來對抗布局系統.如您所見,您必須嘗試實現一些方法來幫助布局.
There are a couple ways to do this, but most of all I would recommend maybe not fighting against the layout system by trying to hint the aspect. As you can see, you are having to try and implement a number methods trying to help the layout.
我可以舉兩個例子.他們都不使用布局...
I can offer two examples. Neither of them use layouts...
第一個使用子 QLabel
來顯示圖像,并通過調整大小事件驅動其固定大小:
The first uses a child QLabel
to show the image, and drives its fixed size off of the resize event:
// imagelabel.h
class ImageLabel : public QWidget
{
Q_OBJECT
public:
explicit ImageLabel(QWidget *parent = 0);
const QPixmap* pixmap() const;
public slots:
void setPixmap(const QPixmap&);
protected:
void resizeEvent(QResizeEvent *);
private slots:
void resizeImage();
private:
QLabel *label;
};
// imagelabel.cpp
ImageLabel::ImageLabel(QWidget *parent) :
QWidget(parent)
{
label = new QLabel(this);
label->setScaledContents(true);
label->setFixedSize(0,0);
}
void ImageLabel::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
resizeImage();
}
const QPixmap* ImageLabel::pixmap() const {
return label->pixmap();
}
void ImageLabel::setPixmap (const QPixmap &pixmap){
label->setPixmap(pixmap);
resizeImage();
}
void ImageLabel::resizeImage() {
QSize pixSize = label->pixmap()->size();
pixSize.scale(size(), Qt::KeepAspectRatio);
label->setFixedSize(pixSize);
}
第二個示例基于@Arnold_Spence 給出的答案.它甚至更短,因為它不使用子 QLabel.它只是在繪制事件中繪制像素圖:
The second example is based off of the answer given by @Arnold_Spence. It is even shorter as it doesn't use a child QLabel. It just draws the pixmap in the paint event:
// imagelabel2.h
class ImageLabel2 : public QWidget
{
Q_OBJECT
public:
explicit ImageLabel2(QWidget *parent = 0);
const QPixmap* pixmap() const;
public slots:
void setPixmap(const QPixmap&);
protected:
void paintEvent(QPaintEvent *);
private:
QPixmap pix;
};
// imagelabel2.cpp
ImageLabel2::ImageLabel2(QWidget *parent) :
QWidget(parent)
{
}
void ImageLabel2::paintEvent(QPaintEvent *event) {
QWidget::paintEvent(event);
if (pix.isNull())
return;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QSize pixSize = pix.size();
pixSize.scale(event->rect().size(), Qt::KeepAspectRatio);
QPixmap scaledPix = pix.scaled(pixSize,
Qt::KeepAspectRatio,
Qt::SmoothTransformation
);
painter.drawPixmap(QPoint(), scaledPix);
}
const QPixmap* ImageLabel2::pixmap() const {
return &pix;
}
void ImageLabel2::setPixmap (const QPixmap &pixmap){
pix = pixmap;
}
這篇關于如何使圖像調整大小以在 Qt 中縮放?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!