前言
为什么需要设计自定义提示框呢? 1.Qt自带的提示框样式单一; 2.提示框的太小; 3.界面风格跟项目的不搭配;
程序执行效果源码下载
https://gitee.com/jiang_bin_yu/qt---custom-prompt-box
程序源码
使用说明 参数info:输入你需要显示的提示内容 参数parent:指定此对话框的父类,即窗口会根据父类窗口大小自适应 参数exec:是否使用模态方式显示 参数confirmName:确认按钮显示的文本,默认是确 定
void showMessageBoxInfo(const QString &info,QWidget *parent,bool exec = true,const QString &confirmName="确 定");
程序应用
void MainWindow::on_pushButton_showInfo_clicked()
{
myDialog::Instance()->setAnimationDirection(QtMaterialJbyDialog::UPTODOWN);
myDialog::Instance()->showMessageBoxInfo("通过单例方式显示的\r\n提示框",this,true);
}
void MainWindow::on_pushButton_showQuestion_clicked()
{
myDialog::Instance()->setAnimationDirection(QtMaterialJbyDialog::RIGHTTOLEFT);
int dlg = myDialog::Instance()->showMessageBoxQuestion("是否看懂了\r\n此程序",this,true,"简 单!","讲个der~?");
if(dlg == QMessageBox::Yes)
{
qDebug() << "用户看懂了";
}
if(dlg == QMessageBox::No)
{
qDebug() << "用户是个老六 没看懂";
}
}
void MainWindow::on_pushButton_showError_clicked()
{
myDialog::Instance()->setAnimationDirection(QtMaterialJbyDialog::LEFTTORIGHT);
myDialog::Instance()->showMessageBoxError("通过单例方式显示的\r\n提示框",this,true);
}
输入框继承说明 myDialog继承QtMaterialJbyDialog QtMaterialJbyDialog继承QtMaterialOverlayWidget QtMaterialOverlayWidget继承QWidget 输入框各父类对应功能讲解 1.QtMaterialOverlayWidget 主要实现窗口与父类窗口大小一致
bool QtMaterialOverlayWidget::event(QEvent *event)
{
if (!parent()) {
return QWidget::event(event);
}
switch (event->type())
{
case QEvent::ParentChange:
{
parent()->installEventFilter(this);
setGeometry(overlayGeometry());
break;
}
case QEvent::ParentAboutToChange:
{
parent()->removeEventFilter(this);
break;
}
default:
break;
}
return QWidget::event(event);
}
2.QtMaterialJbyDialog 主要负责界面动画 动画1:界面背景模板的淡入淡出
void QtMaterialJbyDialog::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::black);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.setOpacity(m_opacity);
painter.drawRect(rect());
}
Q_PROPERTY(qreal opacity WRITE setOpacity READ opacity)
动画2:窗口移动
enum ANIMATIONDIRECTION
{
UPTODOWN = 0,
DOWNTOUP = 1,
RIGHTTOLEFT = 2,
LEFTTORIGHT = 3
};
showAnimation = new QPropertyAnimation(mainWidget, "geometry",this);
showAnimation->setDuration(AnimationTime);
showAnimation->setEasingCurve(QEasingCurve::InCirc);
if(hideAnimation)
delete hideAnimation;
hideAnimation = new QPropertyAnimation(mainWidget, "geometry",this);
hideAnimation->setDuration(AnimationTime);
hideAnimation->setEasingCurve(QEasingCurve::InCirc);
connect(hideAnimation, SIGNAL(finished()),this, SLOT(slotHideAnimationFinish()));
if(m_animationDirection == UPTODOWN)
{
hideAnimation->setStartValue(QRect((mainRect.width()-rect.width())/2, (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
hideAnimation->setEndValue(QRect((mainRect.width()-rect.width())/2,0,rect.width(), rect.height()));
}else if(m_animationDirection == DOWNTOUP)
{
hideAnimation->setStartValue(QRect((mainRect.width()-rect.width())/2, (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
hideAnimation->setEndValue(QRect((mainRect.width()-rect.width())/2,mainRect.height()-rect.height(),rect.width(), rect.height()));
}else if(m_animationDirection == RIGHTTOLEFT)
{
hideAnimation->setStartValue(QRect((mainRect.width()-rect.width()), (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
hideAnimation->setEndValue(QRect(mainRect.width(),(mainRect.height()-rect.height())/2,rect.width(), rect.height()));
}else if(m_animationDirection == LEFTTORIGHT)
{
hideAnimation->setStartValue(QRect(0, (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
hideAnimation->setEndValue(QRect(-rect.width(),(mainRect.height()-rect.height())/2,rect.width(), rect.height()));
}
myDialog 负责提示框的显示
int myDialog::showDialog(const QString &info, QWidget *parent, bool exec, const QString &confirmName, const QString &cancleName)
{
if(info == m_Message)
return QMessageBox::Cancel;
if(exec && !this->isHidden())
{
qDebug() << "showMessageBoxInfo 已经有一个了";
myDialog *dlg = new myDialog();
return dlg->showMessageBoxQuestion(info,parent,exec);
}
setParent(parent);
m_Message = info;
ui->pushButton_confirm->setText(confirmName);
ui->pushButton_cancle->setText(cancleName);
ui->pushButton_confirm->show();
ui->pushButton_cancle->show();
ui->label_Tip->setText(info);
update();
if(exec)
{
m_Message.clear();
QtMaterialJbyDialog::exec();
}
else
{
QtMaterialJbyDialog::showDialog();
}
return DlgState();
}
特别需要注意的地方 myDialog 使用方法 1.设置动画窗口
QtMaterialJbyDialog::->setMainWidget(ui->widget_main);
2.设置动画方向
QtMaterialJbyDialog::setAnimationDirection(QtMaterialJbyDialog::UPTODOWN);
3.设置父类
setParent(parent);
|