c++ Qt无法加载某些图像

ax6ht2ek  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(73)

我尝试通过QPixmap加载两个png图像,一个轻松加载,而第二个根本不加载,无论我使用什么文件加载方法。它也可以与一些jpg和bmp文件没有任何问题。什么可能会导致这样的问题?
我在下面和发布版本中包含了这两个图像。我使用的是Qt 5.9.2。

MainWindow::MainWindow(QWidget *parent )
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QPixmap pm("f4f2dda9605b54f8ff706d5a25d36a45f8f4dc50b025e2c91a375541719b3d5c.png");
    ui->label->setPixmap(pm);
    ui->label->setScaledContents(true); // Works

    QPixmap pm2("background.png");
    ui->label_2->setPixmap(pm2);
    ui->label_2->setScaledContents(true); // Doesn't work
}

字符串
图片:https://ibb.co/HP1skr3https://ibb.co/Swpmdny
项目:https://easyupload.io/rspu0k
版本:https://easyupload.io/vos6xs

qoefvg9y

qoefvg9y1#

你只是通过给出文件名来传递相对文件路径。
Qt doc指出:

If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.

字符串
您的文件是否可以从工作目录中获取?工作目录可能与IDE相关。
选项包括:
1.给予文件的完整路径
1.将文件保存在工作目录中
1.将文件添加到资源文件中,并通过其资源名称https://doc.qt.io/qt-6/resources.html访问它

相关问题