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. PyQt5: Changing current working directory and restoring, causes image loading bug.

PyQt5: Changing current working directory and restoring, causes image loading bug.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.1k 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.
  • E Offline
    E Offline
    enjoysmath
    wrote on 26 Feb 2019, 15:05 last edited by enjoysmath
    #1
        if not os.path.exists(self._workDir):
            os.mkdir(self._workDir)
        curdir = QDir.currentPath()
        QDir.setCurrent(self._workDir)
        while self._renderQueue:
            queue = list(self._renderQueue)
            self._renderQueue.clear()
            for document in queue:
                self.renderDocument(document)
            self.msleep(300)
        QDir.setCurrent(curdir)
    

    That's code of a thread that gets run before startup (and it also is used later, but new threads created).

    Anyway, I'm using QSplashScreen to load an image from /images and that works normally, but only when I change working directory (and then change it back) does it fail.

    My project structure is this:
    [project]
    |
    + images /
    + latex_svg/
    + ( main source code here in project dir)
    + ...
    The above self._workDir that I change to is latex_svg. So I'm assuming the system still thinks we're in there.

    I've tried both Python's os.chdir and PyQt5's QDir approach and both do the same thing.

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    J 1 Reply Last reply 27 Feb 2019, 06:07
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 26 Feb 2019, 19:59 last edited by
      #2

      you said thread, not process. different threads within an application will share the same process environment..IOW, the changing working directory is probably not thread-safe. if you truly need to do concurrent workers in different directories then either hardcode the complete paths correctly in your file operations or use instances of processes, not threads.

      1 Reply Last reply
      2
      • E enjoysmath
        26 Feb 2019, 15:05
            if not os.path.exists(self._workDir):
                os.mkdir(self._workDir)
            curdir = QDir.currentPath()
            QDir.setCurrent(self._workDir)
            while self._renderQueue:
                queue = list(self._renderQueue)
                self._renderQueue.clear()
                for document in queue:
                    self.renderDocument(document)
                self.msleep(300)
            QDir.setCurrent(curdir)
        

        That's code of a thread that gets run before startup (and it also is used later, but new threads created).

        Anyway, I'm using QSplashScreen to load an image from /images and that works normally, but only when I change working directory (and then change it back) does it fail.

        My project structure is this:
        [project]
        |
        + images /
        + latex_svg/
        + ( main source code here in project dir)
        + ...
        The above self._workDir that I change to is latex_svg. So I'm assuming the system still thinks we're in there.

        I've tried both Python's os.chdir and PyQt5's QDir approach and both do the same thing.

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 27 Feb 2019, 06:07 last edited by
        #3

        @enjoysmath You should check the return value of both setCurrent() calls and print out curdir and self._workDir.

        "but only when I change working directory (and then change it back) does it fail" - this is not clear to me. The first setCurrent call succeeds but the second fails?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        2/3

        26 Feb 2019, 19:59

        • Login

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