从QwebView的PYQt槽返回值到主窗口小部件(PYQt python)

时间:2016-04-22 07:34:29

标签: python pyqt signals-slots qwidget qwebview

我正在使用具有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的值放在我的应用程序中的QLineEdit小部件(" xpath")中。请给我一个建议,我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我无法解决问题,因为QtWebkit已从Qt 5.6中删除,但如果您遇到的问题是因为您没有引用QLineEdit,那么请将QLineEdit传递给您QWebView类的__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 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_())