首页 > 解决方案 > 不断将 Git 进度重定向到 QTextBrowser

问题描述

我想将 git 进度发送到 QDialog 中的 QTextBrowser,以便在提取存储库时向用户提供反馈。理想情况下,它将能够是多线程的。你会怎么做?

我查看了显示如何生成进度条的帖子,我仍然不太了解代码,但我无法让它与 pull 一起使用。我已经尝试过使用 gitpython 的进度,但同样,我无法让它与 pull 一起使用。而且我已经尝试将标准输出和标准错误传送到文本浏览器,但它只会挂起而不提供任何文本。

测试代码:

class upd(QtWidgets.QDialog):
    def __init__(self, parent = None):
        super(upd, self).__init__(parent)
        uic.loadUi(os.path.join("C:\\", "testd.ui"))
    
    def poptext(self, p):
        sel = selectors.DefaultSelector()
        sel.register(p.stdout, selectors.EVENT_READ)
        sel.register(p.stderr, selectors.EVENT_READ)

        while True:
            for key, _ in sel.select():
                data = key.fileobj.read().decode()
                if not data:
                    break
                if key.fileobj is p.stdout:
                    self.textBrowser.insertPlainText(data + "\n")
                else:
                    self.textBrowser.insertPlainText(data + "\n")
        self.textBrowser.insertPlainText("Done.")

def test():
    url = "https://github.com/e4862/test.git"
    path = "C:\\test\\"
    clone_repo(url, path)

# Function to clone repo from git
def clone_repo(repo_url, path):
    args = []
    args.append("git")
    args.append("clone")
    args.append("--progress")
    args.append(repo_url)
    args.append(path)

    p = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True, creationflags = subprocess.CREATE_NEW_CONSOLE)

    testupd = upd()
    testupd.poptext(p)
    testupd.exec()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    test()
    app.exec()

标签: python-3.xpyqt5

解决方案


推荐阅读