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 7860297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:13:55+00:00 2026-06-02T22:13:55+00:00

I have a Django app in which the users can create reports at various

  • 0

I have a Django app in which the users can create reports at various places throughout the webapp. As a little nifty feature I would like to make a “send as PDF” feature for these reports, and I’m almost there.

What I do is that before the report is returned as a HttpResponse through Django I send the raw HTML content through a small PySide/QT snippet (as seen below).

The problem is that I can’t get the QApplication to quit.

I have tried with the standard QCoreApplication.exit() but with no luck. If I try to convert a new report right after the first one, the console says a “QApplication instance already exists”.

I am using Django 1.2.5, Python 2.7, QT 4.8 and PySide 1.1 on OS X 10.7.3 (for testing).

Code:

def makepdf(response,filename):
    try:
        app = QApplication(sys.argv)
    except:
        app = QCoreApplication.instance()
    web = QWebView()

    stylelink = "%s%s" % (media_root,'images/css/reportGenerator.css') 

    web.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(stylelink))
    web.setHtml(response)

    printer = QPrinter()
    printer.setPageSize(QPrinter.A4)
    printer.setOutputFormat(QPrinter.PdfFormat)
    printer.setOutputFileName(filename)

    def convertToPdf():
        web.print_(printer)
        #webresponse = web.close()        
        #QObject.disconnect()
        #print QCoreApplication.instance().children()[0].interrupt()
        #qthread = QCoreApplication.instance().thread()#.cleanup()
        #qthread.exit()
        QCoreApplication.exit()
    QObject.connect(web, SIGNAL("loadFinished(bool)"), convertToPdf())

Code comments:

Currently I have made a try/except clause to be able to keep the code running, by using the current QApplication instance (to avoid the ‘instance exists error’), but this just doesn’t seem right?. I mean, having a QAppliation running for the duration of my Apache server (which will be the case in production) seems a bit off. Shouldn’t it be possible to quit it after the PDF conversion is done?

Other than the QCoreApplication.exit() I have tried to use the sys.exit(app.exec_()) method, as is often seen in examples and snippets. This however just causes an error and makes Python crash and the code works perfectly without this. (well it creates the PDF, but won’t quit).

All the lines commented out are the previous attempts I have made to quit the QApplication.

In short: I don’t know what it is, but it just won’t quit. Can anyone suggest why?

Update: After the latest answer I have edited the code to respond to the input. This is how the final part looks now:

def convertToPdf():
    web.print_(printer)
    app.exit()
web.loadFinished.connect(convertToPdf)
app.exec_()

I do, however, still get an error:

2012-04-30 00:16:10.791 Python[21241:1803] * Assertion failure in +[NSUndoManager _endTopLevelGroupings], /SourceCache/Foundation/Foundation-833.24/Misc.subproj/NSUndoManager.m:324
Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there.

This error only occurs when I implement app.exec_(). However without app.exec_() it is just the normal issue again, with no quitting.

Any ideas?

Update 2: this is the newest code, fixed in accordance with matas latest suggestion:

app = QApplication(sys.argv)
web = QWebView()
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filename)
def convertToPdf():
    web.print_(printer)
    app.exit()
web.loadFinished.connect(convertToPdf)
web.setHtml(response)

I still have the same problem however.

  • 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-02T22:13:56+00:00Added an answer on June 2, 2026 at 10:13 pm

    PySide is designed such that there is only ever one instance of QCoreApplication within a process. The best reference to this (possibly undocumented fact) that I could find was http://bugs.pyside.org/show_bug.cgi?id=855.

    Basically, there may be dangling references to the qapplication that prevent it from being reaped by the garbage collector, so even if you tell the application to exit and you delete your reference, there may still be other references lying around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a django app where users can be looged in with both
I have a Django app which uses LDAP as the authentication backend. I'm not
I have a django app which is used for managing registrations to a survey.
I've created a simple 'favourites' system in my django app. I have events which
Let's suppose I have the following url: /valid/django/app/path/?foo=bar&spam=eggs I can simulate a request to
I'm trying to create a Django app where the user can make a list
I have a model app which has many to many association with users. Here
Let's says I have a Djano app. Users can sign up, get a activation
We have a Django project which runs on Google App Engine and used db.UserProperty
I have a Django app that gets it's data completely from an external source

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.