[Resolved] How can I set the focus on my application at startup

  • Hi There,

    I am trying to start an app on linux (or even windows) that when run should have focus and hide the mouse. My program does this ok until I hide the mouse... when I hide the mouse the app no longer gets focus!

    Here is the code:

    #include <QApplication>
    #include <QThread>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    // Create the app
    QApplication app(argc, argv);

    //app.setOverrideCursor( QCursor( Qt::BlankCursor ) );
    // Create main window
    MainWindow *window = new MainWindow;
    // Set up the UI and execute the App
    // tidy up
    return 0;


    You can see my comment where if I un-comment my code to hide the mouse then when the app starts it does not have focus....

    What do I need to do here?

  • What if you just call the next code in your MainWindow constructor:

    I've just tested it and It worked fine, with no focus lost. Have you tried this already? :)

  • Hi There,

    Yes, I did try something like this, but it did not work for me in all cases. It works if I make a PC app and run it on the PC, perhaps as you have done as well, but it somehow fails to work on my embedded touch screen app.

    I am starting to figure that it may be a difference of the OS/Touchscreen.

    I ended up having to use:
    #include <QWSServer>
    QApplication app(argc, argv);
    in main.cpp just after my app is created. I wanted to avoid bringing in more libraries, but I was un-able to get to the route of the cause :(

    Still, this works nicely, seems to be a "more powerful" way of controlling the mouse (and other QWS things which I have not yet looked at).

  • I hope it solved your problem. Anyway, did you check the "Paint Touch" (or something like that) demo that comes with Qt 4.81? The're basically implementig a classic Paint for touch devices, I'm pretty sure there was no mouse in there, so you may want to look at it.

Log in to reply