Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:11:21+00:00 2026-06-11T00:11:21+00:00

I have a PyQt4 class that displays a html form and fetches the input,

  • 0

I have a PyQt4 class that displays a html form and fetches the input, thanks to previous questions here (an older one is here).

Now I want to exit the main loop, do something else and restart the main loop with a different html form later. The problem is, that my event handles do not work in the second execution of the main loop. What am I doing wrong?

The problem seems to be that the class MyWebPage does not work properly when app.exec_() is called the second time, as the form is not submitted.

Here is my code which runs, but does not submit the second form:

import sys
from urllib import unquote_plus

from PyQt4 import QtCore, QtGui, QtWebKit

class MyWebPage(QtWebKit.QWebPage):
    formSubmitted = QtCore.pyqtSignal(QtCore.QUrl)

    def acceptNavigationRequest(self, frame, req, nav_type):
        if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted:
            self.formSubmitted.emit(req.url())
        return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)



class PsyTML(QtGui.QWidget):
    def __init__(self):
        super(PsyTML, self).__init__()
        self.elements = {}
        self.view = QtWebKit.QWebView(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.setContentsMargins(0, 0, 0, 0)
        self.view.setPage(MyWebPage())
        self.view.page().formSubmitted.connect(self.handleFormSubmitted)

    def viewPsyTML(self, html):
        self.view.setHtml(html)
        self.show()

    def handleFormSubmitted(self, url):
        elements = self.elements
        self.close()
        for key, value in url.encodedQueryItems():
            key = unquote_plus(bytes(key)).decode('utf8')
            value = unquote_plus(bytes(value)).decode('utf8')
            elements[key] = value
        # do stuff with elements...
        for item in elements.iteritems():
            print '"%s" = "%s"' % item
        QtGui.qApp.quit()


# setup the html form
html = """
<form action="" method="get">
Like it?
<input type="radio" name="like" value="yes"/> Yes
<input type="radio" name="like" value="no" /> No
<br/><input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""

html2 = """
<form action="" method="get">
Choose wisely?
<input type="radio" name="choose" value="A"/> Yes
<input type="radio" name="choose" value="B" /> No
<br/><input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""

def main():
    app = QtGui.QApplication(sys.argv)
    intro = PsyTML()
    intro.viewPsyTML(html)
    # now, the html form is displayed nicely and the form elemtns returned
    app.exec_()
    # do something else here

    # in the secomd run, the form is not submitted.
    intro.viewPsyTML(html2)
    app.exec_()



if __name__ == "__main__":
    main()

I am workinh with Python 2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:11:23+00:00Added an answer on June 11, 2026 at 12:11 am

    The current structure of your program needs to be changed.

    Firstly, the main function should be kept as simple as possible. It should just create an application object and a main window, and then run the event loop. After that, everything should be structured around signals/events and their handlers.

    Currently, there is only one central event: form submission. This happens asynchronously, so the application has to save its current state and then wait passively for each form to be completed, before it can move on to the next task.

    EDIT

    Now that your spec is clearer, one way to achieve what you want is to use a QDialog, which has its own event loop.

    Here’s a revised version of your script that is hopefully closer to what you want:

    import sys
    from urllib import unquote_plus
    
    from PyQt4 import QtCore, QtGui, QtWebKit
    
    class MyWebPage(QtWebKit.QWebPage):
        formSubmitted = QtCore.pyqtSignal(object)
    
        def acceptNavigationRequest(self, frame, req, nav_type):
            if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted:
                elements = {}
                for key, value in req.url().encodedQueryItems():
                    key = unquote_plus(bytes(key)).decode('utf8')
                    value = unquote_plus(bytes(value)).decode('utf8')
                    elements[key] = value
                self.formSubmitted.emit(elements)
            return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)
    
    class PsyTML(QtGui.QDialog):
        def __init__(self):
            super(PsyTML, self).__init__()
            self.elements = {}
            self.view = QtWebKit.QWebView(self)
            layout = QtGui.QVBoxLayout(self)
            layout.addWidget(self.view)
            layout.setContentsMargins(0, 0, 0, 0)
            self.view.setPage(MyWebPage())
            self.view.page().formSubmitted.connect(self.handleFormSubmitted)
    
        def viewPsyTML(self, html):
            self.view.setHtml(html)
            self.exec_()
    
        def handleFormSubmitted(self, elements):
            self.elements = elements
            self.accept()
    
    # setup the html form
    html = """
    <form action="" method="get">
    Like it?
    <input type="radio" name="like" value="yes"/> Yes
    <input type="radio" name="like" value="no" /> No
    <br/><input type="text" name="text" value="" />
    <input type="submit" name="submit" value="Send"/>
    </form>
    """
    
    html2 = """
    <form action="" method="get">
    Choose wisely?
    <input type="radio" name="choose" value="A"/> Yes
    <input type="radio" name="choose" value="B" /> No
    <br/><input type="text" name="text" value="" />
    <input type="submit" name="submit" value="Send"/>
    </form>
    """
    
    def main():
        app = QtGui.QApplication(sys.argv)
    
        intro = PsyTML()
        intro.viewPsyTML(html)
    
        # do stuff with elements...
        for item in intro.elements.iteritems():
            print '"%s" = "%s"' % item
    
        intro = PsyTML()
        intro.viewPsyTML(html2)
    
    if __name__ == "__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's the story: I have a QListview that uses a QSqlQueryModel to fill
We have a PyQt4/PySide Qt4 video player app that has QToolButton object in the
In my Qt-based application (built using PyQt 4.8.6), I have a class that is
I have a lot of existing code that just uses the normal dateTime class
have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have an issue with marshall and unmarshall readers and writers. So here it is.
have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have a custom data structure that I want to display in a PyQt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.