1.效果
2.简述
静态方法实现,不用实例化。
自动居中父控件,父控件传nullptr在屏幕居中。
调用方式showHintLabel(nullptr,“设置错误”);
showHintLabel(nullptr,“设置错误,请设置有效的值,\n范围为20~100;”);
定义
static void widgetCenter(QWidget *pWidget,QWidget*parent = nullptr);
static void showHintLabel(QWidget *parent,QString strText,QString strFontSize = "12px",QString strColor = "#ffffff",QString strBgColor = "#000000"); //一个提示便签 会淡化消失
实现
void widgetCenter(QWidget *pWidget, QWidget *parent)
{
QSize parentSize = (nullptr == parent) ? QApplication::desktop()->screenGeometry().size() : parent->size(); //双屏情况下在主屏幕上提示
QSize subSize = parentSize - pWidget->size();
pWidget->move(subSize.width()/2,subSize.height()/2);
}
void showHintLabel(QWidget *parent, QString strText, QString strFontSize, QString strColor, QString strBgColor)
{
if(nullptr == parent){
parent = QApplication::desktop()->screen();
}
QFrame *pFrmBg = new QFrame(parent); //为了兼容parent为nullptr时的圆角边框 方法是背景透明 上边叠加圆角控件
QLabel *pHintLabel = new QLabel(pFrmBg);
pHintLabel->setStyleSheet(QString("QLabel{background:%1;color:%2;font:%3 SimHei;border-radius:5px;}")
.arg(strBgColor).arg(strColor).arg(strFontSize));
pHintLabel->setText(strText);
pHintLabel->setAlignment(Qt::AlignCenter);
pHintLabel->adjustSize();
pHintLabel->resize(pHintLabel->size() + QSize(60,30));
pFrmBg->resize(pHintLabel->size());
pFrmBg->setWindowFlags(Qt::FramelessWindowHint);
pFrmBg->setAttribute(Qt::WA_TranslucentBackground);
CQuickTools::widgetCenter(pFrmBg,parent);
pFrmBg->show();
QPropertyAnimation *pAnimation = new QPropertyAnimation(pFrmBg,"windowOpacity");
pAnimation->setDuration(2000);
pAnimation->setEasingCurve(QEasingCurve::InCirc);
pAnimation->setStartValue(1.0f);
pAnimation->setEndValue(0.0f);
pAnimation->start();
connect(pAnimation,&QPropertyAnimation::finished,[=]{
delete pAnimation;
delete pFrmBg;
});
}