Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Keep child window on top of parent even when it loses focus

I have a UI window called by the main window. I would like it to be non-modal, but stay on top of the parent, and if the parent is minimized, to be minimized with it.

If the user wants to make changes in both, he should be able to, and the child would not be closed unless the user closes it. The child would always stay on top of the parent - but not on top of anything else.

m_child->show();
m_child->activateWindow();

The above places the child on top of the parent, it allows work on the parent while the child has been started... but the child is hidden behind the parent if it loses focus.

m_child->show();
m_child->activateWindow();
m_child->raise();

No change.

Using

Qt::WindowFlags flags = m_child->windowFlags();
m_child->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
m_child->show();
m_child->activateWindow();

The child is on top, as I want, I can work on the parent while the child is still shown on top... but minimizing the parent does not minimize the child as well, and the child stays on top of ALL windows (it should only stay on top of the parent)

How can I make a "toolbox style" effect - have the child on top of parent while parent is active, but minimize child when parent is minimized ?

I also experimented with all the window flags but they allow the child to become hidden when it loses focus.

like image 541
Thalia Avatar asked Jan 09 '15 23:01

Thalia


1 Answers

You can achieve this behavior by adding Qt::Tool flag to the toolbox widget and setting main window as its parent.

See http://doc.qt.io/qt-5/qt.html#WindowType-enum

like image 96
Pavel Strakhov Avatar answered Oct 29 '22 20:10

Pavel Strakhov