Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    [solved] How to make a code to wait while a file is written on hard drive?

    General and Desktop
    3
    17
    7010
    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
      peterm last edited by

      I need to save frequently QPixmap or QImage into a file on a hard drive, while the image is constantly changing, so eventually I’ll have a set of image files, which I could then convert to animated gif or to some other animated format. I am using the pixmap.save(“filename”) or qimage.save(“filename”) methods. However, the code crashes with the following message: Fatal IO error 11 (Resource temporarily unavailable) on X server. I believe I need to wait while the image is saved into a file and then the image could be changed and only then saved again. Does anyone know how to make a code to wait till a file is saved on a hard drive and then continue to run? Thank you.

      1 Reply Last reply Reply Quote 0
      • M
        mlong last edited by

        As I understand it, the QPixmap/QImage save commands should block until the IO is complete. By the time the save() call returns, the file should be written.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

          I imagine 2 different possibilities: (1) the next save() command comes too soon before the previous save() command was completed, or (2) the image changes during the save() operation before save() completed. I do not know which possibility actually realizes, but the code crushes with Fatal IO error 11. Any advise to fix this problem? Thanks.

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            FWIW, the error message you're getting is from the X Server, and shouldn't have anything to do with File IO .

            Can you post a code snippet of the section of code which is doing the processing/saving of the images?

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply Reply Quote 0
            • R
              reactive last edited by

              http://doc.qt.nokia.com/4.7/qpixmap.html#save
              Since save() blocks until finished as mlong pointed out, the only way for save() to be called
              again simultaneously is if it's from another thread. In which case, you will have to make
              your code thread-safe, either by using flags/locks or queued slot connections.

              http://doc.qt.nokia.com/4.7/qtconcurrent.html
              http://doc.qt.nokia.com/qq/qq14-threading.html

              1 Reply Last reply Reply Quote 0
              • M
                mlong last edited by

                Of course, if you're using QPixmap outside the main thread, then you're bound to have troubles.

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                  There is no much to show, the code is very simple. And it works fine when I need to save a single image. It leads to the crush only when image savings happen very frequently.

                  @
                  int AppWind::updateImage()
                  {
                  ...

                  if (iterCurrent%iterPerOut==0) {
                  frame2d.saveImage();
                  ...
                  }
                  }

                  bool Tp2dFrame::saveImage()
                  {
                  QPixmap* pm = getPixmap();
                  return pm->save(imageFileName);
                  }
                  @

                  [EDIT: code formatting, please wrap in @-tags, Volker]

                  1 Reply Last reply Reply Quote 0
                  • M
                    mlong last edited by

                    (Please wrap your code in @ tags.)

                    Have you tried running the program under the debugger to find out exactly where the crash is occurring?

                    Again, just double checking... this is not running in a separate thread, correct?

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                      Actually, there is a question about threads, because AppWind::updateImage() is a time consuming routine and is run by a separate thread. Any advice? Thanks.

                      1 Reply Last reply Reply Quote 0
                      • M
                        mlong last edited by

                        Is that separate thread using QPixmaps? If it is, that's going to cause you problems.

                        QImages should be safe, if I remember correctly, though.

                        Software Engineer
                        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                          Debugging does not help in this case. When doing debugging step-by-step, then everything goes just fine, no crashes, I think because it's not fast enough. If I allow the code to run then it crashes with the error message "Fatal IO error 11 (Resource temporarily unavailable) on X server", and no other information can be obtained.

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

                            A separate thread calls AppWind::updateImage(), which in turn calls Tp2dFrame::saveImage(), which gets pixmap and saves it to a file: QPixmap* pm = getPixmap(); pm->save(imageFileName);

                            1 Reply Last reply Reply Quote 0
                            • R
                              reactive last edited by

                              Then you need a traffic cop so cars don't crash into one another at the intersection. See my links above.

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

                                I tried to use the safe-thread methods as well, by using QMutexLocker locker(&mutex), but nothing helps. Maybe the problem is that the image changes too fast while the save() operation is still in process. Could it cause a crash?

                                1 Reply Last reply Reply Quote 0
                                • M
                                  mlong last edited by

                                  Again, if you're using QPixmaps in another thread (or in code that's called from another thread) you're going to have issues, whether you're using mutexes or not.

                                  Software Engineer
                                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                                    mlong,
                                    I've fixed the problem as you advised: the main thread should operate on QPixmaps. Thank you so much.

                                    1 Reply Last reply Reply Quote 0
                                    • M
                                      mlong last edited by

                                      Good deal. Glad to help! Please be sure and edit the title of the thread to add [Solved]. Thanks!

                                      Software Engineer
                                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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