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
EDIT : 이것은 처음로드 후의 QLabel 차이와 QLabel 후 한번 확대이다 diffchecker.com/1uDcb83
입력 해 주셔서 감사합니다. 생성자에서 일부 디버깅을했는데 크기가 사용 가능함을 보여줍니다. 그러나, 나는 이미지를로드하고 모든 계산을 수행하고 main()에서 show()를 호출 한 후에 그것을 호출하고 여전히 동일하게 동작하는 foo() 함수를 만들었습니다. –