Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Moved] How to use QProcess in a loop?

    Language Bindings
    4
    10
    5836
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      ManasQt last edited by

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

      thanks,

      1 Reply Last reply Reply Quote 0
      • A
        andre last edited by

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

        1 Reply Last reply Reply Quote 0
        • M
          ManasQt last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • T
            tobias.hunger last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • M
              ManasQt last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

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

                1 Reply Last reply Reply Quote 0
                • M
                  ManasQt last edited by

                  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)
                  

                  @

                  1 Reply Last reply Reply Quote 0
                  • G
                    goetz last edited by

                    Moved to the language bindings forum.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply Reply Quote 0
                    • M
                      ManasQt last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • G
                        goetz last edited by

                        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.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post