Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to specify a file path from QFileDialog as another argument in QProcess?
Forum Updated to NodeBB v4.3 + New Features

How to specify a file path from QFileDialog as another argument in QProcess?

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpython
3 Posts 2 Posters 722 Views
  • 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.
  • D Offline
    D Offline
    Dara1
    wrote on 15 May 2024, 12:38 last edited by
    #1

    I'm fairly new to PyQt and have been struggling with this problem for some time.

    I would like to have a possibility to start and stop an imported file that contains a running loop (in fact, it contains a Python “threading” inside a while-loop). This imported file in turn requires input data/files. Here is a reproducible code:

    from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QPlainTextEdit,QVBoxLayout, QWidget, QProgressBar, QFileDialog)
    from PyQt5.QtCore import QProcess
    import sys
    import re
    
    progress_re = re.compile("Total complete: (\d+)%")
    
    def simple_percent_parser(output):
        m = progress_re.search(output)
        if m:
            pc_complete = m.group(1)
            return int(pc_complete)
    
    
    class MainWindow(QMainWindow):
    
        def __init__(self):
            super().__init__()
    
            self.p = None
    
            self.btn = QPushButton("Execute")
            self.btn.pressed.connect(self.start_process)
    
            self.btn1 = QPushButton("Stop")
            self.btn1.pressed.connect(self.stop_process)
    
            self.dButton = QPushButton("Select a File")
            self.dButton.clicked.connect(self.getfile)
    
    
            self.text = QPlainTextEdit()
            self.text.setReadOnly(True)
    
            self.progress = QProgressBar()
            self.progress.setRange(0, 100)
    
            l = QVBoxLayout()
            l.addWidget(self.btn)
            l.addWidget(self.btn1)
            l.addWidget(self.dButton)
            l.addWidget(self.text)
    
            w = QWidget()
            w.setLayout(l)
    
            self.setCentralWidget(w)
    
        def message(self, s):
            self.text.appendPlainText(s)
    
        def start_process(self):
            if self.p is None:
                self.message("Executing process")
                self.p = QProcess()
                self.p.readyReadStandardOutput.connect(self.handle_stdout)
                self.p.readyReadStandardError.connect(self.handle_stderr)
                self.p.finished.connect(self.process_finished)
                self.p.start("python", ['dummy_script2.py'])
    
        def handle_stderr(self):
            data = self.p.readAllStandardError()
            stderr = bytes(data).decode("utf8")
            progress = simple_percent_parser(stderr)
            if progress:
                self.progress.setValue(progress)
            self.message(stderr)
    
        def handle_stdout(self):
            data = self.p.readAllStandardOutput()
            stdout = bytes(data).decode("utf8")
            self.message(stdout)
    
        def process_finished(self):
            self.message("Process finished.")
            self.p = None
    
        def stop_process(self):
            self.message("Process finished.")
            self.p = None
    
    
        def getfile(self):
            # global fileName
            filename = QFileDialog.getOpenFileName()
            fileName = filename[0]
            return fileName
    
    
    app = QApplication(sys.argv)
    
    w = MainWindow()
    w.show()
    
    app.exec_()
    

    dummy_script2.py:

    import time 
    
    
    def run(filePath):
        count =0
        while True:
            count = count + 1
            print(count)
            print(filePath)
            time.sleep(1)
    
    
    run(filePath)
    

    So, I have no problem running the imported file (if it doesn't require input) and getting the file path from QFileDialog. But I have a problem passing the path to that file. I will appreciate any tips and help!

    J 1 Reply Last reply 15 May 2024, 12:49
    0
    • D Dara1
      15 May 2024, 12:38

      I'm fairly new to PyQt and have been struggling with this problem for some time.

      I would like to have a possibility to start and stop an imported file that contains a running loop (in fact, it contains a Python “threading” inside a while-loop). This imported file in turn requires input data/files. Here is a reproducible code:

      from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QPlainTextEdit,QVBoxLayout, QWidget, QProgressBar, QFileDialog)
      from PyQt5.QtCore import QProcess
      import sys
      import re
      
      progress_re = re.compile("Total complete: (\d+)%")
      
      def simple_percent_parser(output):
          m = progress_re.search(output)
          if m:
              pc_complete = m.group(1)
              return int(pc_complete)
      
      
      class MainWindow(QMainWindow):
      
          def __init__(self):
              super().__init__()
      
              self.p = None
      
              self.btn = QPushButton("Execute")
              self.btn.pressed.connect(self.start_process)
      
              self.btn1 = QPushButton("Stop")
              self.btn1.pressed.connect(self.stop_process)
      
              self.dButton = QPushButton("Select a File")
              self.dButton.clicked.connect(self.getfile)
      
      
              self.text = QPlainTextEdit()
              self.text.setReadOnly(True)
      
              self.progress = QProgressBar()
              self.progress.setRange(0, 100)
      
              l = QVBoxLayout()
              l.addWidget(self.btn)
              l.addWidget(self.btn1)
              l.addWidget(self.dButton)
              l.addWidget(self.text)
      
              w = QWidget()
              w.setLayout(l)
      
              self.setCentralWidget(w)
      
          def message(self, s):
              self.text.appendPlainText(s)
      
          def start_process(self):
              if self.p is None:
                  self.message("Executing process")
                  self.p = QProcess()
                  self.p.readyReadStandardOutput.connect(self.handle_stdout)
                  self.p.readyReadStandardError.connect(self.handle_stderr)
                  self.p.finished.connect(self.process_finished)
                  self.p.start("python", ['dummy_script2.py'])
      
          def handle_stderr(self):
              data = self.p.readAllStandardError()
              stderr = bytes(data).decode("utf8")
              progress = simple_percent_parser(stderr)
              if progress:
                  self.progress.setValue(progress)
              self.message(stderr)
      
          def handle_stdout(self):
              data = self.p.readAllStandardOutput()
              stdout = bytes(data).decode("utf8")
              self.message(stdout)
      
          def process_finished(self):
              self.message("Process finished.")
              self.p = None
      
          def stop_process(self):
              self.message("Process finished.")
              self.p = None
      
      
          def getfile(self):
              # global fileName
              filename = QFileDialog.getOpenFileName()
              fileName = filename[0]
              return fileName
      
      
      app = QApplication(sys.argv)
      
      w = MainWindow()
      w.show()
      
      app.exec_()
      

      dummy_script2.py:

      import time 
      
      
      def run(filePath):
          count =0
          while True:
              count = count + 1
              print(count)
              print(filePath)
              time.sleep(1)
      
      
      run(filePath)
      

      So, I have no problem running the imported file (if it doesn't require input) and getting the file path from QFileDialog. But I have a problem passing the path to that file. I will appreciate any tips and help!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 May 2024, 12:49 last edited by
      #2

      @Dara1 said in How to specify a file path from QFileDialog as another argument in QProcess?:

      self.p.start("python", ['dummy_script2.py'])

      params = ['dummy_script2.py', 'PATH_TO_THE_FILE']
      self.p.start("python", params)
      
      import time 
      import sys
      
      def run(filePath):
          count =0
          while True:
              count = count + 1
              print(count)
              print(filePath)
              time.sleep(1)
      
      
      run(sys.argv[1])
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 15 May 2024, 12:59
      2
      • J jsulm
        15 May 2024, 12:49

        @Dara1 said in How to specify a file path from QFileDialog as another argument in QProcess?:

        self.p.start("python", ['dummy_script2.py'])

        params = ['dummy_script2.py', 'PATH_TO_THE_FILE']
        self.p.start("python", params)
        
        import time 
        import sys
        
        def run(filePath):
            count =0
            while True:
                count = count + 1
                print(count)
                print(filePath)
                time.sleep(1)
        
        
        run(sys.argv[1])
        
        D Offline
        D Offline
        Dara1
        wrote on 15 May 2024, 12:59 last edited by
        #3

        @jsulm thank you so much! You solved my problem :)

        1 Reply Last reply
        0
        • D Dara1 has marked this topic as solved on 15 May 2024, 14:08

        1/3

        15 May 2024, 12:38

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved