ricky rana
ricky rana

Reputation: 57

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

i am building a desktop application using PyQt python which has a QwebBrowser. now i am running some function using javascript which is returning a value say abc as per following example.

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()

now, i want to put the value of abc in a QLineEdit widget("xpath") present in my application.please give me suggestion that how i can i do this?

Upvotes: 0

Views: 697

Answers (1)

7stud
7stud

Reputation: 48599

I can't work up an example because QtWebkit has been removed from Qt 5.6, but if the problem you are having is because you don't have a reference to your QLineEdit, then pass the QLineEdit to your QWebView class's __init__() function:

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_())

Then in your 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)

But I think a better way to organize your code would be to do something like this:

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_())

Upvotes: 1

Related Questions