How to make promoted widgets show up larger in Qt Designer [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am working on an app which has a number of promoted widgets. My work flow is as follows:
Design a new widget with buttons, labels, textedits etc. in Qt Designer, save a .ui file new_widget.ui.
I am working in Python with PyQt5 so I use PyQt5 to convert this .ui file into a .py file new_widget_ui.py.
I then make a new file new_widget.py which defines a class NewWidget which inherits from the ui class which is defined in new_widget_ui.py and calls SetupUi(). Additionally signal slot functionality etc. is defined in NewWidget class.
I want this new widget to appear as a subwidget in my mainwindow so in app_window.ui in Qt Designer I place a generic widget and promote it to class NewWidget from header file new_widget.py.
All of the above works perfectly, I've done it for many widgets with nestings etc. The problem I have is cosmetic/convenience.
The app is to control and visualize the data collected by a camera. There is a main widget display_widget which displays the camera output. This widget should be as large as possible given the window size so it has an expanding size policy. All of the other widgets are adjusting settings for this camera so they should be as small as possible to not shrink the main display.
So I've given the main display an expanding size policy and stretch factors of 1 while the other widgets typically have preferred size policies (defined both in the new_widget.ui file and as the properties of the new_widget placeholder in app_window.ui) so that they will shrink in favor of the expansion of the display_widget. I also use spacers to compress the options widgets as much as possible.
All of this works basically as desired when I run the program. The problem is that in Qt Designer all of the widgets shrink to basically zero size (as expected). This makes it unfortunately difficult to develop the Ui since I can't click on the widgets that I want to and it's hard to add new widgets to the appropriate layouts.
My workaround is to set minimum sizes to 50 or 100 for all of the subwidgets so that I can get a rough idea for how the layout will actually look while I'm developing the ui. However, I have to make sure to set all of these minimum sizes back to zero when I finish and I'm ready to run the app again because if I don't it ruins the actual behavior.
Question:
Is there a better way to have promoted placeholder widgets have a non-zero size in Qt Designer so you can easily see where they are and add new widgets to layouts?

Premise
There is no simple solution for this.
If those widgets are pretty complex and you're going to have many of them in your GUI, a possibility is to create your own widget plugin, but that's not easy, as the docs are all C++ oriented, and the rare available documentation for PyQt is very old and fragmentary (also, some functions in the plugin dedicated classes are not even implemented in PyQt).
It is doable, but it's not easy nor painless. You can start by reading a very old tutorial (it's dated back to 2008!), and there are some questions even here on SO that can help you. It takes a lot of time to understand how it works and how to implement it, but if your UI is complex it might be worth it.
A (dirty) workaround
Whenever a widget is loaded from uic or a pyuic file, setMinimumSize() is explicitly called even if only one size direction is set as a minimum size.
As long as you are fine with not setting the actual minimum size for the promoted widget in Designer (at least, not using the dedicated fields; I'll explain more about this on the next point), the (very, very, very dirty) workaround is to override setMinimumSize() and, eventually, restore the default base implementation right after the UI is loaded, so that you can actually set the minimum size if you need to do that programmatically.
In this way, even if a minimum size is set on the UI, setupUi() (or loadUi() if you're using uic) will not be able to set it since the base implementation method is not going to be called.
Note: Remember that if you want a default minimum size for a widget class, you should use minimumSizeHint(), and not setMinimumSize(). Overriding the minimum size hint for the class ensures that all new instances will always have a minimum size for the layout, while you can still set your own minimum size for any widget you want.
This is an example of the promoted class definition:
class Promoted(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# create a reference to the default implementation
self.__setMinimumSize = self.setMinimumSize
self.setMinimumSize = self.ignoredSetMinimumSize
# ...
def minimumSizeHint(self):
# a default minimum size *hint* for the layout, whenever this widget will
# be added to a layout, it will *never* make it smaller than this size;
# this is obviously an arbitrary size
return QtCore.QSize(15, 15)
def ignoredSetMinimumSize(self, minSize):
# ignoring calls to setMinimumSize
return
def restoreSetMinimumSize(self):
# restore the original base implementation
self.setMinimumSize = self.__setMinimumSize
Then, on the window that is going to use the ui (or pyuic) file:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from promoted import Promoted
class Win(QtWidgets.QWidget):
def __init__(self):
super().__init__()
uic.loadUi('test.ui', self)
for widget in self.findChildren(Promoted):
widget.restoreSetMinimumSize()
# an example to show that the original implementation is actually restored
self.promotedWidget.setMinimumSize(50, 50)
How to actually set a minimum size
As said, this has an important drawback: if you do need to set a minimum size for any of those custom widgets in Designer, you obviously cannot do that from the normal minimum size fields on the property editor.
The solution is to use a dynamic property with a custom method for that.
In the property editor, create a new property (for example, customMinimumHeight) by clicking on the "+ [Add Dynamic Property]" button on top of the property panel, and set it to the actual required amount, then override the event() method and check for QtCore.QEvent.DynamicPropertyChange event types within an event() override.
Note that you have to select the correct property type from the menu, then type in the property name.
Select Int from the combo box if you're going to change only one size direction, otherwise you can set both width and height using the Size type. Note that if you're going to set only a size direction, you must also ensure that the opposite side is not already set: in that case the opposite direction must be set to the maximum possible amount (16777215).
Remember also that the minimum and maximum sizes are set to invalid values (-1) only internally: on the "public" side, they will always be equal to 0 if they have explicitly been set to 0 or not set at all. This means that you have to be very careful if, for any reason, you're going to explicitly set the minimum or maximum height/width to 0. While this is usually not a problem for the maximum size, it might be whenever the minimumSizeHint is set.
def event(self, event):
if event.type() == QtCore.QEvent.DynamicPropertyChange:
if event.propertyName() == 'customMinimumHeight':
width = self.minimumWidth() or 16777215
height = self.property('customMinimumHeight')
self.__setMinimumSize(QtCore.QSize(width, height))
elif event.propertyName() == 'customMinimumWidth':
width = self.property('customMinimumWidth')
height = self.minimumHeight() or 16777215
self.__setMinimumSize(QtCore.QSize(width, height))
elif event.propertyName() == 'customMinimumSize':
self.__setMinimumSize(self.property('customMinimumSize'))
return super().event(event)

Related

How can I create a responsive UI with buttons, that scales with size changes with PyQt5?

I am currently working on a Python Project and I would like to create the UI with PyQt5 (preferably with the help of PyQt5 Designer).
I have recently finished a Youtube tutorial about the basics of PyQt5, but I am still at beginner level.
I am currently trying to create the Main Menu of the program.
I would like the Main Menu to look like this (the background image would decorate the background but it's not today's problem):
enter image description here
But if possible, I hope there is a way to achieve it, I would like the buttons to scale with window size.
So for example when the user resizes the window, I would like it to look somehow ike this (not exactly, but something similar):enter image description here
So as you can see I would like the button width and height getting changed, as the user resizes the window (and keeping their ratio within the window).
I hope there is a way to solve this problem.
Thank you for the help in advance.
I tried to right click on QWidget and then clicking on the last option (might be Alignment in English, I am not native) and then clicking on that option (Maybe align as Grid in English)
enter image description here
After doing this, the layout this expanded to the window size, the button got resized as well.
enter image description here
enter image description here
But the button width corresponds to the layout size (not just for example 1/3 of it as I would like) and the height does not change that greatly, just the width.
Premise: there are various questions on the matter here on StackOverflow; while they mostly point out to documentation or answer to specific issues, I've not found a comprehensive answer that could be considered as a "main reference" which can be used as duplicate pointer yet, especially when the developer wants to have big margins and leave some empty space within the UI. The following may be considered as a generic answer thanks to the simple nature of this question.
You are on the right track: layout managers are the solution, and while it's not forbidden to use explicit geometries, that practice its frowned upon[1].
Layout management basics
Know more about layout managers
Qt provides 2 basic layout types:
QBoxLayout, a "direction based" layout, that aligns widget along a specified direction: horizontally or vertically; Qt provides two convenience classes for those directions: respectively, QHBoxLayout and QVBoxLayout;
QGridLayout, a layout that aligns widget using a grid, similarly to a table or a spreadsheet;
There are also more complex layout managers:
form layout (QFormLayout), providing a simple "form style" layout, normally shown with a label on the left, and some widget on the right, with "items" shown from the top to the bottom;
stacked layout (QStackedLayout), which instead switches between visible widgets as they were "pages" (see below);
Additionally, there are complex widgets that implement custom layouts on their own:
QTabWidget is based on the convenience QStackedWidget class (using the above mentioned QStackedLayout) with the addition of a tab bar to allow the user to switch between them;
QSplitter, that can split the available space horizontally or vertically, with handles that allow resizing the contents within the available area;
QToolBox that behaves similarly to QTabWidget, with the difference that the "pages" are put in a column, similarly to a file cabinet;
Finally, in Qt, layout managers use layout items, which are abstract object that represent physical geometries that are being shown on the screen. Those items may be widgets, spacers or even nested layouts.
In this answer, I will only cover the basic (grid and boxed) layout managers.
Set a main layout
The first step is to ensure that the parent widget (the one that contains a group of child widgets) has a layout manager: in Designer (as the documentation explains), you have to open the context menu of the "container widget" by right clicking on an empty area inside it, and choose an appropriate item in the "Lay out" submenu.
Using code, that's normally done like this:
widget = QWidget()
layout = QVBoxLayout()
widget.setLayout(layout)
# alternatively, just use the target widget as argument in the constructor
# layout = QVBoxLayout(widget)
# the above line automatically calls setLayout(widget) internally
# ...
layout.addWidget(someWidget)
The issue of "responsiveness" and spacing
Modern UIs may often have lots of free space with relatively small controls. For instance, a login interface takes some amount of screen space, even if its input fields are quite small, allowing some space to show a fancy background or, even, just to better capture attention from the user.
With nowadays devices, we normally have a lot of available (and actually readable) screen size, but that wasn't the case until 10-20 years ago, when it was still normal to have "big" screens with a very small resolution (19" CRT screens only showing 1280x960 pixels if not less... heck, that's one of my screens!): you had about 90-100 pixels for inch, while High DPI screens or modern mobile devices can show about 10 times more in the same size. Less resolution means that it's more difficult to distinguish objects on the screen, especially when dealing with text; it's like having some light form of visual impairment: you may still be able to read text and set shapes apart, but it's difficult, you need to be more focused on what you're trying to see, and after some time that can be annoying and create fatigue.
The QtWidget module, similarly to other namespaces of other "old" common toolkits (eg. Gtk), was born on those concepts, meaning that "widgets" (UI elements that are used as human interface) have some requirements based on the available pixels, also considering the capabilities of the system to show text.
Long story short: widgets normally take as much space as it's possible in order to show their context, based on the principles above, but also considering previous, existing conventions.
Some widgets use space weirdly
Let's create two buttons and manually set their geometries:
And then set a vertical layout as explained above:
As you can see, they take all the available horizontal space, but do not extend vertically. The previous convention says that buttons do not expand vertically.
In the early days, UIs were simple: there were few buttons that were normally shown at the bottom of some dialog, and they used as much horizontal space as possible. Screen resolutions were actually small (640x480, or even less when using simple ASCII characters to display UI elements), there was no point in having "big" buttons, not to mention tall buttons.
Other widgets require different space usage
Let's add a QLineEdit to the top of the above layout: QLineEdit is a simple input field, it normally requires a simple string consisting of a single line; meaning that there is no point in requiring more vertical space, so the result is quite the same:
Now, we need a more complex text input, allowing text that may have multiple lines; let's add a QTextEdit widget:
Whoa, what's happened? Since QTextEdit is normally capable of showing multiple lines, it's important to take all the available space. Since the other widgets don't really need all that space, it will take advantage of it.
Size hints and policies
Layout managers have to decide how to set the geometry (position and size) of all the items they manage. In order to do that, they query all the items they manage (consider that layouts may be nested) and ask them about their size hints (the size that the item prefers), their constraints (minimum/maximum or fixed size) and their policy.
All Qt widgets provide a size policy, which tells the layout that manages them how they could be eventually resized and positioned. QPushButton has a fixed vertical policy (meaning that they usually have a predefined fixed height[2], as shown in the second image), while all scroll areas have an expanding vertical and horizontal policy, so they will try to take advantage of all the available space (and the layout will decide how much, based on all the other items).
Stretch factors
Basic layout managers support stretch factors, which can also be used as spacers: each layout item has a stretch factor that is considered as a ratio (computed with the total sum of all stretch factors of the layout).
Consider the following layout, using 2 QTextEdits:
Now, select the container widget in Designer, and scroll to the bottom of the Property Editor until we get the container layout properties; change the layoutStretch property to 1, 2, meaning that the available vertical space will be split in 3, with the first QTextEdit using the result of that value, and the second using twice (height / sum_of_stretches * stretch_of_item):
Margins and spaces
Considering the above, we still want to have a fancy UI that uses a lot of the huge available resolution and show very tiny widgets.
QBoxLayout classes provide the helper functions addStretch() and insertStretch(), that allow to create "empty" spaces (using QSpacerItem objects). This can also be done in Designer, by dragging and dropping the "Horizontal" or "Vertical" spacer item from the widget box; in the following image, I've added a vertical spacer to the top, and changed again the layoutStretch property to 1, 1, 2, meaning that there will be an empty space on top that is tall as much as the first QTextEdit, and both will be half of the second:
Complex layout management
As said above, QLayout subclasses manage their items, including nested layouts[3]. Let's restart from the basic two buttons and add spacers above and below:
That's still not fine. Let's set stretch factors properly; for instance, let the buttons take 1/5 of the available space each, with 2/5 of the space above and a remaining fifth below:
But that still doesn't work as expected. Remember about the size policy explained above. That property can be changed also in Designer (or by code by using setSizePolicy()); select those buttons and change their vertical policies to Preferred [4]:
That's better. But still not close enough: there's still a lot of unnecessarily used horizontal space.
We could change the maximumWidth properties of those buttons, but that would be fixed; we don't like that: the buttons will always have the same width, even if the window is very wide.
Enter QGridLayout
One of the benefits (and falls) of QGridLayout is that it always has a static amount of rows and columns [5], even if no layout item exists at that row/column cell position. This means that we can use its setRowStretch() and setColumnStretch() functions even when the layout has no widget for such row or column amount.
In Designer, we can change the layout type to a grid by right clicking on an empty area of the container, then it's just a matter of setting proper stretch factors, but if you already had a layout set it's better to select the Break Layout item and manually set an hypothetical layout by hand, then select Grid Layout from the submenu.
Let's restart again from scratch, as shown in the first image; break the layout and reposition/resize the buttons:
Then select a grid layout from the context menu (the following shows buttons for which a Preferred vertical size policy was already set):
Now add horizontal and vertical spacers:
Assuming that the vertical size policy of those buttons is set to Preferred as explained above, finally update the parent layout's layoutRowStretch and layoutColumnStretch factors to 2, 1, 1, 1 and 1, 1, 1 respectively; the vertical space will be split in 5, with an empty area occupying twice the resulting value, while the buttons and the bottom spacer occupying that fifth each; horizontally, left and right spacers will be as wide as the buttons:
If we resize the form or its preview, the button sizes are more responsive:
Note that in order to do that by code you must previously consider cell positions: the buttons will be added to rows 1 and 2, and column 1, then you have to properly call setRowStretch() and setColumnStretch() with appropriate indexes and factors.
This is one of the reasons for which QGridLayout may not always be the proper choice, especially for dynamic layouts for which the row/column cell count might not be known at first.
Layout management seems difficult, is it required?
The simple answer is "no", but reality is quite more complex.
As mentioned above, widgets must always ensure that they are always visible and usable. Modern systems use HighDPI screens, meaning that the physical pixels are never the same as logical pixels: a line that has a width of 1 "pixel" may actually be 10 pixels wide. Text may also depend on the font scaling.
Some visually impaired users (including people just having "simple" presbyopia) may set up their computers to have high font scale ratios (150, 200 or even more) in order to be able to read text more easily; some people may just use a peculiar default font that, at the default size, requires much more space to be shown, vertically and/or horizontally. Since Qt always tries to fully show a widget containing text considering the required space of its font, the result is that you may have overlapping widgets, because you didn't consider that another widget on its left or top may require more space than you thought.
The rule of thumb is: what you see on your device(s) is never what others will see.
Qt layout management (including QStyle functions, specifically those related to QSize such as QStyle.sizeFromContents()) always consider these factors, and, besides some unexpected behavior/bug, you can normally expect a resulting UI that is properly shown to the user.
99.9% of the times somebody wants to use fixed geometries, they are doing something wrong: they are probably trying to do something for the wrong reason (normally, lack of experience), and, in any case, they are not considering the above aspects.
As a reference, you've probably browsed some website on a mobile device that is simply "not responsive": you have to scroll a lot, and navigation is really annoying. That is because those website obviously didn't consider modern devices; and that's as annoying as a "not layout managed" UI might look. Luckily, even if the QtWidgets module is "old", it considers these modern aspects, and, even considering some glitches and inconsistencies, it normally allows proper geometry management as long as layout managers are properly used.
[1]: there is theoretically nothing wrong in explicitly setting geometries, as long as it's done with awareness; 99% of the times, it isn't: object require a certain size in order to be properly shown and used, which requires being aware of system settings: most importantly screen DPI and font scaling; Qt is quite careful about these aspects and tries to ensure that all widgets are properly displayed and usable; if you're getting issues with font or widget display, avoiding layout managers is not the solution;
[2]: Qt uses QStyle functions to decide how wide or tall a widget should or could be, based on the detected system configuration; you should normally trust it;
[3]: See the following posts: 1, 2 and 3;
[4]: It's possible to set properties to multiple widgets at once, as long as those properties are common; since the sizePolicy property is common to all widgets, we can select both buttons (using Ctrl) and the property change will be applied to both of them;
[5]: See this related post;

In PyQt, how can I cycle through multiple copies of a widget and have each of them remember their state?

I'm building a program where a user can interact with a pyqtgraph ImageView (e.g. place markers, change brightness and contrast). I want to be able to cycle through different images, but have each image remember its state with the markers etc. I feel like the most appropriate solution would be to create a copy of the ImageView for each image and show the current one and hide the other ones. What would be the best way to do that and would that even be a good idea?
My naive approach was placing an empty widget in Qt Designer (called graphWidget), promoting that to the ImageView class, and then in the program:
graphWidget_backup = self.graphWidget
secondWidget = pg.ImageView()
secondWidget.setImage(np.random.rand(256,256))
self.graphWidget = secondWidget
But with that, nothing happens.
Of course I could also just save the image data and all other changes manually and set them on the ImageView, but that feels less elegant and "object oriented".

How to set the initial size of a QTreeView in a QSplitter?

I have a QTreeView inside of a QSplitter that I want to set the initial size of. I have found that I can set both setMinimumWidth(val) and setFixedWidth(val) on the QTreeView, but neither of those seem to do what I need.
The setMinimumWidth(val) only prevents the QTreeView from getting smaller than the value of val, and setFixedWidth prevents the user from resizing the QTreeView entirely.
Is there anyway to set the initial size, either by setting QSplitter or setting QTreeView? I assume I need to set the sizeHint, but I'm not quite sure.
I'm using Python 3 with PyQt5
You can reimplement sizeHint to set the initial size:
class TreeView(QtWidgets.QTreeView):
def sizeHint(self):
size = super().sizeHint()
size.setWidth(320)
return size
However, if creating a subclass is not desirable, and you don't mind setting a proportional initial size, you can use QSplitter.setSizes. The Qt docs define the behaviour of this method in this way:
The overall size of the splitter widget is not affected. Instead, any
additional/missing space is distributed amongst the widgets according
to the relative weight of the sizes. [emphasis added]
So it doesn't matter if sum of the sizes is larger than the splitter; all that matters is the relative weights. Given this, we can just calculate the maximum possible size available, and then divide it up accordingly, like this:
width = QtWidgets.qApp.desktop().availableGeometry(self).width()
self.splitter.setSizes([width * 2/3, width * 1/3])
This will make the first widget twice as large as the second.
In addition to these approaches, you can also use QSplitter.saveState and QSplitter.restoreState to save and restore the splitter sizes set by the user from a previous session.
PS:
If you're using Qt Designer, the above sizeHint approach can be used with widget promotion, which is quite simple to implement. See this answer for an explanation of how to do this.

PyQt5 Pixmap in Label dynamic resize via Splitter

I am currently trying to solve a problem with resizing Pixmap dynamically, I have a QLabel in one corner of my QMainWindow, from both sides surrounded by two different QSplitters when I change picture it is scaled by the size of the label with KeepAspectRatio. Both splitters also are connected with signal to a function that once again scales the pixmap to fit as much as it can.
The problem I ran into is, that I cannot figure out how to be able to decrease the size when the pixmap already fits all the space available, because at that moment the splitters just stop working.
This is the pixmap setup:
self.picture_field.setStyleSheet("border: 4px solid")
self.pixmap = QPixmap('dandelion.jpg')
self.picture_field.setAlignment(Qt.AlignCenter)
self.picture_field.setPixmap(self.pixmap.scaled(self.picture_field.width()-8,
self.picture_field.height()-8, Qt.KeepAspectRatio))
The -8 has to be there because of the border, if it was missing, the widget gets bigger with every change
One of the Qsplitters:
right_splitter = QSplitter(Qt.Vertical)
right_splitter.addWidget(self.picture_field)
right_splitter.addWidget(self.person_field)
right_splitter.splitterMoved.connect(self.dynamic_scaling)
Person_field is simple QTreeView
The dynamic scaling function:
def dynamic_scaling(self):
self.picture_field.setPixmap(self.pixmap.scaled(self.picture_field.width()-8, self.picture_field.height()-8, Qt.KeepAspectRatio))
EDIT: I tested it a little bit more, and it seems, that the pixmap reacts, but only once either the width or height is halved.
Judging by this statement, "I cannot figure out how to be able to decrease the size when the pixmap already fits all the space available, because at that moment the splitters just stop working", I think I know what the problem is. Every QWidget has a minimum size, available through the function QWidget.minimumSize. From experience I know that a QSplitter honors the minimum sizes of its children, and it will not allow the user to move the sash to a position that would force one of the children to take a size less than its minimum.
You say that one of the splitter's children is a QLabel. By design, QLabels are pretty smart about their own size requirements. If you change the text of a QLabel, for example, it will automatically recompute its size values and trigger a re-layout. I assume it does the same thing if you change the Pixmap, although I have not tried this myself. The point is that the QLabel's minimum size changes as its contents change.
Because of the dynamic resizing of the QLabel, and the refusal of a QSplitter to allow a child widget to violate its minimum size, the splitter may appear to "get stuck" and prevent you from making it smaller.
To fix this you need to make the QLabel have a minimum size that's very small, say (1,1). There are a few ways to do this. Since I don't have your complete application I can't be sure which one will work best for you.
Subclass QLabel and override minimumSize to return (0,0) This is pretty straightforward.
Set the size policy on the QLabel object to QSizePolicy.Ignore. See the docs for the function QWidget.SetSizePolicy.
Use the function QWidget.SetMimimumSize. I'm not sure this will work for QLabels since any change you try to make may get undone the next time you change the pixmap.
Replace your QLabel with a custom widget subclassed directly from QWidget. A generic QWidget has no minimum size, so your problem goes away. You will need to write a small function to set a new pixmap into your control, which will entail a paint event handler.
I hope that one of these methods will work for you (and that I have understood the problem correctly).
try that:
w = QtGui.Qlabel.width();
h = QtGui.Qlabel.height();
#set a scaled pixmap to a w x h window keeping its aspect ratio
QtGui.Qlabel.setPixmap(p.scaled(w, h, Qt.KeepAspectRatio))
now try to change h and w.
If you get any issues comment below.
πŸ…ΏπŸ†ˆπŸ†€πŸ†ƒ

How to show/hide a child QWidget with a motion animation?

I am working on an application with two children. One's a widget that functions as a toolbar, the second, below, functions as dashboard, on which information would appear. The latter can be shown/hidden with buttons on the former. Here's a screen-cast of the prototype.
Now I am looking at doing the same but with a motion animation whilst showing/hiding the lower widget.
In short: the effect should be giving the impression the entire application rises or falls progressively when toggling the dashboard.
In details: I would like the height of the lower widget to decrease until it is reduced to 0 and then hidden completely. Likewise it would increase slowly when showing the widget again. In the meanwhile the position of the application should change accordingly so it stays at the bottom of the screen.
How can I do that? I've never done animations on Qt before. If you don't have an answer, do you know of a quality tutorial that could lead me there.
NB: I am using PyQt.
I think you can get what you want by using a QPropertyAnimation that animates the geometry property of your widget.
But IMHO this is the window manager's role to do what you want. Maybe you will have some headaches bypassing it (but I'm maybe wrong).
After better reading of your question, it seems that you want to use your own components to trigger the hiding/showing so the WM shouldn't be a problem.
As a start here is some code that animate a minimizing of a widget (assuming tbw is an instance of the widget you want to animate):
formerGeometry = QtCore.QRect(tbw.geometry()) # storing previous geometry in order to be able to restore it later
hideAnimation = QtCore.QPropertyAnimation(tbw, "geometry")
hideAnimation.setDuration(2000) # chose the value that fits you
hideAnimation.setStartValue(formerGeometry)
#computing final geometry
endTopLeftCorner = QtCore.QPoint(tbw.pos() + QtCore.QPoint(0, tbw.height()))
finalGeometry = QtCore.QRect(endTopLeftCorner, QtCore.QSize(tbw.width(), 0))
hideAnimation.setEndValue(finalGeometry)
hideAnimation.start()

Categories