Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Problems using QPixmap's loadFromData to load an image

    General and Desktop
    4
    9
    6082
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Psycho_Path last edited by

      Trying to use the loadFromData to load an image that's in a sub-folder where the code looks like this:

      @
      QPixmap qp;
      QString qs = "folder/pic.png"; // I've also tried dual backslash instead of the forward slash
      qp.loadFromData(qs.toAscii());
      @

      I have the folder both in the project's folder as well as in the debug folder where the debug .exe is (not sure where I'm supposed to have it) and this function always returns false.

      Any help would be much appreciated, thanks.

      1 Reply Last reply Reply Quote 0
      • K
        koahnig last edited by

        You may use "QDir::absoluteFilePath":http://qt-project.org/doc/qt-4.8/qdir.html#absoluteFilePath to check where the file should be.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply Reply Quote 0
        • W
          Wilk last edited by

          Hello
          Maybe I miss something, but loadFromData method is used, when you have your picture loaded into memory. Try to use "QPixmap::load":http://qt-project.org/doc/qt-4.8/qpixmap.html#load method

          1 Reply Last reply Reply Quote 0
          • P
            Psycho_Path last edited by

            Tried using the load function and that doesn't seem to be solving my problem either:

            @
            QPixmap qp;
            QString qs = "folder/pic.png"; // I've also tried dual backslash instead of the forward slash
            qp.load(qs);
            @

            1 Reply Last reply Reply Quote 0
            • G
              goetz last edited by

              QPixamp has a constructor taking a QString with the pathname:

              @
              QPixampa qp("foler/pic.png");
              @

              For your path problem. Make a QFile of the path an check if that absolute path exists:

              @
              QFile qpFile("folder/pic.png");
              QFileInfo qpFileInfo(qpFile);
              qDebug() << qpFileInfo. absoluteFilePath();
              @

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply Reply Quote 0
              • P
                Psycho_Path last edited by

                Yes, the file path exists. Tried to run the same code with using the file name in the constructor and it still doesn't work. Is there some flag I should be setting in the .load() call?

                1 Reply Last reply Reply Quote 0
                • W
                  Wilk last edited by

                  Hello
                  It sounds silly, but try to use "./folder/pic.png" or "/folder/pic.png" instead of "folder/pic.png".

                  1 Reply Last reply Reply Quote 0
                  • G
                    goetz last edited by

                    Did you try with an full, absolute path in the constructor? Just to make sure, it's not a "file not found" issue.

                    If that doesn't work either, check if the imageformats support PNG. Add in your main method:

                    @
                    #include <QImageReader>

                    qDebug() << QImageReader::supportedImageFormats();
                    @

                    This should list PNG, amongst others.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply Reply Quote 0
                    • P
                      Psycho_Path last edited by

                      Figured out that my problem was the folder was in the wrong folder for the code to recognize it. In any case I'm now having the problem where the image wont draw via a qpainter.drawPixmap() function. Going to start a new thread

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post