0

So I've been starting to contribute code to uPyLoader on github (https://github.com/BetaRavener/uPyLoader) and this app has a compile button. When the compile button is clicked, it's connected to a slot called compile_files. Here is that code with a few of my modification attempts(in main.py if you're wanting to see the whole file).

The problem I'm trying to solve is that when you compile foo.py it creates foo.mpy and if foo.mpy already exists then you can't really tell if the compile did anything. I wanted to see the time stamp update.

So I was researching all around the QFileSystemModel to see how to refresh it but then I've learned through my reading that if I don't have a custom model then the standard one already creates a QFileSystemWatcher for me. I then realized sure enough, that would explain why a new .mpy shows up if it's not there. Also, if I go remove the file by hand, the PyQt UI updates. So then I was confused why in my function below the unlink didn't make the file go away and then come back.

I then commented out the actual compile step and sure enough the mpy file did disappear. So I learned that within a slot, I don't believe the updates are processed.

    def compile_files(self):
    local_file_paths = self.get_local_file_selection()
    for path in local_file_paths:
        mpy_path = os.path.splitext(path)[0]+".mpy"
        try:
           os.unlink(mpy_path)
        except:
           pass
        QApplication.processEvents()
        time.sleep(3)
        print("unlinked:"+mpy_path)
        last_slash_idx = path.rfind("/")+1
        directory = path[:last_slash_idx]
        name = path[last_slash_idx:]
        subprocess.Popen([Settings().mpy_cross_path, name], cwd=directory)

I then researched how to refresh or repaint in slot and found these:

QT Repaint/Redraw/Update/Do Something!

Why must QApplication.processEvents() be called until QNetworkRequest is finished when using QWebView?

But I tried QApplication.processEvents() and it didn't help.

Can someone point out how to get the ui to update during a slot.

Bonus question!

Why doesn't the QFileSystemWatcher see an updated timestamp and update the model or the QTreeView? Just curious if there is a bug or just that's how it works.

Eradicatore
  • 1,501
  • 2
  • 20
  • 38
  • Two other quick comments. Oddly I think I have seen a version of this code like this work intermittently where the timestamp of the file in question updates. And two, the time.sleep(3) call here is just my attempt to make sure the UI had enough time to show a removal if it was going to. – Eradicatore Jun 30 '17 at 11:40
  • Ok, after more research finding some possible duplicate questions in the C++ area, but even after calling self.localFilesTreeView.repaint() just before the processEvents() it still doesn't work: https://stackoverflow.com/questions/14077280/refresh-update-gui-during-slot-running and https://stackoverflow.com/questions/12410433/forcing-the-qt-gui-to-update-before-entering-a-separate-function – Eradicatore Jun 30 '17 at 14:07
  • Please read the guidance on how to provide a [mcve]. – ekhumoro Jun 30 '17 at 18:55
  • @ekhumoro Thanks for the tip! So in the case above, do you feel it would have been best to ask without any code? I did spend a few days trying to learn this area of PyQT before I asked. And I did try to follow the good question guidelines of walking through my thought process that gave context to the problem. Perhaps then the code here would have been better to leave out, but I thought it helped give a bit more context even if it wasn't something you can "just run". – Eradicatore Jun 30 '17 at 19:22
  • No, you should provide a small, self-contained example that reproduces the problem. Without that, I doubt you will get many useful answers. – ekhumoro Jun 30 '17 at 19:41

0 Answers0