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
Forum Updated to NodeBB v4.3 + New Features

QTemporaryFile Problems

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 1.7k 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 From what I understand from the documentation of QTemporaryFile is that creating the object with new does not actually create the file. You need to call open to do so. However, as your quoted note indicates, that will attempt to create an unnamed file (which is not necessarily successful). Following the same note, I therefore subsequently call fileName to ensure that it the QTemporaryFile has a name. The test of existance in my code should therefore now only fail in case the disk is full. At least, this is how I understand the documentation...

    With "outside of QtCreator", I mean that I run the deployable variant within the same Linux (WSL) context. This deployable variant includes all the necessary dynamically linked libraries as a stand-alone package (i.e., without QtCreator).

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

    @ModelTech
    Under Ubuntu 22.04, Qt 5.15 supplied from distro, the following code

    #include <QCoreApplication>
    #include <QDebug>
    #include <QDir>
    #include <QTemporaryFile>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QTemporaryFile *ImageFile = new QTemporaryFile(QDir::tempPath() + QDir::separator() + "image.XXXXXX");
        ImageFile->open();
        QString TempName = ImageFile->fileName();
        if (!ImageFile->exists())
            qDebug() << "Error: Could not create Temporary Image File";
    
        return a.exec();
    }
    

    seems to always work and create a named file in /tmp each time. Tested compiled for Release & Debug both inside & outside of Creator. Maybe you should try just this? Can you find a pattern as to when the exists() returns false?

    If you are not wanting the file to be accessed from another application you do not need the temporary file to have a name, as per @Christian-Ehrlicher's comment, which you should answer as to why you do need it.

    If you do need it, and for whatever reason (e.g. maybe WSL for all I know) you cannot get it to work reliably/consistently to your requirements, you can of course just "roll your own" temporary file without having to use QTemporaryFile.

    1 Reply Last reply
    0
    • JonBJ JonB

      @ModelTech
      I do understand about open() and fileName(). But I find the docs phrase

      so most applications will not see a difference

      quite unclear to understand, don't know what they are saying here. In any case I acknowledge that you call open() & fileName() prior to exists().

      I asked you to remove all the code to do with writing to the temporary file. Presumably all that matters is the code up to if (!ImageFile->exists()), assuming that is reporting false and that is all you are interested in?

      Creator does not have or supply any libraries, DLLs etc. for your application to use, so I do not how running inside Creator vs outside affects the behaviour. I am not saying that does not matter, nor that you might not use different libraries depending on, say, your PATH inside vs outside of Creator, but Creator does not supply libraries for your application to use. One thing: I assume the "outside Creator" means Release not Debug build, are you doing same build inside Creator to compare?

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

      @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.

      JonBJ Christian EhrlicherC 2 Replies Last reply
      0
      • 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.

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

        @ModelTech
        I asked you to try my minimal code sample, did you do that?
        If you say the problem happens "sometimes", can you find a pattern? (Per chance, if you put in a delay after open()/fileName() and before exists() is that relevant?)

        ModelTechM 1 Reply Last reply
        0
        • JonBJ JonB

          @ModelTech
          I asked you to try my minimal code sample, did you do that?
          If you say the problem happens "sometimes", can you find a pattern? (Per chance, if you put in a delay after open()/fileName() and before exists() is that relevant?)

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

          @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 1 Reply Last reply
          0
          • 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