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

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.2k 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.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on 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

    jsulmJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on 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
      • enjoysmathE enjoysmath
            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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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

        • Login

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