Return 从 QwebView 的 PYQt 插槽到主要 window 小部件的值(PYQt python)

Return value from PYQt slot of QwebView to main window widget (PYQt python)

我正在使用具有 QwebBrowser 的 PyQt python 构建桌面应用程序。现在我是 运行 一些使用 javascript 的函数,它根据以下示例返回一个值 abc。

class QWebView(QWebView):

 def contextMenuEvent(self,event):  
      menu = QMenu()
      self.actionShowXpath = menu.addAction("Show Xpath")  
      QObject.connect(self.actionShowXpath,SIGNAL("triggered()"),self,SLOT("slotshowxpath()"))

      menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))   

      @pyqtSlot()
      def slotshowxpath(self):
           frame.evaluateJavaScript("var abc = function get()");
           result = frame.evaluateJavaScript("abc").toString()

            **some code code to put result in QLineEdit Widget**
            # something like below
            # xpath.setText(result)


def window():
   app = QtGui.QApplication(sys.argv)
   w = QtGui.QWidget()
   web = QWebView(w)
   web.load(QUrl("http://www.indiatimes.com/"))
   web.show()

   xpath = QtGui.QLineEdit("", w)
   sys.exit(app.exec_())

if __name__ == '__main__':
     window()

现在,我想将 abc 的值放入我的 application.please 中的 QLineEdit 小部件 ("xpath") 给我建议我该怎么做?

我无法编写示例,因为 QtWebkit 已从 Qt 5.6 中删除,但如果您遇到的问题是因为您没有对 QLineEdit 的引用,请将 QLineEdit 传递给您的 QWebView class的__init__()函数:

def start_app():
    app = QtGui.QApplication(sys.argv)

    main_window = QtGui.QWidget()
    xpathInput = QtGui.QLineEdit(main_window)

    web_view = MyWebView(main_window, xpathInput)  #<===HERE
    web_view.load(QUrl("http://www.indiatimes.com/"))

    main_window.show()
    sys.exit(app.exec_())

然后在你的QWebView中class:

class MyWebView(QWebView):
    def __init__(self, parent, xpath_widget):
        #QWebView.__init__(parent)
        QWebView.__init__(self, parent)
        #or: super(MyWebView, self).__init__(parent)

        self.xpath_widget = xpath_widget

    def contextMenuEvent(self,event):  
        menu = QMenu()
        self.actionShowXpath = menu.addAction("Show Xpath") 

        #QObject.connect(
        #    self.actionShowXpath,
        #    SIGNAL("triggered()"),
        #    self,SLOT("slotshowxpath()")
        #)

        self.actionShowXpath.triggered.connect(self.show_xpath)

        menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))   

    #@pyqtSlot()
    def show_xpath(self):
        frame = ...
        frame.evaluateJavaScript("var abc = function get()");
        result = frame.evaluateJavaScript("abc").toString()

        #some code code to put result in QLineEdit Widget**
        self.xpath_widget.setText(result)

但我认为组织代码的更好方法是执行以下操作:

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()

        self.xpathInput = QtGui.QLineEdit(self)

        self.web_view = QWebView(self) 
        self.web_view.load(QUrl("http://www.indiatimes.com/"))

        self.menu = QMenu()
        self.actionShowXpath = self.menu.addAction("Show Xpath") 

        #QObject.connect(
        #    self.actionShowXpath,
        #    SIGNAL("triggered()"),
        #    self,SLOT("slotshowxpath()")
        #)

        self.actionShowXpath.triggered.connect(self.show_xpath)

        menu.exec_(self.mapToGlobal(QPoint(event.x(),event.y())))

    def show_path(self):
        frame = ...
        result = frame.evaluateJavaScript("abc").toString()
        self.xpathInput.setText(result)


def start_app():
    app = QtGui.QApplication(sys.argv)
    main_window = MyWindow()
    main_window.show()
    sys.exit(app.exec_())