[Moved] How to use QProcess in a loop?



  • Hi All,
    How to use QProcess in a loop and update QProgressBar in Main thread?

    thanks,



  • Don't. Just use the asynchronic API of QProcess (using signals & slots) and you do not need a loop.



  • Hi Andre,

         I have designed a Custom QDialog which has QProgessBar in StatusBar.
    

    I am processing some process which performs some file conversions. Here i have 100s of files to convert and while conversion i have to update the progressbar simultaneously.
    please give some sample code which deals with this kind of case.


  • Moderators

    That sounds like a good job for "Qt Concurrent":http://doc.qt.nokia.com/4.7-snapshot/threads-qtconcurrent.html:-) Check the documentation and examples that come with it.



  • Hi Tobias Hunger,
    well i am using PySide so the one you are suggesting is not available.It would be great if you give me an alternative way to achieve this.

    In My case,

    i have a custom QDialog with 3 QPushButtons-Convert,Cancel,Finished and QProgressBar,QThread,QProcess

    I am running QProcess inside QThread. and created a signal which sends the progressbar value to Main Thread.
    here i am getting an Error: Cannot create children for a parent that is in a different thread

    the 'Convert Button' does the bulk file conversions. So when user clicks on it,
    i am running QThread.

    please help me.



  • Did you read the "wiki article":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects on threading?



  • Hi All,
    Please tell me why i am not able to update progressbar without freezing UI
    @import sys,os

        self.thread=Thread()
        self.thread.setFiles(self.fileNames,self.exe_path)
        self.connect(self.thread,QtCore.SIGNAL("setProgressValue(int)"),self.updateProgressBar)
        self.connect(self.thread,QtCore.SIGNAL("updateLogInfo(QString)"),self.updateLogInfo)
        self.connect(self.thread, QtCore.SIGNAL("finished()"), self.threadFinished)
        self.connect(self.thread, QtCore.SIGNAL("terminated()"), self.threadTerminated)
        self.thread.start()
    

    class Thread(QtCore.QThread):
    setProgressValue=QtCore.Signal(int)
    updateLogInfo=QtCore.Signal(str)
    def init(self,parent=None):
    QtCore.QThread.init(self,parent)
    self.moveToThread(self)
    def setFiles(self,files,exe_path):
    print "setFiles"
    self.files=files
    self.exe_path=exe_path

    def startProcessing(self):
        self.count=0
        self.process=QtCore.QProcess()
        self.process.setReadChannel(QtCore.QProcess.StandardOutput)
        self.process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
        self.connect(self.process,QtCore.SIGNAL("readyReadStandardOutput()"),
        self,QtCore.SLOT("readyReadOutput()"))
        #self.connect(self.process,QtCore.SIGNAL("readyRead()"),
        self,QtCore.SLOT("readyRead()"))
        self.connect(self.process,QtCore.SIGNAL("readyReadStandardError()"),
        self,QtCore.SLOT("readyReadErrorOutput()"))
        self.connect(self.process,QtCore.SIGNAL("finished(int)"),self,QtCore.SLOT("processFinished(int)"))
        self.process.error.connect( self.processError )
        
        for filename in self.files:
            arguments=[]
            arguments.append(filename)
            self.process.start(self.exe_path,arguments)
                
            if self.process.waitForStarted():
                self.process.waitForBytesWritten()
                if not self.process.waitForFinished():
                    print "exe failed:",self.process.errorString()
    def run(self):
        QtCore.QTimer.singleShot(0,self,QtCore.SLOT("startProcessing()"))
        self.exec_()
    
    def readyRead(self):
        if not self.process:
            return 
        print "readyRead"
        result=self.process.readAllStandardOutput()
        self.updateLogData(str(result.data()))
        
    def readyReadOutput(self):
        if not self.process:
            return 
        print "readyReadOutput"
        result=self.process.readAllStandardOutput()
        self.updateLogData(str(result.data()))
        
    def readyReadErrorOutput(self):
        if not self.process:
            return 
        print "readyReadErrorOutput"
        error=self.process.readAllStandardError()
        self.updateLogData(str(error.data()))
                
    def processFinished(self,exitCode):
        if self.process.exitStatus()==QtCore.QProcess.ExitStatus.CrashExit:
            self.updateLogData(self.process.errorString())
        print "processFinished"
        self.updateProgressBarValue(self.count)
        
    def processError(self):
        if not self.process:
            return 
        print "processError"
        self.updateLogData(self.process.errorString())
        
    def updateProgressBarValue(self,value):
        self.emit(QtCore.SIGNAL("setProgressValue(int)"),value)
        
    def updateLogData(self,data):
        self.emit(QtCore.SIGNAL("updateLogInfo(QString)"),data)
    

    @



  • Moved to the language bindings forum.



  • Where is "Moved to the language bindings forum." link please!



  • I moved this thread to the mentioned forum. You find the link at the top of the page in the bread crumbs just above the thread title.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.