我打算在我的应用程序的初始化向导(由PyQT开发)中使用QThread来运行数据库初始化,但我发现在我在initializePage中调用thread.start()之后线程没有运行:
class DBInitializeWorker(QtCore.QObject):
finished = QtCore.pyqtSignal()
failed = QtCore.pyqtSignal('QString')
progress = QtCore.pyqtSignal(int, int)
def pg_callbk(self, x, y):
self.progress.emit(x, y)
def do_initialize(self):
from restbooks.service import db_initialize
print 'dbinit>>>>>>>>>>>>>'
try:
db_initialize(pgCallbk=self.pg_callbk)
except Exception, e:
self.failed.emit('%s' % e)
else:
self.finished.emit()
class InitializeDatabasePage(QtGui.QWizardPage):
"""
InitializeDatabasePage:
"""
def __init__(self, parent=None):
super(InitializeDatabasePage, self).__init__(parent)
self.setTitle(self.tr("Initilize database..."))
self.setSubTitle(self.tr("kick star to initialize your system."))
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.groupBox = QtGui.QGroupBox(self)
self.groupBox.setTitle(_fromUtf8(""))
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.groupBox)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.progressBar_initialize = QtGui.QProgressBar(self.groupBox)
self.progressBar_initialize.setProperty("value", 24)
self.progressBar_initialize.setObjectName(_fromUtf8("progressBar_initialize"))
self.verticalLayout_2.addWidget(self.progressBar_initialize)
self.label_initialize_msg = QtGui.QLabel(self.groupBox)
self.label_initialize_msg.setAlignment(QtCore.Qt.AlignCenter)
self.label_initialize_msg.setObjectName(_fromUtf8("label_initialize_msg"))
self.verticalLayout_2.addWidget(self.label_initialize_msg)
self.verticalLayout.addWidget(self.groupBox)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self._db_initialized = False
self.qthread = QtCore.QThread(self)
def do_db_initialize(self):
def pg_callbk(x, y):
self.progressBar_initialize.setValue(x*100/y)
def init_finished():
self.label_initialize_msg.setText(self.tr("Database initialized!"))
self._db_initialized = True
self.completeChanged.emit()
def init_failed(msg):
self.label_initialize_msg.setText(self.tr("Failed to initialize database! %s") % msg)
self._db_initialized = False
self.completeChanged.emit()
self.progressBar_initialize.setValue(0)
self.label_initialize_msg.setText(self.tr("Start initializing database ..."))
worker = DBInitializeWorker()
worker.finished.connect(init_finished)
worker.progress.connect(pg_callbk)
worker.failed.connect(init_failed)
worker.finished.connect(self.qthread.quit)
worker.moveToThread(self.qthread)
self.qthread.started.connect(worker.do_initialize)
self.qthread.finished.connect(worker.deleteLater)
self.qthread.start()
def isComplete(self):
return self._db_initialized
def initializePage(self):
if not self._db_initialized:
self.do_db_initialize()
如上面的代码所示,DBInitializeWorker在调用thread.start()之后永远不会运行。任何人都可以帮忙吗?感谢。