Qt Creator:从资源中读取图像

Qt Creator : Read an image from resources

我尝试从资源中读取图像。

Qt:5.9.1 和 OpenCV:3.2

我的代码是:

// Set initial screen
QImage image;
cv::Mat src = cv::imread(":/images/initialScreen.png");
image = ImageFormat::Mat2QImage(src);
videoFrameLabel->setPixmap(QPixmap::fromImage(image));

我的文件:

enter image description here

我的 .pro 文件:

QT       += core gui
QT += widgets
QT += gui
CONFIG += c++14
QT += multimedia multimediawidgets
...

SOURCES += \
        main.cpp \
        mainwindow.cpp \
    imageformat.cpp

HEADERS += \
        mainwindow.h \
    imageformat.h \
    blob.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv-build\bin\libopencv_video320.dll
...
LIBS += C:\opencv-build\bin\libopencv_imgcodecs320.dll


DISTFILES +=

RESOURCES += \
    rsc.qrc

不行,我哪里出错了?谢谢!

只有 Qt 类 使用 QFile can read from resources stored using the Qt resource system, from the docs:

The resource system is based on tight cooperation between qmake, rcc (Qt's resource compiler), and QFile.

因此,您必须使用QFile从资源文件中读取数据,然后将读取的数据提供给cv::imdecode以获得cv::Mat