Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems using QPixmap's loadFromData to load an image

Problems using QPixmap's loadFromData to load an image

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.8k Views
  • 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 Offline
    P Offline
    Psycho_Path
    wrote on last edited by
    #1

    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
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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
      0
      • W Offline
        W Offline
        Wilk
        wrote on last edited by
        #3

        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
        0
        • P Offline
          P Offline
          Psycho_Path
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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
            0
            • P Offline
              P Offline
              Psycho_Path
              wrote on last edited by
              #6

              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
              0
              • W Offline
                W Offline
                Wilk
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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
                  0
                  • P Offline
                    P Offline
                    Psycho_Path
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved