Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Real-time value display



  • 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
    

  • Lifetime Qt Champion

    @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.



  • @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?

  • Lifetime Qt Champion

    @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.


Log in to reply