QGraphicsScene中未显示图像
Image is not shown in QGraphicsScene
描述
我写了以下Qt代码。并生成适用于Windows的可执行文件(*.exe(。但QGraphicsScene中未显示图像(JPG(。我已经检查了图像的路径是否正确。在下面的代码中,MyQGraphicsScene派生类继承了QGraphicsSene类。
我在macOS中编译了相同的代码。然后,macOS的可执行文件(ELF(正确运行。图像文件显示在其组件中。我觉得源代码是一样的。但结果因环境而异。
开发环境
- Windows 10
- Qt版本:5.7.0
- C++编译器:Microsoft Visual C++14.0版(MSVC2015(
源代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>
#include <QMessageBox>
#include <QIcon>
#include "TrainingData.h"
#include "trainingDataMaker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QGraphicsItem *curGItem;
QListWidgetItem *listItem;
ui->setupUi(this);
scene = new MyQGraphicsScene(QRectF(0, 0, 400, 400));
ui->graphicsView->setScene(scene);
listItem = new QListWidgetItem();
listItem->setIcon(QIcon("data/000001.jpg"));
ui->imgListWidget->addItem(listItem);
listItem = new QListWidgetItem();
listItem->setIcon(QIcon("data/000276.jpg"));
ui->imgListWidget->addItem(listItem);
ui->imgListWidget->setIconSize(QSize(300,100));
QPixmap pixmap;
QImage img("data/000001.jpg");
QTransform transForm;
pixmap = QPixmap::fromImage(img);
QGraphicsPixmapItem *imgPixmap = new QGraphicsPixmapItem(pixmap);
transForm = QTransform();
transForm.scale(0.1, 0.1);
imgPixmap->setTransform(transForm);
scene->addItem(imgPixmap);
//connect(scene, SIGNAL(changed(const QList<QRectF> &)), imgPixmap, SLOT(chgSize(const QList<QRectF> &)));
}
我在macOS中编译了相同的代码。然后,macOS的可执行文件(ELF(运行正确。图像文件显示在其组件中。我觉得很尴尬那个源代码是一样的。但结果因环境而异。
由于它在MacOS/X下工作,但在Windows下不工作,一个可能的解释是这是一个分发/环境问题。特别是,也许您的程序在Windows下找不到Qt的图像格式插件(例如qjpeg4.dll或qjpeg5.dll(,这就是为什么图像没有显示的原因。
为了测试这一理论,你可以将qjpeg*.dll插件文件复制到与你的.exe文件相同的文件夹中,然后重新运行你的程序,看看这是否会让它表现得更好。或者,如果你不想使用图像格式插件,你可以将.jpg文件转换为Qt支持的另一种文件格式,如BMP或PNG,然后使用它。
因此,如果应用程序是从IDE启动的,则此问题的原因是我对当前目录的误解。当前目录是下图中的(a(。
之前的目录结构
[Output directory is specified in build configuration] - (a)
|- debug - (b)
| |- test.exe // executable file of this application
| |- data
| |-000001.jpg
|- release
所以,我应该已经定位了"数据"目录,包括目录(a(中的图像文件,如下图所示。我对IDE的这种规格感到奇怪。当然,在应用程序直接启动的情况下,当前目录与exe文件的目录相同(双击exe文件(。
之后的目录结构
[Output directory is specified in build configuration] - (a)
|- debug - (b)
| |- test.exe // executable file of this application
|- release
|- data
|-000001.jpg
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 将图像添加到资源文件夹UWP C++
- 彩色图像的卤化物处理平均值
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 重新定位图像时如何前进到下一个内存块
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- 为什么 BMP 图像上的 imwrite 会卡住/不返回?
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- 如何使用CImg打开图像?
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 使用 CUDA 和纹理进行图像减法