核心代码如下, 获取桌面,将你要显示的widget显示到桌面中间, 可以添加 QApplication::beep()进行蜂鸣提示, m_widget为自定义组件
/* 设置弹窗信息 */
void setText(const QString &color,const QString &bgcolor,
const int & mestime,const QString &text)
{
QApplication::beep(); // 调用蜂鸣
/* 弹窗信息风格 */
QFontMetrics fm(m_label->font());
int width = fm.boundingRect(text).width() + 30;
m_widget->setFixedWidth(width);
m_label->setFixedWidth(width);
m_label->setText(text);
QString style = QString("color:").append(StringToRGBA(color));
m_label->setStyleSheet(style);
m_widget->setStyleSheet(QString("border: none;border-radius:10px;")
.append("background-color:").append(StringToRGBA(bgcolor)));
/* 用于获取桌面 */
QDesktopWidget *pDesk = QApplication::desktop();
m_widget->move((pDesk->width() - m_widget->width()) / 2, (pDesk->height() - m_widget->height()) / 2);
m_widget->show(); // 显示弹窗
/* 定时器 到时间之后将弹窗自动隐藏 */
m_holdTimer.setInterval(mestime);
m_holdTimer.stop();
m_holdTimer.start();
}