先看效果,当用户点击了错误的节点,提示用户错误,3秒后退出。
#pragma once
#include <QWidget>
#include "ui_QtGuiTip.h"
class QTimer;
class QtGuiTip : public QWidget {
Q_OBJECT
public:
QtGuiTip(QWidget *parent = Q_NULLPTR);
~QtGuiTip();
void shakeWindow();
private slots:
void slotTimeOut();
private:
Ui::QtGuiTip ui;
QTimer* _timer = nullptr;
};
#include "QtGuiTip.h"
#include <QPropertyAnimation>
#include <QTimer>
QtGuiTip::QtGuiTip(QWidget *parent)
: QWidget(parent) {
ui.setupUi(this);
_timer = new QTimer;
_timer->setTimerType(Qt::PreciseTimer);//设置定时器的精度,精确到毫秒
connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeOut()));
}
QtGuiTip::~QtGuiTip() {
}
void QtGuiTip::shakeWindow() {
show();
_timer->start(3000);
QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos");
pAnimation->setDuration(500);
pAnimation->setLoopCount(2);
pAnimation->setKeyValueAt(0, QPoint(geometry().x() - 3, geometry().y() - 3));
pAnimation->setKeyValueAt(0.1, QPoint(geometry().x() + 6, geometry().y() + 6));
pAnimation->setKeyValueAt(0.2, QPoint(geometry().x() - 6, geometry().y() + 6));
pAnimation->setKeyValueAt(0.3, QPoint(geometry().x() + 6, geometry().y() - 6));
pAnimation->setKeyValueAt(0.4, QPoint(geometry().x() - 6, geometry().y() - 6));
pAnimation->setKeyValueAt(0.5, QPoint(geometry().x() + 6, geometry().y() + 6));
pAnimation->setKeyValueAt(0.6, QPoint(geometry().x() - 6, geometry().y() + 6));
pAnimation->setKeyValueAt(0.7, QPoint(geometry().x() + 6, geometry().y() - 6));
pAnimation->setKeyValueAt(0.8, QPoint(geometry().x() - 6, geometry().y() - 6));
pAnimation->setKeyValueAt(0.9, QPoint(geometry().x() + 6, geometry().y() + 6));
pAnimation->setKeyValueAt(1, QPoint(geometry().x() - 3, geometry().y() - 3));
pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
void QtGuiTip::slotTimeOut() {
hide();
}
调用:
初始化先隐藏:
_guiTip = new QtGuiTip(this);
_guiTip->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
_guiTip->hide();
调用的位置:
_guiTip->move(0, 0);
_guiTip->shakeWindow();
设置窗口的大小:
void resizeEvent(QResizeEvent * event)override;
void DragTreeWidget::resizeEvent(QResizeEvent * event) {
_guiTip->move(0, 0);
_guiTip->resize(width(), 50);
}