1

I am starting a QProcess and would like to bring the resulting window on top everytime a command is written to QProcess.

However, if my process is "gnuplot.exe" (Win7) the plot window will be updated but always stays behind the PyQt window. I haven't yet found a way to bring it to front, or make it active, or put the focus on, or however you would call it. Something like self.process. ... raise(), show(), activateWindow(), SetForegroundWindow, WindowStaysOnTopHint ... ???

These posts didn't help me further

How to find the active PyQt window and bring it to the front

https://forum.qt.io/topic/30018/solved-bring-to-front-window-application-managed-with-qprocess/3

Bring QProcess window to front (running Qt Assistant)

Here is the code:

import sys
from PyQt5.QtCore import QProcess, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton 
import subprocess

class MyWindow(QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()

        self.setGeometry(100,100,500,500)
        self.button1 = QPushButton('New plot',self)
        self.button1.clicked.connect(self.button1_clicked)
        self.process = QProcess(self)
        self.process.start(r'C:\Programs\gnuplot\bin\gnuplot.exe', ["-p"])
        self.n = 1

    def button1_clicked(self):
        plotcmd = "plot x**"+str(self.n)+"\n"
        self.process.write(plotcmd.encode())
        self.n += 1
        response = self.process.readAllStandardOutput()
        print(str(response, encoding="utf-8"))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("plastique")
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
theozh
  • 22,244
  • 5
  • 28
  • 72
  • 1
    Unfortunately, this is going to be system-specific. See https://stackoverflow.com/a/2091530/1124661 for a discussion of how its done in Windows. Also see https://stackoverflow.com/a/17273638/1124661 for how to find a window using process id. You could use `QProcess::processId()` to get that. – buck54321 Jul 31 '18 at 21:16

1 Answers1

1

Based on the comment and links from @buck54321, I got to the following version which seems to (almost) work. The solution in the above links seem to be more general and a bit too complicated for my case, since I know the exact name of my window which I want to bring to front. To my original code I just had to add three lines. I was not familiar with win32gui.

However, with my code, only after the button is pressed the second time the window will be brought to front. After the first button press hwnd is still 0 which leads to a crash. I guess this is because gnuplot.exe will start without opening a window, and opens a window only after the first command has been sent. If anybody can show me how to bring the window to front even after the first button click that would be great.

If there are limitation or improvements I would be happy to learn about them.

Here is the code:

import sys
from PyQt5.QtCore import QProcess, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton 
import subprocess
import win32gui     ####### new line

class MyWindow(QWidget):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setGeometry(100,100,500,500)
        self.button1 = QPushButton('New plot',self)
        self.button1.clicked.connect(self.button1_clicked)
        self.process = QProcess(self)
        self.process.start(r'C:\Programs\gnuplot\bin\gnuplot.exe', ["-p"])
        self.n = 1

    def button1_clicked(self):
        plotcmd = "set term wxt 999\n plot x**"+str(self.n)+"\n"
        self.process.write(plotcmd.encode())
        self.n += 1
        hwnd = win32gui.FindWindow(None, "Gnuplot (window id : 999)")  ####### new line
        response = self.process.readAllStandardOutput()
        print(str(response, encoding="utf-8"))
        if hwnd != 0: win32gui.SetForegroundWindow(hwnd)   ####### new line

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("plastique")
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
theozh
  • 22,244
  • 5
  • 28
  • 72