QT - sliding widget - qt

I'm new in QT interfaces. I would like to create sliding widget in my desktop application (no QML). Idea is to create sliding menu like facebook component on some web pages.
For example:
I have main window and I want to have small part of widget on the right window edge
When mouse move on this widget (or click on it) then this widget slide to show all its content.
I know how to create animation and handle mouse events. There is a lot of examples about it. The problem is that this menu widget should not interact with other layouts and widgets. I mean, main window has root horizontal layout and I don't know how to exclude this widget from it and place widget in front of all widgets on main window. Are exists some layers in QT?

You could accomplish similar functionality by overlaying a custom QDockWidget. This widget could then float above the other widgets in a fixed position relative to your mainwindow. Connect the main window's resize/move events to slots on your QDockWidget that keeps their positions in sync.

Related

How to add menu bar and toolbar into the qdialog

Exists any solution how to add menu or tool bar into the QWidget dialog?
I making something like ERP system. There are many windowses opening from others windowses. It's important that one (parent) window waiting for choices in children window. And in the chidren window sometimes I need toolbars and menu bars...
Problem is, that
new child QMainWindow have no exec() function. It cause that parent window no waiting for finishing choices in the child.
new child QDialog have no menu or toll bar.
Exists any solution (without events)? Solution how to add menu or tool bar to QDialog, or solution how to open new qmainwindow with waiting mode myMainWindow->exec()?
Ok. I find solution.
I used QDialog. Menu or tab bar will be added as:
anylayout->addWidget(tabbar); or anylayout->setMenuBar(tabbar);
Thx for answer by Chris Kawa:
The difference is that setMenuBar places the widget outside of the layout content, so the top margin of the layout is below the bar . With addWidget the bar is added as a layout content, so it respects the margins (controlled by setContentsMargins).
For menus and toolbars we usually want them to stick to edges without a gap, so the setMenuBar method is more appropriate for it.

How can I create multiple custom widgets and display them with their absolute position

So I currently have got a custom widget, and I want to add them to the main window after clicking a button. I would like to add them all to one fixed position first and then I will be able to drag them wherever I like. I am able to create and display these custom widgets with help of QHBoxLayout or QVBoxLayout, but in this case they will not be in the same position after I create them. Any help will be appreciated!
As the names suggest, the QLayout classes manage the position and geometry of the items added to them. You cannot move (eg. drag) an item out of a layout w/out first removing it from the layout (QLayout::removeItem() and derivatives). For example when you drag a toolbar or dock widget out of a QMainWindow it goes through all sorts of machinations to remove it from the MW layout, change the widget's window flags, remember the old position in the layout, and so on. And the reverse to dock it again.
To do what you describe (drag widgets arbitrarily around a window) you would need to not use a QLayout and position the widgets manually by specifying a QWidget::setGeometry() for example. After initial position, and assuming the user has some way to grab the widget (title bar or drag handle you made, etc), you'll probably still need to manage their positions, for example if the main window is resized (if you care about keeping them contained). Essentially you'd have a bunch of separate widgets acting as individual windows and probably need some way to keep track of them.
I don't know what kind of widgets you're talking about, but one option may be a QMdiArea which lets the user drag windowed widgets around, tabify them, save/restore state, and so on.
For more flexibility you could also look into the Qt Graphics Framework. The graphics scene has a lot of features for user-movable items/widgets, keeping track of them, and so on. It is probably the most flexible method overall, and you can also use regular QWidgets inside a graphics scene.
A couple other Q/A about arbitrarily positioning widgets (I'm sure there are more to be found):
QPushButton alignment on top another widget
How to keep Push Buttons constant in relative to change of Label Size in PyQt4

How to dock a QDockWidgets inside a QSplitter?

I am trying to have a QSplitter accept QDockWidgets in my application. So far, I have done everything through the Qt Designer and what I have done is create three individual QWidgets. I then select all three of the QWidgets and I right click on them and select Layout->Lay out Vertically in a Splitter.
This lays all three of the widgets in a splitter quite nicely. I then drag a Dock Widget to the Object/Class Window in the top right and set them in the Splitters Widget. This places the QDockWidget happily inside the widget. However, when I fire up the program I cannot click and drag the dock widgets. If I double click the dock widget, the dock widget will pop out, however I cannot place it back since it was never technically docked. Which then creates the problem of the widget not being allowed to dock anywhere else. It cannot be docked on the QMainWindow class or in the QSplitter class.
Is there anyway to have a QDockWidget docked inside of a QSplitter and have the functionality of a QDockWidget?
After you add the dock widget to the QSplitter, the widget has become part of the splitter.
You can try checking like this
//If sure of Dockwidget at zeroth position
QDockWidget *widget1 = (QDockWidget*)ui->splitter->children().at(0);
A Dockwidget has a feature of floating as a top level window.
But you can make a dockwidget look like other widgets by setting QDockWidget::NoDockWidgetFeatures
Either:
Go to the object window in Qtdesigner (top -> right)
And select the dock widget added to splitter.
In proeprties window, down below scroll down and look for "features".
Then uncheck the features like movalble, closable etc....
I made it NoDockWidgetFeatures.
or
You can set programmatically using setFeatures(QDockWidget::NoDockWidgetFeatures)

How can I allow user resize on elements within the window using Qt designer?

I want to allow a user using my application to be able to drag a boundary between two widgets in my window which will resize the two (i.e. you drag it down and the top one will get bigger while the bottom gets smaller, and vice-versa).
Is there anything in Qt designer that will allow a user to resize an element in the window, within certain constraints?
Thank you
What you're describing is called a QSplitter widget. In Qt Designer, you can create one by selecting 2 or more widgets, and then clicking the splitter button on the toolbar at the top. It's in the same location as the layout buttons. It will place those widgets inside a QSplitter. You still need to place the splitter widget inside another layout. It will create a handle between them to let you resize the portion that each widget gets.
You're looking for the QDockWidget. It can do all that you described above and more. The user can dock the widget to different sides of the window, changing which widget is on the top or bottom. You can customize the minimum and maximum sizes, as well as default sizes.

QT layout manager does not recover space when hiding widget until window is moved

I am using Qlabels to plot some graphs and images (via setpixmap).
My basic layout is:
QVBoxlayout main layout via qdialog's setlayout.
QHboxlayout (array of QLabels)
Qlabel expandedPlot (optional expanded plot of one of the above QLabels)
QLabel mainImage Main image display
Within the QDialogs re-implemented keypress event handler, I hide()/show() the expanded plot. When I hide() the expandedPlot, the layoutmanager recovers about 1/2 of the vertical usage. Then when I drag the window, the layout manager recovers the remainder of the vertical space (as if there was no item present).
How can I force the behavior of moving the window? I want the layout manager to completely recover the vertical space.
I am using Qt 5.6 on windows, but want cross-platform solutions.
Thank you, mike
Because laying out the widget is quite expensive, Qt doesn't always do it. If you change the size of enclosed widgets you are likely to need to call updateGeometry on them to trigger the enclosed layout manager to re-layout. But if you hide the widget, updateGeometry does nothing. In that case you need to call adjustSize on the parent widget, which will then trigger the re-layout.

Resources