PDA

View Full Version : QProcess and focus problem



ramazangirgin
20th June 2011, 12:44
Hi,
I am using QProcess for starting cmd.exe application with /c "FilePath" parameter. With cmd.exe i show file to user. But file opened behind the other windows and has no focus.
How can i set focus to opened file with QProcess?
Thanks

stampede
22nd June 2011, 23:07
QProcess can't know that you have used it to open some kind of window, don't expect that it'll have an api related to specific applications.
I see two workarounds. First, easy and unreliable, is to call FindWindow (http://msdn.microsoft.com/en-us/library/ms633499(v=vs.85).aspx) winapi procedure, and use returned window handle to bring the window to the front with ShowWindow (http://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx), so it'll look like:


HWND handle = FindWindow(0,windowTitle); // i'm not sure what will be the title, probably "Document.txt - Notepad" or something
if( ShowWindow(handle,SW_SHOW) ){ qDebug() << "hurray"; }

Second, more reliable, is to enumerate all opened windows, for each of them check the process id of process which opened the window, and use pid() of the QProcess for comparison. If the window pid equals notepad QProcess pid, you can call ShowWindow, passing the window handle like above:
1) EnumWindows (http://msdn.microsoft.com/en-us/library/ms633497(v=vs.85).aspx) - for "looping" through all opened windows (here you'll get HWND of window)
2) GetWindowThreadProcessId (http://msdn.microsoft.com/en-us/library/ms633522(v=vs.85).aspx) - for getting process id of window
3) PROCESS_INFORMATION (http://msdn.microsoft.com/en-us/library/ms684873(v=vs.85).aspx) structure - this you'll get with QProcess:: pid() (you want to use dwProcessId member)
4) ShowWindow (http://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx) - call it using HWND from 1) if pid from 2) equals pid from 3)
Simple, isn't it ?

gorio
15th July 2011, 16:21
Hello,

i'm trying to call a process and i'd like to set focus in this process.

My Source is:
QString program = "/opt/sygic/Drive/Maemo/drive -cwd=/home/user/MyDocs/Drive/Maemo -rfull";
QProcess *myProcess = new QProcess(this);
myProcess->start(program);
myProcess->waitForFinished();

stampede
15th July 2011, 17:47
... and what have you tried so far ?

gorio
15th July 2011, 18:19
I'm new Qt developer and i'd like to call a process in my QDialog form.
I have a button in this form that calls this SLOT when the button is pressed.

My goal is set the focus in this new process, but i can't to do it..
Now, i'm researching about Handle window, but i don't know if it is the solution for this issue.

stampede
15th July 2011, 18:52
Post #2 is about windows, what is your target platform ?
@down: Of course ;) sorry, I can't help you in that case, maybe someone else knows how to do that.

gorio
15th July 2011, 19:05
linux - maemo

gorio
18th July 2011, 18:50
linux - maemo

Could someone help me how to do it for Linux - Maemo ?


HWND handle = FindWindow(0,windowTitle); // i'm not sure what will be the title, probably "Document.txt - Notepad" or something
if( ShowWindow(handle,SW_SHOW) ){ qDebug() << "hurray"; }