简体   繁体   中英

C++ Qt QPixmap load always returns false

I try to load an image using QPixmap.

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap test;
    qDebug()<< test.load(":/test.bmp");
}

No matter how I change the file path, it always returns false. What's wrong?

TL;DR: Add following line to your .pro file.

RESOURCES += test.bmp

File paths that start with a colon like the ":/test.bmp" above are treated like resources (see http://doc.qt.io/qt-5/resources.html ) and are compiled into the binary, so you don't need to ship them as files (I would only use a resource if the BMP file is not that big, because it will be in memory when the binary is loaded).

Alternatively, you could just give the relative or absolute path to your file in QPixmap::load() without the colon, eg test.load("test.bmp") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM