The Qt maximization minimization animation causes the form to display abnormally
-
Hi. Thank you for taking the time to go through my posts. I recently developed with Qt and ran into some issues.
Problem prospects:
Borderless windows mean losing the system's maximization and minimizing animation. So I added two buttons to the UI to click to minimize the customization. But this way without animation will seem blunt. Then I added QPropertyAnimation to maximize and minimize for transitions.auto appMinAni = new QPropertyAnimation(this, "windowOpacity", this); // Minimize animations auto appMaxAni = new QPropertyAnimation(this, "geometry", this); // Maximize animations bool isMin = false; // Whether to minimize the animated flag bits
The minimize animation changes transparency, and the maximize animation changes the size of the form
// Click the minimize button to start minimizing the animation connect(ui->appMinButton, &QPushButton::clicked, this, [=](){ isMin = true; appMinAni->setStartValue(1); appMinAni->setEndValue(0); appMinAni->setDuration(500); appMinAni->start(); });
Minimize animation completion
connect(this->appMinAni, &QPropertyAnimation::finished, this, [=]() { if (isMin) { showMinimized(); }else { showNormal(); } });
At this point, the program has been minimized to the taskbar, and we need to restore to the Normal state when the taskbar is clicked, so in the eventFilter I listened to the QWindowStateChangeEvent to restore
if (event->type == QEvent::WindowStateChange) { auto stateChangeEvent = static_cast<QWindowStateChangeEvent*>(event); if (stateChangeEvent->oldState().testFlag(Qt::WindowMinimized)) { // Revert to the Normal state isMin = true; appMinAni->setStartValue(0); appMinAni->setEndValue(1); appMinAni->setDuration(500); appMinAni->start(); } }
Minimization and minimize restore are both expected behaviors at this point, and here is the logic of maximizing and maximizing restore to Normal, where the problem arises. Separate maximization and maximization restores, separate minimization and minimized restores are expected. But first click maximize, then revert from maximized state to Normal, click minimized, and restore to normal when clicking taskbar. The question is, the program shows not the normal state, but the maximized state, what is the situation? The maximization referred to here is when the program interface covers the entire desktop.
Two variables are used here to represent the width and height of the desktop, which are actually obtained through API calculations
int desktopWidth = 1920; int desktopHeight = 1050; // Whether to maximize flag bits bool isMax = false;
Click the maximize button to start maximizing
connect(ui->appMax, &QPushButton::clicked, this, [=]() { if (isMax) { Maximize restore to Normal isMax = true; appMaxAni->setStartValue(QRect(x(), y(), width(), height())); appMaxAni->setEndValue(QRect((desktopWidth - minimumWidth()) / 2, (desktopHeight - minimumHeight()) / 2, (desktopWidth / 2 ) + minimumWidth(), (desktopHeight / 2) + minimumHeight())); appMaxAni->setDuration(600); appMaxAni->start(); } else { // Normal -> maximum isMax = false; appMaxAni->setStartValue(QRect(x(), y(), minimumWidth(), minimumHeight())); appMaxAni->setEndValue(QRect(0, 0, desktopWidth, desktopHeight)); appMaxAni->setDuration(600); appMaxAni->start(); } });
Maximize animation completion
connect(appMaxAni, &QPropertyAnimation::finished, this, [=]() { if (isMax) { showNormal(); }else { showMaximized(); } });
It stands to reason that it is not the same attribute that minimizes the effect of animation, but why do they affect each other? The expected result is that the maximize button program is clicked to maximize, then click the maximize button program to restore to Normal, click the minimize button program to minimize to the taskbar, and then click the minimize button to restore from the taskbar to Normal, but it is precisely the maximization that is minimized, and the program becomes maximized (that is, covering the screen) instead of Normal (with minimumWidth() and minimumHeight() The size of the composition), what is the situation? If you only minimize the use of related animations and disable maximization-related animations, then minimizing animations is expected, obviously the effect of maximizing animations, but I don't understand why?
A sample code with the same logic is provided here for you to test. How to solve this phenomenon?Code Example
-
@jsulm You don't take the initiative, I don't take the initiative. How can there be a story between us.
@Nan-Feng Sorry, but I'm not your employee and don't have to take initiative. I'm not payed for helping others here. I'm doing it in my own private time.
If you want better support you will have to pay QtCompany as commercial user.