- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码的问题是工作线程在按下取消按钮后不会终止,并且 def pdialog(self) 在 while 循环结束之前不会停止。我想阻止进度条打印进度,从而阻止后台进程。
import sys
from PyQt4 import QtGui, QtCore
import time
#from t6 import Ui_dialog_progress
#from t6 import Ui_dialog_file
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_dialog_progress(object):
def setupUi(self, dialog_progress):
dialog_progress.setObjectName("dialog_progress")
dialog_progress.resize(401, 165)
self.gridLayout = QtGui.QGridLayout(dialog_progress)
self.gridLayout.setObjectName("gridLayout")
self.lblFileName = QtGui.QLabel(dialog_progress)
self.lblFileName.setText("")
self.lblFileName.setObjectName("lblFileName")
self.gridLayout.addWidget(self.lblFileName, 0, 0, 1, 2)
self.pbarFileSize = QtGui.QProgressBar(dialog_progress)
self.pbarFileSize.setProperty("value", 0)
self.pbarFileSize.setObjectName("pbarFileSize")
self.gridLayout.addWidget(self.pbarFileSize, 1, 0, 1, 2)
self.label_2 = QtGui.QLabel(dialog_progress)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.pbarTotal = QtGui.QProgressBar(dialog_progress)
self.pbarTotal.setProperty("value", 0)
self.pbarTotal.setObjectName("pbarTotal")
self.gridLayout.addWidget(self.pbarTotal, 3, 0, 1, 2)
self.lblTotal = QtGui.QLabel(dialog_progress)
self.lblTotal.setText("")
self.lblTotal.setObjectName("lblTotal")
self.gridLayout.addWidget(self.lblTotal, 4, 0, 1, 2)
spacerItem = QtGui.QSpacerItem(213, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 6, 0, 1, 1)
spacerItem1 = QtGui.QSpacerItem(20, 5, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem1, 5, 1, 1, 1)
self.btnPbarCancel = QtGui.QPushButton(dialog_progress)
self.btnPbarCancel.setObjectName("btnPbarCancel")
self.gridLayout.addWidget(self.btnPbarCancel, 6, 1, 1, 1)
self.retranslateUi(dialog_progress)
QtCore.QMetaObject.connectSlotsByName(dialog_progress)
def retranslateUi(self, dialog_progress):
dialog_progress.setWindowTitle(QtGui.QApplication.translate("dialog_progress", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("dialog_progress", "Total:", None, QtGui.QApplication.UnicodeUTF8))
self.btnPbarCancel.setText(QtGui.QApplication.translate("dialog_progress", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
class Ui_dialog_file(object):
def setupUi(self, dialog_file):
dialog_file.setObjectName(_fromUtf8("dialog_file"))
dialog_file.resize(101, 59)
self.btnCreate = QtGui.QPushButton(dialog_file)
self.btnCreate.setGeometry(QtCore.QRect(10, 20, 77, 25))
self.btnCreate.setObjectName(_fromUtf8("btnCreate"))
self.retranslateUi(dialog_file)
QtCore.QMetaObject.connectSlotsByName(dialog_file)
def retranslateUi(self, dialog_file):
dialog_file.setWindowTitle(QtGui.QApplication.translate("dialog_file", "file", None, QtGui.QApplication.UnicodeUTF8))
self.btnCreate.setText(QtGui.QApplication.translate("dialog_file", "Create", None, QtGui.QApplication.UnicodeUTF8))
class ProgressDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent = None)
self.ui = Ui_dialog_progress()
self.ui.setupUi(self)
self.setWindowModality(QtCore.Qt.ApplicationModal)
class MyThread(QtCore.QThread):
trigger = QtCore.pyqtSignal()
updatePD = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
def run(self):
self.trigger.emit()
class Main(QtGui.QDialog):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.ui = Ui_dialog_file()
self.ui.setupUi(self)
self.ui.btnCreate.clicked.connect(self.start_threads)
def start_threads(self):
self.mythread = MyThread(self)
self.mythread.trigger.connect(self.pdialog)
self.connect(self.mythread, QtCore.SIGNAL("updateFileProgress(int)"), self.updateFileProgress)
self.connect(self.mythread, QtCore.SIGNAL("updateTotalProgress(int)"), self.updateTotalProgress)
self.pd = ProgressDialog()
signal = QtCore.SIGNAL("clicked()")
self.pd.ui.btnPbarCancel.connect(self.pd.ui.btnPbarCancel, signal, self.abort)
self.mythread.start()
def abort(self):
self.pd.close()
self.mythread.terminate()
def updateFileProgress(self, j):
self.pd.ui.pbarFileSize.setValue(j)
print j
def updateTotalProgress(self, i):
self.pd.ui.pbarTotal.setValue(i)
print i
def pdialog(self):
self.pd.show()
i = 1
self.pd.ui.pbarTotal.setValue(0)
self.pd.ui.pbarFileSize.setValue(0)
j = 1
while i < 100:
while j <= 100:
time.sleep(0.01)
self.mythread.emit(QtCore.SIGNAL("updateFileProgress(int)"), j)
QtGui.qApp.processEvents()
j += 1
i += 1
j = 1
self.mythread.emit(QtCore.SIGNAL("updateTotalProgress(int)"), i)
QtGui.qApp.processEvents()
self.pd.close()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwindow = Main()
mainwindow.show()
sys.exit(app.exec_())
最佳答案
不要使用terminate()
来终止线程。
参见http://qt-project.org/doc/qt-4.8/qthread.html#terminate
void QThread::terminate () [slot]
Terminates the execution of the thread. The thread may or may not be terminated immediately, depending on the operating systems scheduling policies. Use QThread::wait() after terminate() for synchronous termination.
When the thread is terminated, all threads waiting for the thread to finish will be woken up.
Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to clean up after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.
有许多温和的方法可以停止线程。
例如,您可以使用标志变量来通知您的工作函数停止。
def start_threads(self):
self.end_flag = False
self.mythread = MyThread(self)
self.mythread.trigger.connect(self.pdialog)
self.connect(self.mythread, QtCore.SIGNAL("updateFileProgress(int)"), self.updateFileProgress)
self.connect(self.mythread, QtCore.SIGNAL("updateTotalProgress(int)"), self.updateTotalProgress)
self.pd = ProgressDialog()
signal = QtCore.SIGNAL("clicked()")
self.pd.ui.btnPbarCancel.connect(self.pd.ui.btnPbarCancel, signal, self.abort)
self.mythread.start()
def abort(self):
self.pd.close()
#self.mythread.terminate()
self.end_flag = True
def pdialog(self):
self.pd.show()
i = 1
self.pd.ui.pbarTotal.setValue(0)
self.pd.ui.pbarFileSize.setValue(0)
j = 1
while i < 100:
while j <= 100:
if self.end_flag:
self.pd.close()
return
time.sleep(0.01)
self.mythread.emit(QtCore.SIGNAL("updateFileProgress(int)"), j)
QtGui.qApp.processEvents()
j += 1
i += 1
j = 1
self.mythread.emit(QtCore.SIGNAL("updateTotalProgress(int)"), i)
QtGui.qApp.processEvents()
self.pd.close()
关于python - 发送终止后Qthread不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051105/
我想写一些必须在自己的线程中工作的类。我读过这篇文章:http://wiki.qt.io/Threads_Events_QObjects。它建议移动必须在自己的线程中工作的对象,例如: TestCla
在调用所有与 QThread::finished() 信号关联的槽后,QThread::wait() 是否返回(即解除阻塞执行)? 提前致谢。 最佳答案 不,它可能会在与信号 finished() 关
我不是在打电话 exec()在我的代码中,但 timer和 QUdpSocket工作正常。是 exec()用于等待 event接着说? 更新:timer正在工作,因为我没有调用 moveToThrea
如果我创建 QThread 作为局部变量,我发现了一个奇怪的行为。 例如,下面的代码将作为单线程工作,这意味着我需要等待 10 秒,结果才会出来。 但是如果我将线程从局部变量更改为成员变量,它将作为多
I solved my issue by moving the mySubQThread run() into the myQThread run() That said, I still would
一段时间以来,我一直在使用 Qt 开发一个应用程序,在该应用程序中我必须从相机中抓取帧。相机将在与应用程序其余部分不同的线程中运行。我遵循了以下建议: http://mayaposch.wordpre
考虑以下代码片段: class ThreadA::QThread { public: ThreadA() { } void run() { myVariable = n
所以我在构造函数中有以下代码。 m_someObject = new SomeObject(); m_someObject->moveToThread(&m_thread); m_thread.sta
我有 3 个 QThreads 相互调用(全部继承自 QThread。我知道有些人可能建议使用 moveToThread,但暂时忽略这个事实)。简化的代码如下所示: Thread1 类: void T
我想知道 new QThread(this) 和 new QThread() 之间有什么区别,以及这将如何影响我的代码在使用 QThread 时的行为. 最佳答案 QThread 的父级谁执行什么没有
我是 PySide2 的新手。我只是想启动一个示例应用程序并在应用程序启动时启动一个线程,并希望在应用程序关闭时停止该线程。当我关闭应用程序时,我收到以下错误:QThread:在线程仍在运行时销毁。s
关于如何实例化和使用 QThread 的官方文档可以在这里找到: http://doc.qt.io/qt-5/qthread.html 该文档描述了两种基本方法:(1) 工作对象方法和 (2) QTh
我有以下设置: int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Create the DBM
这是 QTread 的子对象...并在主线程中将其具体化.... 运行时错误如下: ASSERT failure in QCoreApplication::sendEvent: "Cannot sen
在过去的几天里,我一直在尝试使用 QThreads 而不对 QThread 进行子类化的新的首选方法。我遇到的麻烦是当我试图关闭我创建的一组线程时。我经常收到“线程仍在运行时已被销毁”消息(如果我在
我已经创建了这个继承自QThread的类,用于向数据库服务器发送数据,你觉得怎么样?可以改进吗? 谢谢 #ifndef QUERYTHREAD_H#define QUERYTHREAD_H#inclu
我需要在一个线程中进行无限循环。 在 this article作者写道 >you should never ever block the event loop 因为它会阻塞信号槽概念。如何在 QTh
我试图在一个单独的线程中执行一些工作,并在工作完成后停止该线程。 我已经建立了这样的连接 thread.connect( workerClass, SIGNAL( finished() ), SLOT
我有一个快速的问题。我应该创建一个小的多线程程序来从多个传感器获取数据,并且我了解 pthreads 和 qthreads。我可以访问两个图书馆。我个人偏向于使用 Qt,因为它的设计和各种功能。但是,
我需要在主线程上运行 QThread::usleep() (出于各种原因)。 但是,usleep 是受静电保护的。 我想在没有包装器的情况下使用 QThread 的 usleep 函数(我目前正在使用
我是一名优秀的程序员,十分优秀!