gpt4 book ai didi

python - PyQt4 线程正确

转载 作者:行者123 更新时间:2023-12-01 09:30:39 25 4
gpt4 key购买 nike

我想制作一个在线程上运行的进度条,并且我希望能够在此过程中移动小部件:

import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QComboBox, QProgressBar, QFileDialog
from PyQt4.QtCore import QSize, pyqtSlot, QCoreApplication, SIGNAL, QThread

class App(QMainWindow):

def __init__(self):
super(App, self).__init__()
self.setGeometry(500, 300, 820, 350)
self.setWindowTitle("Program")
self.initUI()

def initUI(self):

#Buttons
btnposx = 30
btnposy = 50


self.btn4 = QPushButton('Load', self)
self.btn4.move(btnposx,btnposy+220)
self.connect(self.btn4, SIGNAL("released()"), self.test)

#ProgressBar
self.pb = QProgressBar(self)
self.pb.move(btnposx+150,btnposy+220)
self.pb.resize(470,27)

self.show()

def load(self, val):
self.pb.setValue(val)

def test(self):

self.workThread = WorkThread()
self.connect( self.workThread, SIGNAL('pb_update'), self.load)
self.workThread.start()

class WorkThread(QThread):

def __init__(self):
super(WorkThread, self).__init__()
QThread.__init__(self)

def __del__(self):
self.wait()

@pyqtSlot()
def run(self):
val = 0
l = range(1000000)
for i in l:
if i < len(l):
val += 100/len(l)
self.emit(SIGNAL('pb_update'), val)
return

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())

到目前为止,这可行,但效果很差。当我尝试在此过程中移动小部件时,它几乎无法在我的计算机上运行。有没有办法让这个工作更好,这样小部件就不会滞后或停止响应?

最佳答案

您的代码可以进行的改进如下:

  • 在信号和槽之间使用新的连接样式
  • 必须留一点时间让辅助线程将信息发送到主线程。
  • 您必须指明连接类型,在您的情况下为Qt::QueuedConnection
  • 使用pyqtSlot装饰器。
  • 您只需在必要时发出信号,在您的情况下,当值的整个值发生变化时,因为 QProgressBar 无法识别 float 。
<小时/>
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QComboBox, QProgressBar, QFileDialog
from PyQt4.QtCore import QSize, pyqtSlot, pyqtSignal, QThread, Qt

class App(QMainWindow):

def __init__(self):
super(App, self).__init__()
self.setGeometry(500, 300, 820, 350)
self.setWindowTitle("Program")
self.initUI()

def initUI(self):

#Buttons
btnposx = 30
btnposy = 50


self.btn4 = QPushButton('Load', self)
self.btn4.move(btnposx,btnposy+220)
self.btn4.released.connect(self.test)

#ProgressBar
self.pb = QProgressBar(self)
self.pb.move(btnposx+150,btnposy+220)
self.pb.resize(470,27)

self.show()

@pyqtSlot(int)
def load(self, val):
self.pb.setValue(val)

def test(self):

self.workThread = WorkThread()
self.workThread.pb_update.connect(self.load, Qt.QueuedConnection)
#self.workThread.pb_update.connect(self.pb.setValue)
self.workThread.start()

class WorkThread(QThread):
pb_update = pyqtSignal(float)
def __init__(self, *args, **kwargs):
QThread.__init__(self, *args, **kwargs)
self.value = 0

def __del__(self):
self.wait()

@pyqtSlot()
def run(self):
val = 0
l = range(1000000)
for i in l:
if i < len(l):
val += 100/len(l)
int_val = int(val)
if int_val != self.value:
self.value = int_val
self.pb_update.emit(self.value)
QThread.msleep(1)
return

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())

关于python - PyQt4 线程正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50000548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com