QPropertyAnimation makes my widget disappear
Unsolved
General and Desktop
-
@jsulm
here is my code#include "Widget.h" #include "ui_Widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget), m_timer(new QTimer) { ui->setupUi(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(OnTimer())); } Widget::~Widget() { delete ui; } void Widget::StartTimer() { m_time = ui->lineEdit->text().toInt(); m_past = 0; // m_isCheck = false; // m_isStartTime = true; ui->label->setText(QString::number((m_time - m_past))); m_timer->start(1000); } void Widget::OnTimer() { m_past++; if(m_past != m_time) { ui->label->setText(QString::number((m_time - m_past))); } else { m_timer->stop(); TimeOut(); } } void Widget::SetTime(int time) { m_time = time; } void Widget::TimeOut() { this->setWindowFlags(Qt::WindowStaysOnTopHint); ui->label->setText("Time out!"); // onShakeWindow(); onTwinkle(); } int Widget::alpha() const { return m_alpha; } void Widget::setAlpha(const int alpha) { m_alpha = alpha; QString strQSS = QString("color: rgb(0, 160, 230); background-color: rgba(10, 160, 105, %1);").arg(m_alpha); ui->label->setStyleSheet(strQSS); } void Widget::onShakeWindow() { QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos"); pAnimation->setDuration(500); pAnimation->setLoopCount(2); int x = geometry().x(); int y = geometry().y(); x = 10; y = 10; pAnimation->setKeyValueAt(0, QPoint(x - 3, y - 3)); pAnimation->setKeyValueAt(0.1, QPoint(x + 6, y + 6)); pAnimation->setKeyValueAt(0.2, QPoint(x - 6, y + 6)); pAnimation->setKeyValueAt(0.3, QPoint(x + 6, y - 6)); pAnimation->setKeyValueAt(0.4, QPoint(x - 6, y - 6)); pAnimation->setKeyValueAt(0.5, QPoint(x + 6, y + 6)); pAnimation->setKeyValueAt(0.6, QPoint(x - 6, y + 6)); pAnimation->setKeyValueAt(0.7, QPoint(x + 6, y - 6)); pAnimation->setKeyValueAt(0.8, QPoint(x - 6, y - 6)); pAnimation->setKeyValueAt(0.9, QPoint(x + 6, y + 6)); pAnimation->setKeyValueAt(1, QPoint(x- 3, y - 3)); pAnimation->start(QAbstractAnimation::DeleteWhenStopped); } void Widget::onTwinkle() { QPropertyAnimation* pa = new QPropertyAnimation(); pa->setTargetObject(this); pa->setPropertyName("alpha"); pa->setDuration(5); pa->setLoopCount(5); pa->setKeyValueAt(0, 255); pa->setKeyValueAt(0.5, 100); pa->setKeyValueAt(1, 255); pa->start(); } //void Widget::on_pushButton_clicked() //{ // if(m_isStartTime == false) // { // return; // } // m_isCheck = true; // m_isStartTime = false; //} void Widget::on_pushButton_2_clicked() { StartTimer(); }
-
@Donation I'm pretty sure
alpha
is not a valid property of a QWidget, you're probably looking foropacity
edit: nope, opacity does not exist either, one is supposed to use https://doc.qt.io/qt-5/qgraphicsopacityeffect.html
-
@Donation
not for me, works perfectly fine.int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget w; w.resize(100, 100); w.show(); QPropertyAnimation *p= new QPropertyAnimation(); p->setTargetObject(&w); p->setPropertyName("pos"); p->setDuration(2000); p->setLoopCount(-1); int x = 100; int y = 100; p->setKeyValueAt(0, QPoint(x - 3, y - 3)); p->setKeyValueAt(0.1, QPoint(x + 6, y + 6)); p->setKeyValueAt(0.2, QPoint(x - 6, y + 6)); p->setKeyValueAt(0.3, QPoint(x + 6, y - 6)); p->setKeyValueAt(0.4, QPoint(x - 6, y - 6)); p->setKeyValueAt(0.5, QPoint(x + 6, y + 6)); p->setKeyValueAt(0.6, QPoint(x - 6, y + 6)); p->setKeyValueAt(0.7, QPoint(x + 6, y - 6)); p->setKeyValueAt(0.8, QPoint(x - 6, y - 6)); p->setKeyValueAt(0.9, QPoint(x + 6, y + 6)); p->setKeyValueAt(1, QPoint(x- 3, y - 3)); p->start(); return app.exec(); }