I'm using Qt 5 and C++ and I want to force some of my child windows to have their own task bar entries. Right now, I am able to create parentless QWidgets and use the signal-slot mechanism to close those windows when a main window (QMainWindow) is closed.
However as I add more and more parents and childs this whole signal-slot technique will get tedious (won't it?) and I am sure that Qt already has a feature I can use for this. I've seen this; Primary and Secondary Windows section talks about what I'm trying to do. It says:
In addition, a QWidget that has a parent can become a window by setting the Qt::Window flag. Depending on the window management system such secondary windows are usually stacked on top of their respective parent window, and not have a task bar entry of their own.
I can see that I need to set my QWidgets as Primary Windows but I don't exactly know how.
So I tried:
// when a QMainWindow's (this) push button is clicked:
QWidget* newWindow = new QWidget(this, Qt::Window);
newWindow->show();
It doesn't give me the behavior I want. How can I set newWindow
as a Primary Window while still keeping this
as its parent?
I don't now native method to do it in Qt. But you can use for it signal/slot it's definitely not create a serious burden on the processor until you have at least a thousand windows. For it you can implement your own child parent mechanizme.
For example:
}
using:
In code
PrimaryWindow
create without Qt-parent, but for closing child windows you should inherit all your windows frommyOwnWidget
.Hope it help.
Heavily based on posters Konstantin T.'s and Mike's ideas, I have developed two classes,
MyWidget
andMyMainWindow
, which can use themselves and each other in their constructors to create child windows which will have their own task bar entries while still acting as children to their parent windows (i.e. getting automatically closed and destroyed upon termination of the parent window).MyWidget
replacesQWidget
andMyMainWindow
replacesQMainWindow
if such a behavior is desired.my_widget.h:
my_widget.cpp:
Example main.cpp:
So the window chain is:
mw1 -> mmw1 -> mmw2 -> mw2
where any window that is closed will also destroy all windows to its right and all windows in the chain will have their own task bar entries.I'm sure there are more elegant ways of defining the constructors in
my_widget.h
, but as a newbie this worked for me to obtain the exact behavior I needed. I'd appreciate to see better practices onmy_widget.h
.