2012-02-20 65 views
1

我已经在Qt中做了以下基本的图像查看器,但它不像预期的那样行事,对于神圣的爱,我无法找出问题所在。基本上,这是一个QLabel,我已经将该电影设置为jpeg/gif,然后调整大小并将其集中在屏幕上。问题是,它在启动时不会调整大小,也不会将其置于屏幕上,而是会填满窗口并延伸图像。QLabel定位困境

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

如果我把它放在QVBoxLayout,这是同样的事情。 但是,如果我执行缩放/缩放(通过按keyRelease事件中定义的“+”或“ - ”),它使图像居中并完美调整大小。

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

所以,问题似乎只能是当它加载图像,但我不能找出。我知道它一定是简单的,但我已经搜索了Qt文档,谷歌,尝试了评论行的组合,并添加了不同的功能,我什么都没有提出。

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

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

的量,报头是:

#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一次放大后:diffchecker.com/1uDcb83

回答

1

我相信问题是你正试图在主窗口构造函数中计算你的尺寸。这些尺寸目前尚未确定。您需要覆盖QWidget::resizeEvent()(位于ImageViewer)并在那里进行大小计算。该功能在设置小部件的几何图形后调用。

+0

感谢您的意见。我已经在构造函数中进行了一些调试,并且显示尺寸可用;然而,我创建了一个函数foo()来加载图像并执行所有计算,并在main()中调用show()之后调用它,它仍然表现相同。 – 2012-02-20 20:43:09