From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


Problem loading an image with QImage



  • I am having errors while loadin an image.
    I have the images in this path (where I have my Qt project):
    0_1555847594422_Captura.PNG

    QVector<QString> rutas = QVector<QString>();
    rutas.push_back("Texturas\plantilla_completa.png");
    rutas.push_back("Texturas\plantilla_completa_grises.png");
    

    Then, I call this function to create a texture with that image:

    texture::texture(QVector<QString> rutas, QVector<TexturesTypes> tipo)
    {
        initializeOpenGLFunctions();
        for (int i = 0; i < rutas.size(); i++)
        {
    
    
            QString filename = rutas[i];
            TexturesTypes tipoTextura = tipo[i];
            QImage imagen = QImage(filename);
            QFileInfo info = QFileInfo(filename);
            std::cout<<info.isSymLink()<<std::endl;
            std::cout<<info.absoluteFilePath().toStdString()<<std::endl;
    
            if(imagen.isNull())
            {
                std::cout<<"ES NULA"<<std::endl;
            }
    
            QOpenGLTexture *texture = new QOpenGLTexture(QImage(filename).mirrored());
    
            if (texture == nullptr)
            {
                std::cout << filename.toStdString() << " cannot be loaded" << std::endl;
                return;
            }
        }
    }
    

    And this is what I have in QImage object and QFileInfo:

    0_1555847491916_Captura.PNG .

    What's happening? Maybe the path is wrong?


  • Qt Champions 2018

    What does your std::cout debug output says?
    Also I would suggest to not use relative paths since the current working directory my change during runtime. Use QCoreApplication::applicationFilePath() or add your files to a resource file



  • @Christian-Ehrlicher

    QCoreApplication::applicationFilePath() returns C:\Users\Moreno\Documents\Qt\build-PrototipoIluminacion-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\debug\prototipoIluminacion.exe

    And I have my folder called "Texturas" in C:\Users\Moreno\Documents\Qt\build-PrototipoIluminacion-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\debug , but it is still not working.

    This is what I changed and the std::cout:

            QString filename = rutas[i];
            TexturesTypes tipoTextura = tipo[i];
            std::string ruta =  QCoreApplication::applicationFilePath().toStdString();
            std::cout<<"File path: "<<ruta<<std::endl;
            std::cout<<"My folder path: "<<filename.toStdString()<<std::endl;
            QImage imagen = QImage(filename);
    
            if(imagen.isNull())
            {
                std::cout<<"IS NULL"<<std::endl;
            }
    
    

    0_1556011028650_Captura.PNG

    This is my textures path:

    0_1556011133821_Captura.PNG



  • @JesusM
    Use QCoreApplication::applicationDirPath() instead of QCoreApplication::applicationFilePath()



  • @KillerSmath That solved my problem. Thanks!