久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使圖像調(diào)整大小以在 Qt 中縮放?

How do I make an image resize to scale in Qt?(如何使圖像調(diào)整大小以在 Qt 中縮放?)
本文介紹了如何使圖像調(diào)整大小以在 Qt 中縮放?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

我創(chuàng)建了一個(gè)名為 ImageLabel 的類,它擴(kuò)展了 QLabel.我希望它保持它顯示的圖像的大小比例,無(wú)論它有多拉伸.當(dāng)您使窗口變大時(shí),它工作正常.當(dāng)您嘗試使窗口變小時(shí),問題就出現(xiàn)了:它不會(huì)調(diào)整高度的大小,而是將其拉伸.我該如何解決這個(gè)問題?

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();
}

推薦答案

有幾種方法可以做到這一點(diǎn),但最重要的是,我建議不要通過(guò)嘗試暗示方面來(lái)對(duì)抗布局系統(tǒng).如您所見,您必須嘗試實(shí)現(xiàn)一些方法來(lái)幫助布局.

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.

我可以舉兩個(gè)例子.他們都不使用布局...

I can offer two examples. Neither of them use layouts...

第一個(gè)使用子 QLabel 來(lái)顯示圖像,并通過(guò)調(diào)整大小事件驅(qū)動(dòng)其固定大小:

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);
}

第二個(gè)示例基于@Arnold_Spence 給出的答案.它甚至更短,因?yàn)樗皇褂米?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;
}

這篇關(guān)于如何使圖像調(diào)整大小以在 Qt 中縮放?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 免费激情 | 天天草av | 日本在线看片 | 日本免费网 | 国产激情一区二区三区 | 国产高清美女一级a毛片久久w | 中文字幕人成乱码在线观看 | 成人在线播放网站 | avav在线看| 日韩在线不卡 | 欧美激情视频网站 | 亚洲a视频 | 亚洲男人的天堂网站 | 中文字幕国产视频 | 成人不卡| 9999国产精品欧美久久久久久 | 99精品视频免费观看 | 99久久久99久久国产片鸭王 | 欧美亚洲视频 | 九九热九九 | 欧美日韩在线免费观看 | 亚洲色图在线观看 | 国产欧美在线视频 | 在线国产一区 | 欧洲免费毛片 | 99热在线观看精品 | 国产二区视频 | 久久中文字幕视频 | h肉视频| 黄色大片免费观看 | 国产精品99久 | 国产成人精品一区二区三区四区 | 一区二区三区中文字幕 | 国产精品爱久久久久久久 | 国产中文字幕av | 亚洲一区二区三区四区五区午夜 | 伊人色综合久久天天五月婷 | 亚洲 欧美 另类 综合 偷拍 | 久久久久久久久中文字幕 | 影音先锋中文字幕在线观看 | 亚洲综合一区二区三区 |