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

  • Home
  • SEARCH
  • 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 6666251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:48:32+00:00 2026-05-26T02:48:32+00:00

Writing a script to render arbitrary HTML as an image, and using PyQt. I’m

  • 0

Writing a script to render arbitrary HTML as an image, and using PyQt. I’m confused as to why my QApplication.exit call isn’t making QApplication.exec_ return. Any ideas on this?

class ScreenshotterApplication(QApplication):

    def __init__(self, args, html):
        QApplication.__init__(self, args)

        self.html = html

        self.browser = QWebView()
        self.browser.resize(0, 0)
        self.browser.loadFinished.connect(self.save)
        self.browser.loadProgress.connect(self.progress)

    def render(self):
        self.browser.setHtml(self.html)

    def progress(self, progress):
        print '%d%%' % progress

    def save(self, finished):
        success = False
        if finished:
            print 'saving...'
            # ... snip ...

            success = pixmap.save('screenshot.png')
            if success:
                print 'saved as "screenshot.png"'

        QApplication.exit(0 if success else 1)

    def exec_(self, *args, **kwargs):
        self.render()
        super(QApplication, self).exec_(*args, **kwargs)

def take_screenshot(html):
    app = ScreenshotterApplication(sys.argv, html)
    return app.exec_()

if __name__ == "__main__":
    print take_screenshot('<h1 style="width: 500px">Hello, World!</h1>')
  • 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-05-26T02:48:33+00:00Added an answer on May 26, 2026 at 2:48 am

    QApplication.exit tells the application to leave the main event loop, i.e. to return from exec_() as soon as possible.

    Your problem is that the small piece of static HTML is so easy to retrieve and render, QtWebKit does it right when setHtml is called. No delays or background processing, everything’s done before setHtml returns. So, save is called before the main loop starts (i.e. before exec_ is called).

    It’s like writing return when you’re not in a function – except Qt will just ignore exit quietly if the loop isn’t running.

    Solution: Use a QueuedConnection in your connect call to force the signal to be queued, and delivered when event loop starts. This’ll also work if the loop is already running, of course.

    self.browser.loadFinished.connect(self.save, Qt.QueuedConnection)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script to keep track of missing frames in a render (thousands
I'm writing a script that takes command line arguments. Using BBEdit 10.0.1 on Mac
I'm writing script in remote.ini The script looks like on 1:start:{ server some.irc.server server
I'm writing a script for Caseware, the accounting software my Company uses, and I
I am writing a script for MS PowerShell. This script uses the Copy-Item command.
I'm writing a script to remove some build artifacts older than 1 week. The
I am writing a script at the moment that will grab certain information from
I am writing a script which is likely to be modified by users. Currently
I'm writing a script in python which basically queries WMI and updates the information
I'm writing a script that has to move some file around, but unfortunately it

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.