How to create Fade in, Fade out effect in Qt



  • Need guidence in qt, how to create special effect where QPushButton fades out and taken over by
    QRadioButton in Qt one by one.
    Not sure which class can achieve this effect.

    @
    //Below is the grid with 3 buttons
    QGridLayout *gridLayout = new QGridLayout;
    QPushButton *b1 = new QPushButton("A");
    QPushButton *b2 = new QPushButton("B");
    QPushButton *b3 = new QPushButton("C");
    QPushButton *b4 = new QPushButton("D");
    QPushButton *b5 = new QPushButton("E");
    QPushButton *b6 = new QPushButton("F");

    // row 0
    gridLayout->addWidget(b1,0,0,1,1);
    gridLayout->addWidget(b2,0,1,1,1);
    gridLayout->addWidget(b3,0,2,1,1);

    // row 1
    gridLayout->addWidget(b4,1,0,1,1);

    // row 2 with 2 column span
    gridLayout->addWidget(b5,2,0,1,2);

    //row 3 with 3-column span
    gridLayout->addWidget(b6,3,0,1,3);

    //create a widget
    QWidget *w = new QWidget();
    w->setLayout(gridLayout);

    //Window Title
    w->setWindowTitle("Grid Layout 3 x4");
    //Display
    w->show();

    }

    @



  • Could you explain a bit more what you want to archive ?

    On Button Clicked the Button should disappear and at the same place there should then be a radio button ?



  • In QGridlayout, how to replace item A with item B



  • Not quit sure if this is what you expect. But i think you could simply use it for your approach.

    @class FadeOutButton : public QWidget
    {
    Q_OBJECT
    public:
    explicit FadeOutButton(QWidget *parent = 0);
    ~FadeOutButton();

    signals:

    private slots:
    void onButtonClicked();
    void onAnimationFinished();
    private :
    QPushButton* mButton;
    QRadioButton * mRadioButton;
    QGraphicsOpacityEffect* mEffect;
    };@

    @FadeOutButton::FadeOutButton(QWidget *parent) : QWidget(parent)
    {

    QGridLayout* layout = new QGridLayout(this);
    layout->setContentsMargins(0,0,0,0);
    
    mButton = new QPushButton("Button",this);
    mRadioButton = new QRadioButton(this);
    
    layout->addWidget(mButton,0,0,1,1);
    layout->addWidget(mRadioButton,0,0,1,1);
    
    mEffect = new QGraphicsOpacityEffect(mButton);
    mEffect->setOpacity(1.0);
    mButton->setGraphicsEffect(mEffect);
    
    mRadioButton->setVisible(false);
    connect(mButton,SIGNAL(clicked()),this,SLOT(onButtonClicked()));
    

    }

    FadeOutButton::~FadeOutButton()
    {

    }

    void FadeOutButton::onButtonClicked()
    {
    QPropertyAnimation* animation = new QPropertyAnimation(mEffect,"opacity");
    animation->setDuration(500);
    animation->setStartValue(1.0);
    animation->setEndValue(0.0);
    connect(animation,SIGNAL(finished()),this,SLOT(onAnimationFinished()));
    animation->start(QAbstractAnimation::DeleteWhenStopped);
    }

    void FadeOutButton::onAnimationFinished()
    {
    mButton->setVisible(false);
    mRadioButton->setVisible(true);

    }@



  • it work :> thanks alot


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.