Qt World Summit: Submit your Presentation

Give focus to dialog invoked from outside of the application

  • I have a hardware controller which can send events to my application. Now when I press the button, a dialog box shows up with some input fields. I want to give keyboard focus to the dialog so that the user can enter data without having to activate the dialog box by clicking it. I have tried several things including different Qt flags but it doesn't seem to work on Windows. The dialog box just flashes in the taskbar. I have tried native windows calls as well to no luck. Any help will be appreciated.

  • Qt Champions 2017

    Quick check. Have tried windowsontophint or modality flags ?

  • @dheerendra I am using these flags Qt::WindowFlags flags = Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;. I have tried modality too but doesn't seem to work.

  • If you are getting events from an external source I don't see why something like setFocus() wouldn't work. You might need to do a few things prior to this so that the setFocus() command isn't ignored (the dialog must be visible and not minimized for example).

    I think playing around with the window modal flags is probably not the best idea. I don't know about you but I hate it when a dialog pop's up ontop of everything else and all you can do is move it off to one side of the screen (or close it).

  • @Rondog Could you provide me with some more information about what should I be doing to get setFocus to work properly. I am showing the dialog before hand and it's not minimized.

    I agree with you about the dialogs popping up, but this is related to physical controller which shows some feedback when pressed and gives you ability to enter info. So I think it might not be as obstructing since it's being shown when the user actually expects it.

  • I have a program I ran a test with. It uses a named pipe that a second program can send data through (the original idea was to synchronize programs). I modified it so that the program receiving the data automatically set focus on one of the QLineEdit's. Basically trying to recreate what you described (I hope) except I am not working in the QEvents.

    void TMyDialog::Make_This_In_Focus(void)
    	if(this->isHidden())	// must be visible
    	if(this->isMinimized())	// cannot be minimized
    	d_measurement_description_edit->setFocus();	// QLineEdit that I want to focus on
    	this->raise();	// move this widget to the top of the Z order.  This sets the widget 'in focus' or active.

    I didn't try any of this on Windows but I don't see why it wouldn't work.

  • @Rondog Thanks for this. One last issue with this will be that when I call raise() it brings the other application windows on the top as well. Is there a way to not do that and just bring the dialog in focus while not making other app window active?

  • I took a second look at the documentation. Maybe 'activateWindow()' is a better option than 'raise()' (?).

    I am not clear on what you mean by your comment ("...other application windows on top as well..."). The idea is to move the window of interest to the top of the Z order so all other windows must fall behind it. I can't picture this bringing other application windows to the top along with the one you want to have active. I probably don't understand what you are describing.

    If the other window that moves to the top is the one that that creates the signal for the target window (the one you want to have focus) then how are they connected together? When I tried my test I was using a named pipe so the programs were very separate and it worked well.

  • @Rondog Sorry for the confusion. I have a main application window and then I have this dialog which I want to show/give focus to. After call to raise() is made, both the application window and the dialog come to the top, i.e., in front of other apps with the focus given to the dialog. I am looking for a way to just raise the dialog box and keep the application where it was in the z-order. Hope this explains it.

  • Hmmm. Got it.

    I am not sure if this is possible. If the dialog was created as a child of the parent window the two might always be attached to each other. If you can get the dialog to be on top with other programs between the dialog and the main window (i.e. Z order = dialog, something_else, something_else, main_window) then it might be possible. If you can't do this by using some method like the mouse then you won't be able to do this using setFocus().

    If the dialog was not a child of the other window this might help (but can create new problems).

Log in to reply