Qt 自定义提示框 类似QMessageBox

前言

为什么需要设计自定义提示框呢?
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);
  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Qt自定义消息提示框是指开发者可以根据自己的需求设计出符合自己应用程序风格的消息提示框,从而提高应用程序的用户体验。可以使用Qt的QWidget或QDialog等控件来实现消息的展示和弹出。一般而言,自定义消息提示框通常包括标题、消息内容和按钮,通过控制按钮的显示和响应来实现交互。同时,还需要考虑消息提示框的美观和易读性,比如可以选择一些颜色和图标来增强视觉效果,并且还需要支持多语言,方便不同国家和地区的用户使用。另外,为了方便使用,还可以将自定义消息提示框封装成一个函数或类,供应用程序中其他地方调用。总之,Qt自定义消息提示框的使用可以大大提升应用程序的用户体验,增强用户的满意度和忠诚度。 ### 回答2: QT框架提供了一个简便实用的方法来实现自定义消息提示框。首先,我们可以创建一个继承自QWidget类的消息提示框,这个类可以包含我们自定义的消息显示区域、确定按钮和取消按钮等控件。接下来,我们可以通过使用QHBoxLayout或QVBoxLayout等布局管理器来设置这些控件的位置和大小,使得它们在提示框中排版合理。同时,在确定按钮和取消按钮的点击事件中,我们可以使用accept()和reject()函数来确认或取消消息提示框的显示。 值得注意的是,在消息提示框的显示过程中,我们可以使用QPropertyAnimation等动画效果来增加提示框显示时的平滑感。另外,为了使得消息提示框更加美观,我们也可以在其样式表中设置背景、边框等相关样式。最终,我们可以通过调用自定义消息提示框类的实例来显示我们自己设计的消息提示框,在用户点击确定或取消按钮后,根据其结果来进行相应的操作。 综上所述,通过继承QWidget类并使用布局管理器、动画效果和样式表等技巧,我们可以实现自定义的消息提示框,并在QT应用程序中使用它来提供更好的用户体验。 ### 回答3: QT是一款非常强大的跨平台图形用户界面库,开发者可以使用QT框架创建丰富功能的图形化应用程序。其中,消息提示框是在应用程序中非常常用的一种控件,使用者可以在提示框中显示各种类型的消息,比如警告、错误和信息等等。本文将介绍如何使用QT自定义消息提示框。 首先,我们需要创建一个新窗口,用于显示自定义消息提示框。然后,我们可以在窗口中添加各种控件,比如QLabel、QPushButton和QMessageBox等控件。控件的颜色、大小和位置等属性可以按照自己的设计来设置。 接下来,我们需要添加一些功能,比如设置提示框的标题、消息内容和图标等。在处理这些功能时,我们可以使用QT提供的信号和槽机制。比如,在点击按钮时,我们可以发出一个信号,然后使用槽函数来处理信号并显示消息内容。 最后,我们需要将自定义的消息提示框集成到我们的应用程序中。这可以通过调用消息框的show函数来实现。如果我们希望用户能够在不关闭提示框的情况下执行其他操作,我们可以调用消息框的exec函数。 总之,使用QT自定义消息提示框是非常简单的,只需要创建一个新窗口并添加所需的控件和功能即可。同时,QT也提供了丰富的文档和示例,可以帮助我们更好地理解和使用自定义消息提示框

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jbyyy、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值