Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid application activation and focus in when clicking buttons on it - Windows API or Qt

Situation: A border-less QDialog stays successfully on top of other applications.

The problem is when clicking on this always-on-top application window, the following occurs:

  • The clicked always-on-top application gets activated
  • The clicked always-on-top application window steals the focus of previous active/focused app

Is there a possibility that when clicking on this always-on-top inactive and unfocused application window,

  • the current application does not loose activation and focus
  • while user being still able to interact with the always-on-top application (hitting buttons or drop-down menus, dragging the window)?

I'm working with Qt but there's no problem about using native Windows API.

I tried the following Qt windowFlag:

  • Qt::WindowDoesNotAcceptFocus but it does not work: the always-on-top application is activated, focused.
  • Qt::WindowTransparentForInput, the always-on-top application is really transparent to clicks: not activated and not focused in but buttons are unfortunately not triggered when hit.
like image 748
Derek Avatar asked Sep 06 '13 16:09

Derek


2 Answers

It is possible to make a window unactivable and unfocusable when clicking on it by using Windows flags (#include <qt_windows.h>). The following has to be used after the window is created and shown:

HWND winHandle = (HWND)winId();
ShowWindow(winHandle, SW_HIDE);
SetWindowLong(winHandle, GWL_EXSTYLE, GetWindowLong(winHandle, GWL_EXSTYLE)
    | WS_EX_NOACTIVATE | WS_EX_APPWINDOW);
ShowWindow(winHandle, SW_SHOW);
like image 114
Derek Avatar answered Sep 30 '22 12:09

Derek


I don't know about QDialog, I'm using just a QWidget for similar purpose (displaying a Windows 8 style notification).

Try setting:

dialog->setFocusPolicy(Qt::NoFocus);
dialog->setAttribute(Qt::WA_ShowWithoutActivating); 

maybe you'll have to set focus policy on all children.

like image 22
headsvk Avatar answered Sep 30 '22 13:09

headsvk