Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Real-time value display
Forum Updated to NodeBB v4.3 + New Features

Real-time value display

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 205 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.
  • K Offline
    K Offline
    KOHAK
    wrote on last edited by
    #1
    import sys
    
    # On Windows it looks like cp850 is used for my console. We need it to decode the QByteArray correctly.
    # Based on https://forum.qt.io/topic/85064/qbytearray-to-string/2
    import ctypes
    CP_console = "cp" + str(ctypes.cdll.kernel32.GetConsoleOutputCP())
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class gui(QtWidgets.QMainWindow):
        def __init__(self):
            super(gui, self).__init__()
            self.initUI()
    
        def dataReady(self):
            cursor = self.output.textCursor()
            cursor.movePosition(cursor.End)
    
            # Here we have to decode the QByteArray
            cursor.insertText(str(self.process.readAll().data().decode(CP_console)))
            self.output.ensureCursorVisible()
    
        def callProgram(self):
            # run the process
            # `start` takes the exec and a list of arguments
            self.process.start('ipconfig')
    
        def initUI(self):
            # Layout are better for placing widgets
            layout = QtWidgets.QVBoxLayout()
            self.runButton = QtWidgets.QPushButton('Run')
            self.runButton.clicked.connect(self.callProgram)
    
            self.output = QtWidgets.QTextEdit()
    
            layout.addWidget(self.output)
            layout.addWidget(self.runButton)
    
            centralWidget = QtWidgets.QWidget()
            centralWidget.setLayout(layout)
            self.setCentralWidget(centralWidget)
    
            # QProcess object for external app
            self.process = QtCore.QProcess(self)
            # QProcess emits `readyRead` when there is data to be read
            self.process.readyRead.connect(self.dataReady)
    
            # Just to prevent accidentally running multiple times
            # Disable the button when process starts, and enable it when it finishes
            self.process.started.connect(lambda: self.runButton.setEnabled(False))
            self.process.finished.connect(lambda: self.runButton.setEnabled(True))
    
    
    #Function Main Start
    def main():
        app = QtWidgets.QApplication(sys.argv)
        ui=gui()
        ui.show()
        sys.exit(app.exec_())
    #Function Main END
    
    if __name__ == '__main__':
        main()
    

    The above is a code found on the Internet.
    I want to change the ping command in that content to the code that runs Java below. Can you help me?

    java -Xms16G -Xmx16G -jar spigot.jar
    
    jsulmJ 1 Reply Last reply
    0
    • K KOHAK
      import sys
      
      # On Windows it looks like cp850 is used for my console. We need it to decode the QByteArray correctly.
      # Based on https://forum.qt.io/topic/85064/qbytearray-to-string/2
      import ctypes
      CP_console = "cp" + str(ctypes.cdll.kernel32.GetConsoleOutputCP())
      
      from PyQt5 import QtCore, QtGui, QtWidgets
      
      class gui(QtWidgets.QMainWindow):
          def __init__(self):
              super(gui, self).__init__()
              self.initUI()
      
          def dataReady(self):
              cursor = self.output.textCursor()
              cursor.movePosition(cursor.End)
      
              # Here we have to decode the QByteArray
              cursor.insertText(str(self.process.readAll().data().decode(CP_console)))
              self.output.ensureCursorVisible()
      
          def callProgram(self):
              # run the process
              # `start` takes the exec and a list of arguments
              self.process.start('ipconfig')
      
          def initUI(self):
              # Layout are better for placing widgets
              layout = QtWidgets.QVBoxLayout()
              self.runButton = QtWidgets.QPushButton('Run')
              self.runButton.clicked.connect(self.callProgram)
      
              self.output = QtWidgets.QTextEdit()
      
              layout.addWidget(self.output)
              layout.addWidget(self.runButton)
      
              centralWidget = QtWidgets.QWidget()
              centralWidget.setLayout(layout)
              self.setCentralWidget(centralWidget)
      
              # QProcess object for external app
              self.process = QtCore.QProcess(self)
              # QProcess emits `readyRead` when there is data to be read
              self.process.readyRead.connect(self.dataReady)
      
              # Just to prevent accidentally running multiple times
              # Disable the button when process starts, and enable it when it finishes
              self.process.started.connect(lambda: self.runButton.setEnabled(False))
              self.process.finished.connect(lambda: self.runButton.setEnabled(True))
      
      
      #Function Main Start
      def main():
          app = QtWidgets.QApplication(sys.argv)
          ui=gui()
          ui.show()
          sys.exit(app.exec_())
      #Function Main END
      
      if __name__ == '__main__':
          main()
      

      The above is a code found on the Internet.
      I want to change the ping command in that content to the code that runs Java below. Can you help me?

      java -Xms16G -Xmx16G -jar spigot.jar
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @KOHAK said in Real-time value display:

      Can you help me?

      Did you read https://doc.qt.io/qt-5/qprocess.html ? There is also an example. Else please say what exactly is the problem.

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

      K 1 Reply Last reply
      2
      • jsulmJ jsulm

        @KOHAK said in Real-time value display:

        Can you help me?

        Did you read https://doc.qt.io/qt-5/qprocess.html ? There is also an example. Else please say what exactly is the problem.

        K Offline
        K Offline
        KOHAK
        wrote on last edited by
        #3

        @jsulm ```
        import sys

        On Windows it looks like cp850 is used for my console. We need it to decode the QByteArray correctly.

        Based on https://forum.qt.io/topic/85064/qbytearray-to-string/2

        import ctypes
        CP_console = "cp" + str(ctypes.cdll.kernel32.GetConsoleOutputCP())

        from PyQt5 import QtCore, QtGui, QtWidgets

        class gui(QtWidgets.QMainWindow):
        def init(self):
        super(gui, self).init()
        self.initUI()

        def dataReady(self):
            cursor = self.output.textCursor()
            cursor.movePosition(cursor.End)
        
            # Here we have to decode the QByteArray
            cursor.insertText(str(self.process.readAll().data().decode(CP_console)))
            self.output.ensureCursorVisible()
        
        def callProgram(self):
            # run the process
            # `start` takes the exec and a list of arguments
            self.process.start('java -Xms16G -Xmx16G -jar spigot.jar')
        
        def initUI(self):
            # Layout are better for placing widgets
            layout = QtWidgets.QVBoxLayout()
            self.runButton = QtWidgets.QPushButton('Run')
            self.runButton.clicked.connect(self.callProgram)
        
            self.output = QtWidgets.QTextEdit()
        
            layout.addWidget(self.output)
            layout.addWidget(self.runButton)
        
            centralWidget = QtWidgets.QWidget()
            centralWidget.setLayout(layout)
            self.setCentralWidget(centralWidget)
        
            # QProcess object for external app
            self.process = QtCore.QProcess(self)
            # QProcess emits `readyRead` when there is data to be read
            self.process.readyRead.connect(self.dataReady)
        
            # Just to prevent accidentally running multiple times
            # Disable the button when process starts, and enable it when it finishes
            self.process.started.connect(lambda: self.runButton.setEnabled(False))
            self.process.finished.connect(lambda: self.runButton.setEnabled(True))
        

        #Function Main Start
        def main():
        app = QtWidgets.QApplication(sys.argv)
        ui=gui()
        ui.show()
        sys.exit(app.exec_())
        #Function Main END

        if name == 'main':
        main()

        
        This code is not working Is it possible to resolve it?
        jsulmJ 1 Reply Last reply
        0
        • K KOHAK

          @jsulm ```
          import sys

          On Windows it looks like cp850 is used for my console. We need it to decode the QByteArray correctly.

          Based on https://forum.qt.io/topic/85064/qbytearray-to-string/2

          import ctypes
          CP_console = "cp" + str(ctypes.cdll.kernel32.GetConsoleOutputCP())

          from PyQt5 import QtCore, QtGui, QtWidgets

          class gui(QtWidgets.QMainWindow):
          def init(self):
          super(gui, self).init()
          self.initUI()

          def dataReady(self):
              cursor = self.output.textCursor()
              cursor.movePosition(cursor.End)
          
              # Here we have to decode the QByteArray
              cursor.insertText(str(self.process.readAll().data().decode(CP_console)))
              self.output.ensureCursorVisible()
          
          def callProgram(self):
              # run the process
              # `start` takes the exec and a list of arguments
              self.process.start('java -Xms16G -Xmx16G -jar spigot.jar')
          
          def initUI(self):
              # Layout are better for placing widgets
              layout = QtWidgets.QVBoxLayout()
              self.runButton = QtWidgets.QPushButton('Run')
              self.runButton.clicked.connect(self.callProgram)
          
              self.output = QtWidgets.QTextEdit()
          
              layout.addWidget(self.output)
              layout.addWidget(self.runButton)
          
              centralWidget = QtWidgets.QWidget()
              centralWidget.setLayout(layout)
              self.setCentralWidget(centralWidget)
          
              # QProcess object for external app
              self.process = QtCore.QProcess(self)
              # QProcess emits `readyRead` when there is data to be read
              self.process.readyRead.connect(self.dataReady)
          
              # Just to prevent accidentally running multiple times
              # Disable the button when process starts, and enable it when it finishes
              self.process.started.connect(lambda: self.runButton.setEnabled(False))
              self.process.finished.connect(lambda: self.runButton.setEnabled(True))
          

          #Function Main Start
          def main():
          app = QtWidgets.QApplication(sys.argv)
          ui=gui()
          ui.show()
          sys.exit(app.exec_())
          #Function Main END

          if name == 'main':
          main()

          
          This code is not working Is it possible to resolve it?
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @KOHAK said in Real-time value display:

          self.process.start('java -Xms16G -Xmx16G -jar spigot.jar')

          That should be

          self.process.start('java', ['-Xms16G', '-Xmx16G', '-jar', 'spigot.jar'])
          

          Please read documentation to do it correctly.

          Add error handling to your code to see what exactly happens, else only guessing is possible.
          See https://doc.qt.io/qt-5/qprocess.html#errorOccurred

          And I guess you have to provide absolute path to the java executable.

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

          1 Reply Last reply
          3

          • Login

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