Fix usage of QApplication for PyQt 4.6, since in 4.6 the global application instance is garbage collected, we need to store it in a global variable

This commit is contained in:
Kovid Goyal 2009-10-29 13:12:34 -06:00
parent ef6e9a9abc
commit a28079ff17
11 changed files with 60 additions and 52 deletions

View File

@ -87,4 +87,6 @@ class Economist(BasicNewsRecipe):
feeds[key].append(article) feeds[key].append(article)
ans = [(key, feeds[key]) for key in ans if feeds.has_key(key)] ans = [(key, feeds[key]) for key in ans if feeds.has_key(key)]
if not ans:
raise Exception('Could not find any articles. Has your subscription expired?')
return ans return ans

View File

@ -11,7 +11,6 @@ from PyQt4.Qt import QUrl, QApplication, QSize, QEventLoop, \
SIGNAL, QPainter, QImage, QObject, Qt SIGNAL, QPainter, QImage, QObject, Qt
from PyQt4.QtWebKit import QWebPage from PyQt4.QtWebKit import QWebPage
class HTMLTableRenderer(QObject): class HTMLTableRenderer(QObject):
def __init__(self, html, base_dir, width, height, dpi, factor): def __init__(self, html, base_dir, width, height, dpi, factor):
@ -87,8 +86,9 @@ def render_table(soup, table, css, base_dir, width, height, dpi, factor=1.0):
return images return images
def do_render(html, base_dir, width, height, dpi, factor): def do_render(html, base_dir, width, height, dpi, factor):
if QApplication.instance() is None: from calibre.gui2 import is_ok_to_use_qt
QApplication([]) if not is_ok_to_use_qt():
raise Exception('Not OK to use Qt')
tr = HTMLTableRenderer(html, base_dir, width, height, dpi, factor) tr = HTMLTableRenderer(html, base_dir, width, height, dpi, factor)
tr.loop.exec_() tr.loop.exec_()
return tr.images, tr.tdir return tr.images, tr.tdir

View File

@ -18,7 +18,6 @@ from PyQt4.QtGui import QColor
from PyQt4.QtGui import QImage from PyQt4.QtGui import QImage
from PyQt4.QtGui import QPainter from PyQt4.QtGui import QPainter
from PyQt4.QtSvg import QSvgRenderer from PyQt4.QtSvg import QSvgRenderer
from PyQt4.QtGui import QApplication
from calibre.ebooks.oeb.base import XHTML, XLINK from calibre.ebooks.oeb.base import XHTML, XLINK
from calibre.ebooks.oeb.base import SVG_MIME, PNG_MIME from calibre.ebooks.oeb.base import SVG_MIME, PNG_MIME
from calibre.ebooks.oeb.base import xml2str, xpath from calibre.ebooks.oeb.base import xml2str, xpath
@ -30,8 +29,9 @@ KEEP_ATTRS = set(['class', 'style', 'width', 'height', 'align'])
class SVGRasterizer(object): class SVGRasterizer(object):
def __init__(self): def __init__(self):
if QApplication.instance() is None: from calibre.gui2 import is_ok_to_use_qt
QApplication([]) if not is_ok_to_use_qt():
raise Exception('Not OK to use Qt')
@classmethod @classmethod
def config(cls, cfg): def config(cls, cfg):

View File

@ -18,7 +18,7 @@ from calibre.ebooks.metadata import authors_to_string
from PyQt4 import QtCore from PyQt4 import QtCore
from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, \ from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, \
QApplication, QPrinter, QMetaObject, QSizeF, Qt QPrinter, QMetaObject, QSizeF, Qt
from PyQt4.QtWebKit import QWebView from PyQt4.QtWebKit import QWebView
from pyPdf import PdfFileWriter, PdfFileReader from pyPdf import PdfFileWriter, PdfFileReader
@ -37,8 +37,9 @@ class PDFMetadata(object):
class PDFWriter(QObject): class PDFWriter(QObject):
def __init__(self, opts, log): def __init__(self, opts, log):
if QApplication.instance() is None: from calibre.gui2 import is_ok_to_use_qt
QApplication([]) if not is_ok_to_use_qt():
raise Exception('Not OK to use Qt')
QObject.__init__(self) QObject.__init__(self)
self.logger = log self.logger = log

View File

@ -528,12 +528,14 @@ class Application(QApplication):
if set_qt_translator(self._translator): if set_qt_translator(self._translator):
self.installTranslator(self._translator) self.installTranslator(self._translator)
_store_app = None
def is_ok_to_use_qt(): def is_ok_to_use_qt():
global gui_thread global gui_thread, _store_app
if islinux and ':' not in os.environ.get('DISPLAY', ''): if islinux and ':' not in os.environ.get('DISPLAY', ''):
return False return False
if QApplication.instance() is None: if _store_app is None and QApplication.instance() is None:
QApplication([]) _store_app = QApplication([])
if gui_thread is None: if gui_thread is None:
gui_thread = QThread.currentThread() gui_thread = QThread.currentThread()
return gui_thread is QThread.currentThread() return gui_thread is QThread.currentThread()

View File

@ -98,7 +98,7 @@ class FontKeyChooser(QDialog, Ui_Dialog):
if __name__ == '__main__': if __name__ == '__main__':
from PyQt4.Qt import QApplication from calibre.gui2 import is_ok_to_use_qt
QApplication([]) is_ok_to_use_qt()
d = FontKeyChooser() d = FontKeyChooser()
d.exec_() d.exec_()

View File

@ -9,7 +9,7 @@ Scheduler for automated recipe downloads
from datetime import datetime, timedelta from datetime import datetime, timedelta
from PyQt4.Qt import QDialog, QApplication, SIGNAL, Qt, QTime, QObject, QMenu, \ from PyQt4.Qt import QDialog, SIGNAL, Qt, QTime, QObject, QMenu, \
QAction, QIcon, QMutex, QTimer QAction, QIcon, QMutex, QTimer
from calibre.gui2.dialogs.scheduler_ui import Ui_Dialog from calibre.gui2.dialogs.scheduler_ui import Ui_Dialog
@ -306,7 +306,8 @@ class Scheduler(QObject):
self.download(urn) self.download(urn)
if __name__ == '__main__': if __name__ == '__main__':
QApplication([]) from calibre.gui2 import is_ok_to_use_qt
is_ok_to_use_qt()
from calibre.library.database2 import LibraryDatabase2 from calibre.library.database2 import LibraryDatabase2
d = SchedulerDialog(RecipeModel(LibraryDatabase2('/home/kovid/documents/library'))) d = SchedulerDialog(RecipeModel(LibraryDatabase2('/home/kovid/documents/library')))
d.exec_() d.exec_()

View File

@ -314,8 +314,8 @@ class %(classname)s(%(base_class)s):
self.source_code.setText('') self.source_code.setText('')
if __name__ == '__main__': if __name__ == '__main__':
from PyQt4.Qt import QApplication from calibre.gui2 import is_ok_to_use_qt
QApplication([]) is_ok_to_use_qt()
from calibre.library.database2 import LibraryDatabase2 from calibre.library.database2 import LibraryDatabase2
from calibre.web.feeds.recipes.model import RecipeModel from calibre.web.feeds.recipes.model import RecipeModel
d=UserProfiles(None, RecipeModel(LibraryDatabase2('/home/kovid/documents/library'))) d=UserProfiles(None, RecipeModel(LibraryDatabase2('/home/kovid/documents/library')))

View File

@ -282,9 +282,10 @@ class StatusBar(QStatusBar):
if __name__ == '__main__': if __name__ == '__main__':
# Used to create the animated status icon # Used to create the animated status icon
from PyQt4.Qt import QApplication, QPainter, QSvgRenderer, QColor from PyQt4.Qt import QPainter, QSvgRenderer, QColor
from subprocess import check_call from subprocess import check_call
app = QApplication([]) from calibre.gui2 import is_ok_to_use_qt
is_ok_to_use_qt()
def create_pixmaps(path, size=16, delta=20): def create_pixmaps(path, size=16, delta=20):
r = QSvgRenderer(path) r = QSvgRenderer(path)

View File

@ -10,7 +10,7 @@ from BeautifulSoup import BeautifulSoup, Tag
from PyQt4 import QtCore from PyQt4 import QtCore
from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, QApplication, Qt, \ from PyQt4.Qt import QUrl, QEventLoop, SIGNAL, QObject, Qt, \
QPrinter, QPrintPreviewDialog, QPrintDialog, QDialog, QMetaObject, Q_ARG QPrinter, QPrintPreviewDialog, QPrintDialog, QDialog, QMetaObject, Q_ARG
from PyQt4.QtWebKit import QWebView from PyQt4.QtWebKit import QWebView
@ -18,8 +18,9 @@ PRINTCSS = 'body{width:100%;margin:0;padding:0;font-family:Arial;color:#000;back
class Printing(QObject): class Printing(QObject):
def __init__(self, spine, preview): def __init__(self, spine, preview):
if QApplication.instance() is None: from calibre.gui2 import is_ok_to_use_qt
QApplication([]) if not is_ok_to_use_qt():
raise Exception('Not OK to use Qt')
QObject.__init__(self) QObject.__init__(self)
self.loop = QEventLoop() self.loop = QEventLoop()