qt 彈出標籤自動漸漸消失
阿新 • • 發佈:2022-05-17
https://github.com/Greedysky/TTKWidgetTools
研究網上別人程式碼,看到一個可能以後會用的功能
彈出一個label 過段時間自動淡淡退出
先自定義一個label
// 設定背景 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())); }