问题
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.
来源:https://stackoverflow.com/questions/44839096/how-can-i-get-a-pyqt-slot-to-update-the-qtreeview-during-the-slot