Results 1 to 8 of 8

Thread: QProcess and focus problem

  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QProcess and focus problem

    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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess and focus problem

    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 winapi procedure, and use returned window handle to bring the window to the front with ShowWindow, so it'll look like:
    Qt Code:
    1. HWND handle = FindWindow(0,windowTitle); // i'm not sure what will be the title, probably "Document.txt - Notepad" or something
    2. if( ShowWindow(handle,SW_SHOW) ){ qDebug() << "hurray"; }
    To copy to clipboard, switch view to plain text mode 
    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 - for "looping" through all opened windows (here you'll get HWND of window)
    2) GetWindowThreadProcessId - for getting process id of window
    3) PROCESS_INFORMATION structure - this you'll get with QProcess:: pid() (you want to use dwProcessId member)
    4) ShowWindow - call it using HWND from 1) if pid from 2) equals pid from 3)
    Simple, isn't it ?

  3. #3
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess and focus problem

    ... and what have you tried so far ?

  5. #5
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QProcess and focus problem

    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.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QProcess and focus problem

    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.
    Last edited by stampede; 15th July 2011 at 20:08.

  7. #7
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QProcess and focus problem

    linux - maemo

  8. #8
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: QProcess and focus problem

    Quote Originally Posted by gorio View Post
    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"; }

Similar Threads

  1. Focus Out problem
    By Chirag in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2011, 07:32
  2. Problem of Focus QMacNativeWidget
    By skuallpa in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2010, 15:19
  3. Focus problem
    By waynew in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2010, 02:24
  4. QProcess, how to give focus to the new process on Mac
    By simlab in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2010, 00:27
  5. QGraphicsTextItem focus problem
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2007, 18:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.