Reputation: 1458
I am using QT 5.15 and I'm creating a single-window application and want to add a widget (which will contain a list of items), with the following requirements:
It should be able to appear above other widgets. It is like a temporary widget, which will be visible only if the user selects a specific action. I just cannot fit it in existing layouts without screwing other widgets' positions and so the best position for it is to be above other widgets. It's dimensions are 1/3 the width and 1/3 the height of the main window. I also do not want to place it outside the main window, as this will reqiure more screen space
It should move together with the main window when the user moves the window. Just like everything in the main window moves as one piece, I want this widget to move together with it. This is because there is suitable position for it and I don't want to complicate the user experience
In order to avoid ambiguous behavior, let's say that there is a tiny element in one of the layouts, and when I manipulate this layout or resize the main window, the floating widget is always aligned at this tiny element.
What have I tried:
-QDialog: Couldnt't find a way to move it together with the parent. Also, it creates a new entry in the taskbar, which is a little annoying
-QDockWidget: Better than QDialog for this usage, but still I cannot force it to move relative to the main window.
-QStackedWidget: As I understand, this will not work, because only one widget can be visible at a time. Also, I have a collection of QStackedWidgets and adding another one will just make things more complicated
-Any other widget wants to be part of a layout, but as I said, the best position is on top of another widgets. Maybe a solution will be a widget with dimensions 10x10, which actually draws the entire reqired contents. But I do not know if Qt allows a widget to draw outside of it's reported area.
I also don't want any hacks related to overriding the mainWindow's moveEvent callback. First, when I move the main window, the callback is called only on move end. Second, the widget which will control the floating widget is deep in the hierarchy. There are even multiple of them, placed in a QStackedWidget, and only one is active. I would need to make signal handling tricks when switching QStackedWidget entries in order to handle them.
Is there elegant and simple solution? A combination of QDockWidget and the standard relative-position behavior of all widgets in a layout.
Actually, the element with the exact behavior is the QMenu. It always appears over other widgets, independent on their layouts. Also, it's position is always relative to the element, which opens the menu. However, there are also drawbacks - the menu is intended to show text entries and submenus only. Also, it closes when I select an item, or when I try to move the window. Again, a lot of hacks will have to be performed.
Upvotes: 0
Views: 607
Reputation: 881
You can just create the widget and not setting it in a layout. That way you need to move its position and handle its size manually but it will be above other child widgets of the same parent (using the raise
method if necessary) and will stay to its position (relative to its parent) even if main window is resized or moved.
Upvotes: 0