Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>windowOpacity</code> property only applies to top level windows so it won't help you with animating transparency on child widgets unfortunately.</p> <p>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 :)</p> <p>To set the transparency of a <code>QPushButton</code>, 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 a <code>QPropertyAnimation</code>, you can create your own custom property and animate that.</p> <p>Below is some code that specifies a custom property for a <code>MainWindow</code> called <code>alpha</code>. The alpha value is used to set the alpha portion of the button color. With this property in place, we can use <code>QPropertyAnimation</code> 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.</p> <p><strong>MainWindow.h:</strong></p> <pre><code>#ifndef MAINWINDOW_H #define MAINWINDOW_H #include &lt;QWidget&gt; #include &lt;QPushButton&gt; class MainWindow : public QWidget { Q_OBJECT Q_PROPERTY(int alpha READ alpha WRITE setAlpha); public: MainWindow(); virtual ~MainWindow(); private: int m_alpha; QPushButton * m_button1, *m_button2; int alpha() const; void setAlpha(const int a_alpha); }; #endif /* MAINWINDOW_H */ </code></pre> <p><strong>MainWindow.cpp:</strong> <em>(Updated to include stylesheet transparency example)</em></p> <pre><code>#include &lt;QPlastiqueStyle&gt; #include &lt;QPropertyAnimation&gt; #include "MainWindow.h" MainWindow::MainWindow() : m_button1(0), m_button2(0), m_alpha(255) { resize(200, 200); QPalette windowPalette(palette()); windowPalette.setBrush(QPalette::Background, QBrush(QColor(200, 0, 0))); setPalette(windowPalette); m_button1 = new QPushButton(this); m_button1-&gt;setText("Palette Transparency"); m_button1-&gt;setAutoFillBackground(false); // NOTE: Changing the button background color does not work with XP Styles // so we need to use a style that allows it. m_button1-&gt;setStyle(new QPlastiqueStyle()); m_button2 = new QPushButton(this); m_button2-&gt;move(0, 50); m_button2-&gt;setText("Stylesheet Transparency"); m_button2-&gt;setAutoFillBackground(false); m_button2-&gt;setStyle(new QPlastiqueStyle()); QPropertyAnimation *animation = new QPropertyAnimation(this, "alpha"); animation-&gt;setDuration(1000); animation-&gt;setKeyValueAt(0, 255); animation-&gt;setKeyValueAt(0.5, 100); animation-&gt;setKeyValueAt(1, 255); animation-&gt;setLoopCount(-1); animation-&gt;start(); } MainWindow::~MainWindow() { } int MainWindow::alpha() const { return m_alpha; } void MainWindow::setAlpha(const int a_alpha) { m_alpha = a_alpha; QPalette buttonPalette(m_button1-&gt;palette()); QColor buttonColor(buttonPalette.button().color()); buttonColor.setAlpha(m_alpha); buttonPalette.setBrush(QPalette::Button, QBrush(buttonColor)); m_button1-&gt;setPalette(buttonPalette); QString stylesheet("background-color: rgba(0,200,0," + QString::number(m_alpha) + ");"); m_button2-&gt;setStyleSheet(stylesheet); } </code></pre> <p><strong>main.cpp:</strong></p> <pre><code>#include &lt;QtGui/QApplication&gt; #include "MainWindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow m; m.show(); return app.exec(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload