0

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?

9
  • Take a look at this post: PySide. JavaScript. Execute js an get result as a pure text or html Commented Apr 22, 2016 at 7:45
  • 1
    That's not how you call a function in js. Commented Apr 22, 2016 at 7:54
  • @salomonderossi thanks for the reference, but i want to know that how to put the value of result in the QLineEdit widget which is present in another def.?? Commented Apr 22, 2016 at 8:34
  • @salomonderossi please help if you have any idea of this question Commented Apr 25, 2016 at 2:38
  • @7stud please take a look. Commented Apr 25, 2016 at 5:11

1 Answer 1

1

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_())
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the help buddy, i will try this and update you within a few hours
its showing me an error- Traceback (most recent call last): File "GUI-Test.py", line 91, in <module> window() File "GUI-Test.py", line 75, in window web = QWebView(w,xpath) TypeError: __init__() takes exactly 2 arguments (3 given)
@ricky rana, GUI programming is an intermediate to advanced topic in computer programming. It's clear that you are a python beginner. You are not ready for PyQt programs yet. Nevertheless, see the change I made.
you are right, i am new to python but don't worry i will be a good programmer like you soon, thanks for the help but sorry to say that its again showing the error - File "GUI-Test.py", line 12, in init QWebView.__init__(parent) TypeError: unbound method __init__() must be called with QWebView instance as first argument (got QWidget instance instead).
@ricky rana: QWebView.__init__(self, parent). Or, you might be able to use super(): super(MyWebView, self).__init__(parent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.