How can I get a PyQt slot to update the QTreeView during the slot?

て烟熏妆下的殇ゞ 提交于 2019-12-11 18:29:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!