I want to progressively decrease the opacity of a QPushButton over a time of 2 seconds to complete transparency. For that I used the QPropertyAnimation class and used the property "windowOpacity" of the button to achieve the effect. But that worked only for a standalone QPushButton. When I assigned a parent to the button, the effect disappeared. Is there any way of achieving the same effect for child buttons ?
相关问题
- QML: Cannot read property 'xxx' of undefin
- QTextEdit.find() doesn't work in Python
- QT Layouts, how to make widgets in horizontal layo
- QT GUI freezes even though Im running in separate
- QGraphicsView / QGraphicsScene size matching
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- Why doesn't valgrind detect a memory leak in m
- Keep constant number of visible circles in 3D anim
- QTreeView remove decoration/expand button for all
- qt界面拥挤
I faced the same problem a while ago and came to basically the same solution(manipulating the controls palette). But, while the helper property in the MainWindow is surely a quick and easy solution, it's a dirty one too. So, at least for larger and reoccurring usage it seamed much more appropriate to create a new animation class covering those needs. This isn't much more code(simply inherit QAbstractAnimation, move that palette stuff in there and pass the target control as a parameter into that class) but it keeps your parent control(like the mainwindow-class) free from such animation implementation details which surely don't belong in there.
The
windowOpacity
property only applies to top level windows so it won't help you with animating transparency on child widgets unfortunately.Standard controls are a bit problematic as well as there are many considerations contributing to their final appearance. There are many approaches you could take but they will all involve a certain amount of coding. There is no easy way :)
To set the transparency of a
QPushButton
, you would need to either set a stylesheet for it, or change some of the properties of the palette. Since neither of these options are directly usable by aQPropertyAnimation
, you can create your own custom property and animate that.Below is some code that specifies a custom property for a
MainWindow
calledalpha
. The alpha value is used to set the alpha portion of the button color. With this property in place, we can useQPropertyAnimation
to animate it. The result is a button that fades in and out. This only handles the buttons background and not the text but it should provide a starting point for you.MainWindow.h:
MainWindow.cpp: (Updated to include stylesheet transparency example)
main.cpp: