1

我编写了一个简单的浏览器程序,在 QTextBrowser 中显示 html 内容。在那个 html 内容中,我有一个超链接,我希望超链接打开一个不同的页面(显示从服务器接收到的 html 内容)。所以基本上在用户在 url 框中输入“服务器”之后,服务器向客户端(浏览器)发送一些 html 数据,然后在 QTextBrowser 中显示之后,用户可以点击链接。单击链接后,客户端请求另一个页面。然后服务器通过发送 html 数据来满足这个请求,然后浏览器应该再次显示 html 内容。我的代码的问题是,在第二次请求之后,即使所有功能都正常工作,也不会显示 html 数据。可能是什么问题呢?

class Browser(QtGui.QMainWindow):
    def __init__(self):
        super(Browser, self).__init__()
        self.ui = uic.loadUi('gui_browser.ui')

        self.csi_thread = Client_Server_Interactive_Thread()
        self.connect(self.csi_thread, QtCore.SIGNAL("display_html(QString)"), self.display_html)

        self.ui.txt_browser.setOpenExternalLinks(True)
        self.connect(self.ui.txt_browser,
                 QtCore.SIGNAL('anchorClicked(const QUrl &)'),
                 self.anchorClickedHandler)

    def anchorClickedHandler(self):
        self.ui.txt_browser.setSource(QtCore.QUrl("html_file.html"))
        send_msg('link') # sends the server a request

    def display_html(self, data):
        temp_html_file = open('html_file.html', 'w')
        temp_html_file.write(data)
        temp_html_file.close()
        self.ui.txt_browser.setText(data)
        self.parse_document(data)

class Client_Server_Interactive_Thread(QtCore.QThread):
    def __init__(self):
        super().__init__()

    def run(self):
        socket_create()
        while True:
            msg = listen_for_msg()
            self.emit(QtCore.SIGNAL('display_html(QString)'), msg)
4

0 回答 0