Search code examples
macosqtqt5qwidgetqmainwindow

How to avoid bringing QMainWindow to the front when clicking on independent QWidget


I am working on Qt app. There I have QMainWindow and QWidget which is displayed independently and out of the window.

I want to achieve that if I click on that QWidget, the window doesn't come to the front. I.e if it is behind another app, it should remain like that.

I have created test app:

main.cpp

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);
   MainWindow w;
   w.show();
   Widget mywidget;
   return app.exec();
}

Widget.cpp
namespace
{
 Qt::WindowFlags defaultWindowFlags()
 {
  Qt::WindowFlags f = 0;

  f |= Qt::X11BypassWindowManagerHint;
  f |= Qt::FramelessWindowHint;
  f |= Qt::WindowStaysOnTopHint;
  f |= Qt::CustomizeWindowHint;
  f |= Qt::WindowDoesNotAcceptFocus;  
  f |= Qt::Window;
 return f;
 }
}

Widget::Widget(QWidget *parent) : QWidget(parent, defaultWindowFlags())
{
 setFixedSize(100,100);
 setStyleSheet("background-color:blue;");
 move(56,89);
 setVisible(true);
}

Solution

  • Leave out the line f |= Qt::WindowDoesNotAcceptFocus;, this makes Qt keep the focus at the main window.