天天看点

qt 弹出标签自动渐渐消失

qt 弹出标签自动渐渐消失
qt 弹出标签自动渐渐消失

​​https://github.com/Greedysky/TTKWidgetTools​​

研究网上别人代码,看到一个可能以后会用的功能

//  设置背景
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_QuitOnClose);
setAttribute(Qt::WA_DeleteOnClose);
//设置文字和大小
m_font.setPointSize(size);
QFontMetrics metrics = QFontMetrics(m_font);
setFixedSize(metrics.width(text) + m_margin.x(),
metrics.height() + m_margin.y());
QLabel::setText(text);
//paint
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 175));
painter.drawRoundRect(rect(), 6, 6);

painter.setPen(QColor(255, 255, 255));
painter.drawText(rect(), Qt::AlignCenter, text());
painter.end();      
//绑定
connect(&m_timer, SIGNAL(timeout()), SLOT(closeAnimation()));
 m_timer.setInterval(1000);
 m_timer.start();

//结束槽函数
void   closeAnimation() {
m_timer.stop();
QPropertyAnimation *animation =
new QPropertyAnimation(this, "windowOpacity", this);
animation->setDuration(1000);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
connect(animation, SIGNAL(finished()), SLOT(close()));
}