C++ QLabel定位问题
我已经在Qt中制作了以下基本的图像查看器,但是它的行为并不像预期的那样,出于对神圣事物的热爱,我无法找出问题所在。基本上,它是一个QLabel,我为它将电影设置为jpeg/gif格式,然后调整大小并在屏幕上居中显示。问题是,当它启动时,它既不会调整大小,也不会在屏幕上居中,而是填充窗口并拉伸图像 例如: 如果我把它放在QVBoxLayout中,它是一样的但是,如果我执行缩放/缩放(按keyRelease事件中定义的“+”或“-”),它会将图像居中并完全调整大小 例如: 所以问题似乎只是当它加载图像时,但我不知道在哪里。我知道这一定很简单,但我搜索了Qt文档,谷歌,尝试了注释行和添加不同功能的组合,但我什么也没找到 顺便说一下,loadImage函数底部的输出打印出正确的大小和位置,但它似乎不尊重它们C++ QLabel定位问题,c++,qt,positioning,qlabel,C++,Qt,Positioning,Qlabel,我已经在Qt中制作了以下基本的图像查看器,但是它的行为并不像预期的那样,出于对神圣事物的热爱,我无法找出问题所在。基本上,它是一个QLabel,我为它将电影设置为jpeg/gif格式,然后调整大小并在屏幕上居中显示。问题是,当它启动时,它既不会调整大小,也不会在屏幕上居中,而是填充窗口并拉伸图像 例如: 如果我把它放在QVBoxLayout中,它是一样的但是,如果我执行缩放/缩放(按keyRelease事件中定义的“+”或“-”),它会将图像居中并完全调整大小 例如: 所以问题似乎只是当它加载图
#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));
}
}
#包括
#包括“imageviewer.h”
ImageViewer::ImageViewer(QString imagePath)
{
setWindowTitle(imagePath+“-简单图像查看器”);
currentImageSize=新的QSize(0,0);
填充屏幕();
imageLabel=新的QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
图像标签->设置缩放内容(真);
QVBoxLayout*布局=新的QVBoxLayout();
布局->添加小部件(图像标签);
QFrame*frame=新QFrame();
框架->设置布局(布局);
//setCentralWidget(框架);
setCentralWidget(图像标签);
loadImage(imagePath);
}
void ImageViewer::loadImage(QString imagePath){
currentImagePath=imagePath;
打开(图像路径);
fitImage();
centerImage();
QTextStream out(标准输出);
//对于调试:
输出宽度()
.arg(图像标签->高度()
.arg(图像标签->x())
.arg(imageLabel->y());
}
void ImageViewer::打开(QString imagePath){
如果(!imagePath.isEmpty()){
QImage图像(imagePath);
currentImageSize=新QSize(image.size().width(),image.size().height());
if(image.isNull()){
QMessageBox::信息(这个,tr(“图像查看器”),
tr(“无法加载%1”).arg(图像路径));
返回;
}
imageLabel->resize(currentImageSize->width(),
currentImageSize->height());
QMovie*movie=新的QMovie(imagePath);
图像标签->设置电影(电影);
电影->开始();
scaleFactor=1.0;
imageLabel->adjustSize();
}
}
void ImageViewer::fitImage(){
int windowHeight,windowWidth;
int imageHeight,imageWidth;
windowHeight=此->高度();
windowWidth=此->宽度();
imageHeight=currentImageSize->height();
imageWidth=currentImageSize->width();
如果(图像高度>窗口高度| |图像宽度>窗口宽度){
图像标签->调整大小((窗口高度-40)*图像宽度/图像高度,
窗高-40);
}
}
void ImageViewer::centerImage(){
int windowHeight,windowWidth;
int imageHeight,imageWidth;
windowHeight=此->高度();
windowWidth=此->宽度();
imageHeight=imageLabel->height();
imageWidth=imageLabel->width();
int x,y;
x=(窗口宽度图像宽度)/2;
y=(窗口高度图像高度)/2;
图像标签->移动(x,y);
}
void ImageViewer::fillScreen(){
此->显示最大化();
}
void ImageViewer::scaleImage(双因素)
{
双新尺度=尺度因子+因子;
如果(newScale>MAX|u SCALE | newScaleresize(scaleFactor*currentImageSize->width()),
scaleFactor*currentImageSize->height();
out我认为问题在于您正试图在主窗口构造函数中计算大小。此时未建立这些大小。您需要覆盖(在ImageViewer
)然后在那里进行大小计算。该函数在设置小部件的几何图形后调用。我认为问题在于您正试图在主窗口构造函数中计算大小。此时未建立这些大小。您需要覆盖(在ImageViewer
中)然后在那里进行大小计算。该函数在设置小部件的几何体后调用。感谢您的输入。我在构造函数中进行了一些调试,它显示大小可用;但是,我创建了一个函数foo(),它加载图像并执行所有计算,并在show()之后调用该函数调用main()后,它的行为仍然相同。感谢您的输入。我在构造函数中进行了一些调试,它显示大小可用;但是,我创建了一个函数foo(),该函数加载图像并执行所有计算,在调用show()后,调用main()后,它的行为仍然相同。
#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