简体   繁体   中英

Using QImage load() with filename containing spaces in QT

I am using QImage.load() to load an image in my program, but it only works when the filename doesn't contain any spaces (for exemple: "/Users/Emile/Dropbox/crookedStall cover.jpg" ). As I want the user to be able to select any image from their computer, this is a bit of an issue.

The filename of the selected image is returned by a function is stored in a QString. I have tried using QString.replace() to escape the spaces with a backslash but that didn't seem to work. Obviously, simply removing the spaces doesn't work either.

I've looked around a bit and didn't find any working solutions. How can I load an image with a filename that contains spaces? Thanks!

This works fine for me:

This is just using QFileDialog::getOpenFileNames to locate the files

QString filter = QString("Supported Files (*.shp *.kml *.jpg *.png );;All files (*)");
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select File(s)"), QDir::homePath(), filter);
for(int idx =0; idx < fileNames.size(); ++idx)
{
    QImage image ;
    bool success = image.load(fileNames.at(i));
    qDebug() << "File loaded succesfully " << success ;
}

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