首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环QProgressBar给出错误>> QObject::installEventFilter:无法为不同线程中的对象筛选事件

循环QProgressBar给出错误>> QObject::installEventFilter:无法为不同线程中的对象筛选事件
EN

Stack Overflow用户
提问于 2011-11-04 10:07:05
回答 1查看 3.9K关注 0票数 5

这个问题似乎以许多不同的形式被问了很多次,但我还没有找到一个带有-relevant的代码解决方案。

当我运行程序时,它显示

QObject::installEventFilter:不能为不同线程中的对象过滤事件。

尽管如此,代码一开始可以工作,但过了一段时间,它会弹出一个错误,python说它停止工作。

我的代码如下:

代码语言:javascript
复制
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from xml.etree import ElementTree as ET
import os , time

class LayoutCreator(QDialog):
    def __init__(self , parent=None):
        super(LayoutCreator, self).__init__(parent)
        self.Cameras_Update()


    def Cameras_Update( self ):                                             # Get all shots with camera plots and add them to the cameras_tree
        busyBar = sqrl_QtTools.BusyBar( text = "Gathering Camera Data" )    # Looping progress bar
        busyBar.start()

        # loop through folder structure storing data                

        busyBar.Kill()                                                      # Close looping progress bar    


class BusyBar(QThread):                     # Looping progress bar
    def __init__(self, text = "" ):
        QThread.__init__(self)
        self.text = text
        self.stop = False

    def run( self ):
        self.proBar = QProgressBar()
        self.proBar.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.SplashScreen )
        self.proBar.setMinimum( 0 )
        self.proBar.setMaximum( 100 )
        self.proBar.setTextVisible( True )
        self.proBar.setFormat( self.text )
        self.proBar.setValue( 0 )
        self.proBar.setFixedSize( 500 , 50 )
        self.proBar.setAlignment(Qt.AlignCenter)
        self.proBar.show()
        while not self.stop:                # keep looping while self is visible
            # Loop sending mail 
            for i in range(100):
                progress = self.proBar.value()
                progress = progress + 1
                self.proBar.setValue( progress )

                time.sleep(0.05)
            self.proBar.setValue( 0 )
        self.proBar.hide()

    def Kill(self):
        self.stop = True
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-05 03:17:57

您不能在主线程之外创建或访问任何QWidget

您可以使用信号和插槽间接地从其他线程访问小部件:

代码语言:javascript
复制
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, time

class BusyBar(QThread):                     # Looping progress bar
    # create the signal that the thread will emit
    changeValue = pyqtSignal(int)
    def __init__(self, text = "" ):
        QThread.__init__(self)
        self.text = text
        self.stop = False
        self.proBar = QProgressBar()
        self.proBar.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.SplashScreen )
        self.proBar.setRange( 0, 100 )
        self.proBar.setTextVisible( True )
        self.proBar.setFormat( self.text )
        self.proBar.setValue( 0 )
        self.proBar.setFixedSize( 500 , 50 )
        self.proBar.setAlignment(Qt.AlignCenter)
        self.proBar.show()

        self.changeValue.connect(self.proBar.setValue, Qt.QueuedConnection)
        # Make the Busybar delete itself and the QProgressBar when done        
        self.finished.connect(self.onFinished)

    def run( self ):
        while not self.stop:                # keep looping while self is visible
            # Loop sending mail 
            for i in range(100):
                # emit the signal instead of calling setValue
                # also we can't read the progress bar value from the thread
                self.changeValue.emit( i )
                time.sleep(0.05)
            self.changeValue.emit( 0 )

    def onFinished(self):
        self.proBar.deleteLater()
        self.deleteLater()

    def Kill(self):
        self.stop = True

class LayoutCreator(QDialog):
    def __init__(self , parent=None):
        super(LayoutCreator, self).__init__(parent)
        self.Cameras_Update()

    def Cameras_Update( self ):                                       
        # Looping progress bar 
        self.busyBar = BusyBar( text = "Gathering Camera Data" )
        self.busyBar.start()

        # loop through folder structure storing data

        # Simulate async activity that doesn't block the GUI event loop
        # (if you do everything without giving control back to 
        # the event loop, you have to call QApplication.processEvents()
        # to allow the GUI to update itself )
        QTimer.singleShot(10000, self.stopBar)

    def stopBar(self):
        self.busyBar.Kill()                        # Close looping progress bar    

app = QApplication(sys.argv)
win = LayoutCreator()
win.show();
sys.exit(app.exec_())

如果您只想要一个繁忙的指示符,您可以简单地将QProgressBar的最小值和最大值设置为0,并且您不需要一个线程,如文档中所示。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8007602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档