首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QLabel定位困境

QLabel定位困境
EN

Stack Overflow用户
提问于 2012-02-20 18:34:49
回答 1查看 1.1K关注 0票数 1

我在Qt中制作了以下基本的图像查看器,但是它的行为并不像预期的那样,而且出于对神圣事物的热爱,我无法找到问题所在。基本上,这是一个QLabel,我将电影设置为jpeg/gif,然后在屏幕上调整大小并将其居中。问题是,当它启动时,它既不调整屏幕大小,也不将其中心放在屏幕上,而是填充窗口并拉伸图像。

示例:http://i.imgur.com/6l9wA.jpg

如果我把它放在QVBoxLayout里,那也是一样的。然而,,如果我执行缩放/缩放(按"+“或"-",就像keyRelease事件中定义的那样),它会对图像进行中心化和调整大小。

示例:http://i.imgur.com/jGeUV.png

所以问题似乎只有当它加载图像,但我找不出在哪里。我知道这一定很简单,但是我搜索了Qt文档,google,尝试了注释行的组合,添加了不同的函数,但是我什么也没想出来。

顺便说一句,loadImage函数底部的输出输出了正确的大小和位置,但它似乎并不尊重它们。

代码语言:javascript
复制
#include <QtGui>
#include "imageviewer.h"

ImageViewer::ImageViewer(QString imagePath)
{
    setWindowTitle(imagePath + " - Simple Image Viewer");
    currentImageSize=new QSize(0,0);

    fillScreen();

    imageLabel = new QLabel;
    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setScaledContents(true);

    QVBoxLayout* layout=new QVBoxLayout();
    layout->addWidget(imageLabel);

    QFrame* frame=new QFrame();
    frame->setLayout(layout);
    //setCentralWidget(frame);

    setCentralWidget(imageLabel);

    loadImage(imagePath);
}

void ImageViewer::loadImage(QString imagePath){
    currentImagePath=imagePath;
    open(imagePath);
    fitImage();
    centerImage();
    QTextStream out(stdout) ;

    //for debugging:
    out << QString("size: %1 %2 pos: %3 %4")
              .arg(imageLabel->width())
              .arg(imageLabel->height())
              .arg(imageLabel->x())
              .arg(imageLabel->y());
}

void ImageViewer::open(QString imagePath){
      if (!imagePath.isEmpty()) {
        QImage image(imagePath);

        currentImageSize=new QSize(image.size().width(),image.size().height());

        if (image.isNull()) {
            QMessageBox::information(this, tr("Image Viewer"),
                                     tr("Cannot load %1.").arg(imagePath));
            return;
        }

        imageLabel->resize(currentImageSize->width(),
                           currentImageSize->height());
        QMovie* movie=new QMovie(imagePath);
        imageLabel->setMovie(movie);
        movie->start();

        scaleFactor = 1.0;

        imageLabel->adjustSize();
      }
}

void ImageViewer::fitImage(){
    int windowHeight, windowWidth;
    int imageHeight, imageWidth;

    windowHeight = this->height();
    windowWidth = this->width();
    imageHeight = currentImageSize->height();
    imageWidth = currentImageSize->width();

    if(imageHeight > windowHeight || imageWidth > windowWidth){
        imageLabel->resize((windowHeight-40)*imageWidth/imageHeight, 
                            windowHeight-40);
    }
}

void ImageViewer::centerImage(){
    int windowHeight, windowWidth;
    int imageHeight, imageWidth;

    windowHeight = this->height();
    windowWidth = this->width();
    imageHeight = imageLabel->height();
    imageWidth = imageLabel->width();

    int x,y;
    x=(windowWidth-imageWidth)/2;
    y=(windowHeight-imageHeight)/2;
    imageLabel->move(x,y);
}

void ImageViewer::fillScreen(){
    this->showMaximized();
}

void ImageViewer::scaleImage(double factor)
{
    double newScale = scaleFactor + factor;

    if(newScale>MAX_SCALE || newScale<MIN_SCALE){
        return;
    }
    else{
        scaleFactor=newScale;
    }

    QTextStream out(stdout);
    imageLabel->resize(scaleFactor * currentImageSize->width(), 
                       scaleFactor * currentImageSize->height());

    out<< scaleFactor << " " 
       << imageLabel->height() << "," 
       << imageLabel->width() <<endl;

    centerImage();
}


void ImageViewer::keyReleaseEvent(QKeyEvent *event){
    if(event->key()==Qt::Key_Plus){
        scaleImage(SCALE_STEP);
    }
    if(event->key()==Qt::Key_Minus){
        scaleImage(0-(SCALE_STEP));
    }
}

其标题为:

代码语言:javascript
复制
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QLabel>
#include <QMainWindow>

class ImageViewer : public QMainWindow
{
    Q_OBJECT

public:
    ImageViewer(QString imagePath);
    void open(QString imagePath);
    void loadImage(QString imagePath);
private:
    void fitImage();
    void centerImage();
    void fillScreen();
    void scaleImage(double);
    void adjustScrollBar(QScrollBar*,double);
    void keyReleaseEvent(QKeyEvent *);
private:
    QLabel* imageLabel;
    double scaleFactor;
    QSize* currentImageSize;
    QString currentImagePath;
    QString currentDir;
    QStringList neighbourImages;
    static const double MAX_SCALE=2.0;
    static const double MIN_SCALE=0.2;
    static const double SCALE_STEP=0.1;
};

#endif // MAINWINDOW_H

编辑:这是第一次加载后的QLabel和一次放大后的QLabel之间的区别:Diffecker.com/1uDcb83

EN

回答 1

Stack Overflow用户

发布于 2012-02-20 19:03:34

我认为问题在于,您正试图在主窗口构造函数中计算您的大小。目前还没有确定这些大小。您需要覆盖QWidget::resizeEvent() (在ImageViewer中)并在那里进行调整大小的计算。这个函数是在小部件的几何图形设置之后调用的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9366519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档