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. Replace image with new QPixmap in QFile

Replace image with new QPixmap in QFile

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.1k Views 2 Watching
  • 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.
  • EngelardE Offline
    EngelardE Offline
    Engelard
    wrote on last edited by
    #1

    I have a QFile which is .jpg image, and it is part of App resources. How can i replace it's image with completely new? New image have a different resolution, but also .jpg.

    I tried this:

    QFile myFile(resourcePath);
    QPixmap myPix = QPixmap::fromImage(QImage(resourcePathNew));
    myFile.open(QIODevice::WriteOnly);
    if(!myPix.save(&myFile)) ui->textEdit->append("Pixmap load failed");
    

    QPixmap::save() is failing, something wrong, and i don't understand what exactly. Any advice will be appreciated.

    JonBJ Pablo J. RoginaP 2 Replies Last reply
    0
    • EngelardE Engelard

      I have a QFile which is .jpg image, and it is part of App resources. How can i replace it's image with completely new? New image have a different resolution, but also .jpg.

      I tried this:

      QFile myFile(resourcePath);
      QPixmap myPix = QPixmap::fromImage(QImage(resourcePathNew));
      myFile.open(QIODevice::WriteOnly);
      if(!myPix.save(&myFile)) ui->textEdit->append("Pixmap load failed");
      

      QPixmap::save() is failing, something wrong, and i don't understand what exactly. Any advice will be appreciated.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @Engelard
      You seem to be trying to write back into a Qt resource path? Resources are read-only assets embedded in your code, you cannot "replace" them. Unless @Christian-Ehrlicher tells me otherwise?

      EngelardE Christian EhrlicherC 2 Replies Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Engelard said in Replace image with new QPixmap in QFile:

        Any advice will be appreciated.

        check the return values of the functions you call (c basics) and if the pixmap is valid.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • EngelardE Engelard

          I have a QFile which is .jpg image, and it is part of App resources. How can i replace it's image with completely new? New image have a different resolution, but also .jpg.

          I tried this:

          QFile myFile(resourcePath);
          QPixmap myPix = QPixmap::fromImage(QImage(resourcePathNew));
          myFile.open(QIODevice::WriteOnly);
          if(!myPix.save(&myFile)) ui->textEdit->append("Pixmap load failed");
          

          QPixmap::save() is failing, something wrong, and i don't understand what exactly. Any advice will be appreciated.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @Engelard
          You seem to be trying to write back into a Qt resource path? Resources are read-only assets embedded in your code, you cannot "replace" them. Unless @Christian-Ehrlicher tells me otherwise?

          EngelardE Christian EhrlicherC 2 Replies Last reply
          1
          • EngelardE Engelard

            I have a QFile which is .jpg image, and it is part of App resources. How can i replace it's image with completely new? New image have a different resolution, but also .jpg.

            I tried this:

            QFile myFile(resourcePath);
            QPixmap myPix = QPixmap::fromImage(QImage(resourcePathNew));
            myFile.open(QIODevice::WriteOnly);
            if(!myPix.save(&myFile)) ui->textEdit->append("Pixmap load failed");
            

            QPixmap::save() is failing, something wrong, and i don't understand what exactly. Any advice will be appreciated.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #4

            @Engelard said in Replace image with new QPixmap in QFile:

            Any advice will be appreciated.

            You may need to understand what the Qt resource system is all about.
            I already suggested the same documentation in your other post related to this same topic.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • JonBJ JonB

              @Engelard
              You seem to be trying to write back into a Qt resource path? Resources are read-only assets embedded in your code, you cannot "replace" them. Unless @Christian-Ehrlicher tells me otherwise?

              EngelardE Offline
              EngelardE Offline
              Engelard
              wrote on last edited by
              #5

              @JonB said in Replace image with new QPixmap in QFile:

              Resources are read-only assets embedded in your code

              oh..

              JonBJ 1 Reply Last reply
              0
              • EngelardE Engelard

                @JonB said in Replace image with new QPixmap in QFile:

                Resources are read-only assets embedded in your code

                oh..

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @Engelard
                :) I had forgotten it's you, @Pablo-J-Rogina has reminded me!

                It's the same issue as before. If you want to be able to replace a "resource" at runtime with a different file, you may be better supplying the file as an external file.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Engelard
                  You seem to be trying to write back into a Qt resource path? Resources are read-only assets embedded in your code, you cannot "replace" them. Unless @Christian-Ehrlicher tells me otherwise?

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @JonB said in Replace image with new QPixmap in QFile:

                  Unless @Christian-Ehrlicher tells me otherwise?

                  No you're right and when he @Engelard would have checked the return value of open() he would already found out it by himself...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @JonB said in Replace image with new QPixmap in QFile:

                    Unless @Christian-Ehrlicher tells me otherwise?

                    No you're right and when he @Engelard would have checked the return value of open() he would already found out it by himself...

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @Christian-Ehrlicher
                    He already did! :)

                    if(!myPix.save(&myFile)) ui->textEdit->append("Pixmap load failed");
                    

                    Well, he should have checked the open() first, I agree.

                    But I think his real issue was he didn't know why it was failing :)

                    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