Solved QPushButtons disappear when mouse moves over them after animation
-
Hello all,
I am new to this forum and to QT library. I am currently learning this library as it seems to be a very popular C++ GUI library.
Anyways. I have this fade in animation for 2 buttons. I created the animation by animating the opacity settings. In order to run the animation at the same time, I placed the animations in a parallel animation group. After the animation successfully runs, when I move my mouse over to click one of the buttons, the corresponding button disappears.
At first, I thought that the buttons were being hidden. So, I created an event that at the end of the parallel animation, the buttons would "unhide" by setting setHidden to false. This did not work and only caused the buttons to be become hidden after the animation! I tried with a true value and there was no change; the buttons still disappeared after the mouse hovered over it.
I then tried to set the opacity setting to 1 after the animation completes thinking that there might be some bug related to this. And results are the same .
void MainWindow::updateGUI() { disconnect(p_parallelGroup, SIGNAL(finished()), 0, 0); switch(p_GUIState) { case systemState::PHYSICS_CHOOSING: { p_parallelGroup = new QParallelAnimationGroup; if(!p_finishButton) { p_finishButton = new QPushButton("Finish", this); p_backButton = new QPushButton("Back", this); this->centralWidget()->layout()->addWidget(p_backButton); this->centralWidget()->layout()->addWidget(p_finishButton); } QGraphicsOpacityEffect *fadeInBackEff = new QGraphicsOpacityEffect(this); QGraphicsOpacityEffect *fadeInFinishEff = new QGraphicsOpacityEffect(this); p_finishButton->setGraphicsEffect(fadeInFinishEff); p_backButton->setGraphicsEffect(fadeInBackEff); QPropertyAnimation *fadeInBackAnim = new QPropertyAnimation(fadeInBackEff, "opacity"); QPropertyAnimation *fadeInFinishAnim = new QPropertyAnimation(fadeInFinishEff, "opacity"); fadeInBackAnim->setDuration(1000); fadeInBackAnim->setStartValue(0); fadeInBackAnim->setEndValue(1); // fadeInBackAnim->setEasingCurve(QEasingCurve::InBack); fadeInFinishAnim->setDuration(1000); fadeInFinishAnim->setStartValue(0); fadeInFinishAnim->setEndValue(1); // fadeInFinishAnim->setEasingCurve(QEasingCurve::InBack); p_parallelGroup->addAnimation(fadeInBackAnim); p_parallelGroup->addAnimation(fadeInFinishAnim); p_parallelGroup->start(); break; } default: break; } connect(p_parallelGroup, SIGNAL (finished()), this, SLOT (updateGUIComplete())); } void MainWindow::updateGUIComplete() { switch(p_GUIState) { case systemState::PHYSICS_CHOOSING: p_anotherButton->setWindowOpacity(1.0); p_button->setWindowOpacity(1.0); p_button->setHidden(true); p_anotherButton->setHidden(true); break; } }
Has anyone else ran into this situation before?
-
Great News!
I was able to figure the issue out. Using your suggestion, I found a method that disabled the effect.
Now, once the GUI is finished updating, the code turns off the effect and everything works fine now.
void MainWindow::updateGUIComplete() { switch(p_GUIState) { case systemState::PHYSICS_CHOOSING: p_backButton->graphicsEffect()->setEnabled(false); p_finishButton->graphicsEffect()->setEnabled(false); break; } }
-
@philm001 I feel like I've run into this before. It was many years ago when I did an animation with Qt Widgets. I want to say like 2012 which is why it's not fresh in my head.
Anyway, IIRC the problem was after the animation gets to opacity 1.0 (visible) it "finishes" and resets back to it's starting state which was opacity 0.0 (invisible). Then when something forces that to redraw, like a button click, then it redraws with the 0 opacity and hides itself.
I am going to look for that old code (if I still have it) and see how I solved it. But if nothing else that should get you going with an idea to look into while I dig around for this code. :)
-
@ambershark Thank you for your response! I had a feeling that the opacity was somehow being set back to 0. I tried having a piece of code that when the animation is complete, to set the opacity back to 1. But the code did not work. Maybe I am calling the incorrect function?
void MainWindow::updateGUIComplete() { switch(p_GUIState) { case systemState::PHYSICS_CHOOSING: p_finishButton->setWindowOpacity(1.0); p_backButton->setWindowOpacity(1.0); break; } }
Right now, I am having an issue where the this variable is inaccessible in the debugger. I am also unable to load the data structure of the buttons in my watch window. I currently have a topic for these issues opened in the forum.
But with this information, I will see if I can explore more solutions.
Thank you
-
So after some additional research, I was using the wrong method for setting the opacity.
The setWindowOpacity method is not useful for QPuhButtons. Rather, I have to use:
setStyleSheet("background-color: rgba(192, 192, 192, 255);");
unfortunately, what happens is that the buttons quickly disappear when I place the function in the updateGUIComplete function. So we still have the same issue.
-
Hi
Have you also tried setting the fadeInBackEff / fadeInFinishEff
back to 1.0 with setOpacity(qreal opacity)
?
Since the buttons still have this effect applied, it might have other value than expected.Also, please try to qDebug() << p_backbutton->isHidden();
to verify its just transparent and not really hidden.
I also assume you are not moving them at all so its not that :) -
Ok So I have verified that the button hidden state is set to false.
Additionally, the eff is only local to the function void MainWindow::updateGUI().
In this test, I moved the variable, fadeInBackEff, to be global to the class and edited the code per your suggestion. Unfortunately, the results are the same.
-
@philm001
Hi
ok. i should have mention to use
http://doc.qt.io/qt-5/qwidget.html#graphicsEffect
to make it faster to test.So we are pretty sure that button is not moved and really not hidden so it must
be QGraphicsOpacityEffect in some way.Would it be possible to have your project to play with ?
-
Great News!
I was able to figure the issue out. Using your suggestion, I found a method that disabled the effect.
Now, once the GUI is finished updating, the code turns off the effect and everything works fine now.
void MainWindow::updateGUIComplete() { switch(p_GUIState) { case systemState::PHYSICS_CHOOSING: p_backButton->graphicsEffect()->setEnabled(false); p_finishButton->graphicsEffect()->setEnabled(false); break; } }
-
@philm001
Ahh good thinking.
I was not considering you could also just disable effect :)
Thank you for updating thread. will surely be handy for others. -
@philm001 That's what I figured ... I remember having that same problem years ago, but I couldn't remember the fix. I think I just ended up calling
pop
off my animation stack and cleaning them up.Glad it's solved. :)