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. QTemporaryFile Problems
Qt 6.11 is out! See what's new in the release blog

QTemporaryFile Problems

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 3.6k Views 1 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.
  • ModelTechM ModelTech

    @JonB I also expected that there should be no difference whatsoever between running it from within the context of QtCreator or not, but that is however exactly what is happening and it does not matter if it is Release or Debug... It's very weird and I never had this before...

    The problem exposes itself from the fact that I need to create a (scaled) QPixmap based on the QTemporaryFile. This gives the error: QPixmap::scaled: Pixmap is a null pixmap

    @Christian-Ehrlicher I also need the QTemporaryFile to be concrete with a filename etc to be able to parse & process it with libtiff elsewhere in my app.

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

    @ModelTech said in QTemporaryFile Problems:

    process it with libtiff elsewhere in my app

    What do you do with libtiff which can Qt not do directly? And TiffOpenExt() allows to pass a custom read function if you really need special tiff functions.

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

    ModelTechM 1 Reply Last reply
    0
    • ModelTechM ModelTech

      @JonB The minimal code works perfectly fine (It took me some time to be able to isolate this part)... I will now try your idea of a delay.

      ModelTechM Offline
      ModelTechM Offline
      ModelTech
      wrote on last edited by
      #11

      @ModelTech Unfortunately, the idea of a delay does not seem to work...

      JonBJ 1 Reply Last reply
      0
      • ModelTechM ModelTech

        @ModelTech Unfortunately, the idea of a delay does not seem to work...

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

        @ModelTech
        If my standalone, minimal example always works but your "larger" code does not, maybe there is something there. Let's start by changing the fileName() line to:

        QString TempName = ImageFile->fileName();
        qDebug() << "Filename created" << TempName;
        if (!ImageFile->exists()) ...
        

        What do you get for the qDebug() line when the exists() fails? (And can you please state categorically that your "failure" case means that exists() returns false and it writes Error: Could not create Temporary Image File, as opposed to any other way of "detecting the file does not exist", you still have not made that clear?)

        ModelTechM 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @ModelTech said in QTemporaryFile Problems:

          process it with libtiff elsewhere in my app

          What do you do with libtiff which can Qt not do directly? And TiffOpenExt() allows to pass a custom read function if you really need special tiff functions.

          ModelTechM Offline
          ModelTechM Offline
          ModelTech
          wrote on last edited by
          #13

          @Christian-Ehrlicher I actually started like that, but I need to process the TIFF images in a way that Qt unfortunately does not support. Qt translates everything to RGB-based color spaces and that is the primary thing that must definately not happen in this case... libtiff is the primary library that supports everything I do need, except for displaying the images. That is what Qt is very good at :)

          Christian EhrlicherC 1 Reply Last reply
          0
          • ModelTechM ModelTech

            @Christian-Ehrlicher I actually started like that, but I need to process the TIFF images in a way that Qt unfortunately does not support. Qt translates everything to RGB-based color spaces and that is the primary thing that must definately not happen in this case... libtiff is the primary library that supports everything I do need, except for displaying the images. That is what Qt is very good at :)

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

            Then pass a custom read function to open or simply use a regular file (or fix the bug in your code whereever it is)

            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
            • JonBJ JonB

              @ModelTech
              If my standalone, minimal example always works but your "larger" code does not, maybe there is something there. Let's start by changing the fileName() line to:

              QString TempName = ImageFile->fileName();
              qDebug() << "Filename created" << TempName;
              if (!ImageFile->exists()) ...
              

              What do you get for the qDebug() line when the exists() fails? (And can you please state categorically that your "failure" case means that exists() returns false and it writes Error: Could not create Temporary Image File, as opposed to any other way of "detecting the file does not exist", you still have not made that clear?)

              ModelTechM Offline
              ModelTechM Offline
              ModelTech
              wrote on last edited by
              #15

              @JonB Good remark! When I mentioned 'exists', I meant that I was looking at the /tmp directory. I have not seen the code produce any qDebug messages in any case...

              Let me do some more testing with the full code by introducing a lot more qDebug() messages to see where the problem arises (and eventually gets exposed by the QPixmap error). It is a bit of a poormans approach to debug, but this is the only way given that it all works fine when debugging with QtCreator ;)

              JonBJ 1 Reply Last reply
              0
              • ModelTechM ModelTech

                @JonB Good remark! When I mentioned 'exists', I meant that I was looking at the /tmp directory. I have not seen the code produce any qDebug messages in any case...

                Let me do some more testing with the full code by introducing a lot more qDebug() messages to see where the problem arises (and eventually gets exposed by the QPixmap error). It is a bit of a poormans approach to debug, but this is the only way given that it all works fine when debugging with QtCreator ;)

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

                @ModelTech said in QTemporaryFile Problems:

                I meant that I was looking at the /tmp directory. I have not seen the code produce any qDebug messages in any case...

                That is a totally different case! I assumed since you showed the debug message if exists() fails that was what you were seeing. You should have said!

                Then the first thing to check is: the file will be deleted any time ImageFile is deleted. Since QTemporaryFile derives from Qobject start by attaching destroyed signal:

                connect(ImageFile, &QObject::destroyed, this /* some object which persists beyond your ImageFile's lifetime */, []() { qDebug() << "ImageFile destroyed!"; } );
                

                Do you get this debug output any time prior to when you expect it to be destroyed (in your destructor)?

                ModelTechM 1 Reply Last reply
                0
                • ModelTechM ModelTech

                  I am trying to use QTemporaryFile as a means to temporarily store an image available from a QIODevice Device during the execution of my app. I have the following code that creates the QTemporaryFile as part of a constructor:

                      QDataStream InStream(Device);
                      ImageFile = new QTemporaryFile(TempDir->path() + QDir::separator() + "image.XXXXXX");
                      ImageFile->open();
                      QString TempName = ImageFile->fileName();
                      if (!ImageFile->exists())
                          qDebug() << "Error: Could not create Temporary Image File";
                      QDataStream OutStream(ImageFile);
                      const int BlockSize = 1024;
                      char Block[BlockSize];
                      int Length = 0;
                      while ((Length = InStream.readRawData(Block, BlockSize)) > 0)
                          OutStream.writeRawData(Block, Length);
                      ImageFile->flush();
                      ImageFile->close();
                  

                  The destructor contains the following code:

                      ImageFile->remove();
                      delete ImageFile;
                  

                  Between calling the constructor and destructor, a QPixmap is created from the QTemporaryFile.

                  This all works perfectly when I run it on Linux (actually WSL - Ubuntu 20.04) from within QtCreator. However, when I run it outside of QtCreator, the QTemporaryFile is not (always) created. What can be wrong here?

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

                  @ModelTech said in QTemporaryFile Problems:

                  QDataStream InStream(Device)

                  What is Device here? A socket?

                  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
                  • JonBJ JonB

                    @ModelTech said in QTemporaryFile Problems:

                    I meant that I was looking at the /tmp directory. I have not seen the code produce any qDebug messages in any case...

                    That is a totally different case! I assumed since you showed the debug message if exists() fails that was what you were seeing. You should have said!

                    Then the first thing to check is: the file will be deleted any time ImageFile is deleted. Since QTemporaryFile derives from Qobject start by attaching destroyed signal:

                    connect(ImageFile, &QObject::destroyed, this /* some object which persists beyond your ImageFile's lifetime */, []() { qDebug() << "ImageFile destroyed!"; } );
                    

                    Do you get this debug output any time prior to when you expect it to be destroyed (in your destructor)?

                    ModelTechM Offline
                    ModelTechM Offline
                    ModelTech
                    wrote on last edited by
                    #18

                    @JonB Sorry for beying unclear earlier. No, I do not get any 'destroyed' messages at all, even not when the destructor is called! It is however really no longer existing after the destructor is called ;)

                    Nevertheless, based on your ideas I did get a lot further now :) I have been able to check that the QTemporaryFile does still exist at the moment that I create the Pixmap. So, the problem exposed by the QPixmap message does not seem to originate from the existance of the QTemporaryFile at all. Although this hasn't solved my problem, I was looking completely in the wrong area...

                    JonBJ 1 Reply Last reply
                    0
                    • ModelTechM ModelTech

                      @JonB Sorry for beying unclear earlier. No, I do not get any 'destroyed' messages at all, even not when the destructor is called! It is however really no longer existing after the destructor is called ;)

                      Nevertheless, based on your ideas I did get a lot further now :) I have been able to check that the QTemporaryFile does still exist at the moment that I create the Pixmap. So, the problem exposed by the QPixmap message does not seem to originate from the existance of the QTemporaryFile at all. Although this hasn't solved my problem, I was looking completely in the wrong area...

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

                      @ModelTech said in QTemporaryFile Problems:

                      No, I do not get any 'destroyed' messages at all, even not when the destructor is called!

                      Well, you ought to! Changing my example to have

                      QObject::connect(ImageFile, &QObject::destroyed, qApp, []() { qDebug() << "ImageFile destroyed!"; } );
                      ...
                      delete ImageFile;
                      

                      does give me the message!

                      But if you are moving to look at a different area you may not care.

                      1 Reply Last reply
                      0
                      • ModelTechM Offline
                        ModelTechM Offline
                        ModelTech
                        wrote on last edited by ModelTech
                        #20

                        I found the problem! I needed to add the relavant Qt image format plugins to my deployable package...

                        Thanks for all the help in directing me to an approach that enabled me to identify the actual problem :) :) :)

                        1 Reply Last reply
                        1
                        • ModelTechM ModelTech has marked this topic as solved on

                        • Login

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