QT 自定义提示窗口,高颜值

一 环境展示

基本通用,如果不能用,部分函数自行百度替代即可。

二 效果展示

三 创建QDialog类

这个类的好处就是可以直接性的阻塞窗口,下去可以自行研究

1 右击项目点击添加

2 点击qt 添加Qt设计师界面类

3 选择 Dialog without button

4 自定义类名,此处为 Custom_warning

四 UI设计

1 界面设计

此处UI设计比较简单

2 图片资源

由于文件加密,此处分享不出来。可以去矢量图网去查找自己想要的。

此处推荐:iconfont-阿里巴巴矢量图标库

五 源代码

1 H文件

#ifndef CUSTOM_WARNING_H
#define CUSTOM_WARNING_H

#include <QWidget>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
#include <QDialog>

namespace Ui {
class Custom_warning;
}

class Custom_warning : public QDialog
{
    Q_OBJECT

public:
    enum PUSHBUTTON_NAME{
        OK,
        NO,
        YES,
    };

    explicit Custom_warning(QString text, Custom_warning::PUSHBUTTON_NAME _type, QWidget *parent = nullptr);
    ~Custom_warning();

    static int Display(QString text, Custom_warning::PUSHBUTTON_NAME type,QWidget *parent = nullptr);
    void Ui_Init();



    QString label_text;
    PUSHBUTTON_NAME type;
    int ret_type = NO;


/*--------------无边框全部参数---------------------*/
private slots:
    void on_pushButton_close_clicked();

    void on_pushButton_clicked();

private:
    int MARGIN = 0;     //可以修改大小的距离
    bool _isleftpressed = false; //判断是否是左键点击
    int _curpos = 0;    //鼠标左键按下时光标所在区域
    QPoint _plast;      //获取鼠标左键按下时光标在全局(屏幕而非窗口)的位置


    int countRow(QPoint p);
    int countFlag(QPoint p, int row);
    void setCursorType(int flag);
    void System_Prompt();
    void paintEvent(QPaintEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void Maximization_And_Recovery();
private:
    Ui::Custom_warning *ui;
};

#endif // CUSTOM_WARNING_H

2 CPP文件

#include "custom_warning.h"
#include "ui_custom_warning.h"

Custom_warning::Custom_warning(QString _text, Custom_warning::PUSHBUTTON_NAME _type, QWidget *parent) :
    QDialog(parent),
    label_text(_text), type(_type), ui(new Ui::Custom_warning)
{
    ui->setupUi(this);

    Ui_Init();


}

Custom_warning::~Custom_warning()
{
    delete ui;
}

/**
* @brief UI init
* @retval None
*/
void Custom_warning::Ui_Init(void)
{
    this->setWindowFlags(Qt::FramelessWindowHint);      //设置为无边框窗口
    this->setAttribute(Qt::WA_TranslucentBackground);   //设置为透明

    //禁用右键菜单
    ui->label_icon->setContextMenuPolicy(Qt::NoContextMenu);
    ui->label_text->setContextMenuPolicy(Qt::NoContextMenu);
    setWindowModality(Qt::ApplicationModal); //阻塞除当前窗体之外的所有的窗体

    QPixmap originalPixmap(":/pages/custom_control/Custom_warning/messageBox_warning.png"); // 加载原始图片
    QPixmap scaledPixmap = originalPixmap.scaled(40, 40, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 平滑缩放图片保持长宽比
    ui->label_icon->setPixmap(scaledPixmap); // 设置缩放后的图片给 QLabel

    ui->pushButton_close->setIcon(QIcon(":/window_close.png"));

    ui->label_text->setText(label_text);

    switch (type)
    {
        case PUSHBUTTON_NAME::OK:
            ui->pushButton->setText("OK");
            break;
        case PUSHBUTTON_NAME::NO:
            ui->pushButton->setText("NO");
            break;
        case PUSHBUTTON_NAME::YES:
            ui->pushButton->setText("YES");
            break;
    }
    //提示声
    QApplication::beep();
}


/**
* @brief 获取输入框的值
* @param text:输入文本
* @param type:按键值
* @retval
*/
int Custom_warning::Display(QString text, Custom_warning::PUSHBUTTON_NAME type,QWidget *parent)
{
    Custom_warning *custom_warning = new Custom_warning(text, type ,parent);

    custom_warning->exec(); // 弹出模态对话框并等待用户操作

    return custom_warning->ret_type;
}


/**
* @brief 关闭 槽函数
* @retval None
*/
void Custom_warning::on_pushButton_close_clicked()
{
    this->close();
}

/**
* @brief 按钮 槽函数
* @retval None
*/
void Custom_warning::on_pushButton_clicked()
{
    this->close();
    ret_type = PUSHBUTTON_NAME::YES;
}


/*===========================================无边框初始化==================================================*/
//鼠标按下事件
/*
 *作用:
 *1.判断是否时左键点击 _isleftpressed
 *2.获取光标在屏幕中的位置 _plast
 *3.左键按下时光标所在区域 _curpos
 */
void Custom_warning::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (event->button() == Qt::LeftButton)
    {
        this->_isleftpressed = true;
        QPoint temp = event->globalPos();
        _plast = temp;
        _curpos = countFlag(event->pos(), countRow(event->pos()));
    }
}


//鼠标释放事件
/*
 *作用:
 *1.将_isleftpressed 设为false
 *2.将光标样式恢复原样式  setCursor(Qt::ArrowCursor);
 */
void Custom_warning::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (_isleftpressed)
        _isleftpressed = false;
    setCursor(Qt::ArrowCursor);
}


//鼠标移动事件
void Custom_warning::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if(this->isFullScreen()) return;    //窗口铺满全屏,直接返回,不做任何操作
    int poss = countFlag(event->pos(), countRow(event->pos()));
    setCursorType(poss);
    if (_isleftpressed)//是否左击
    {
        QPoint ptemp = event->globalPos();
        ptemp = ptemp - _plast;
        if (_curpos == 22)//移动窗口
        {
            ptemp = ptemp + pos();
            move(ptemp);
        }
        else
        {
            QRect wid = geometry();
            switch (_curpos)//改变窗口的大小
            {
            case 11:wid.setTopLeft(wid.topLeft() + ptemp); break;//左上角
            case 13:wid.setTopRight(wid.topRight() + ptemp); break;//右上角
            case 31:wid.setBottomLeft(wid.bottomLeft() + ptemp); break;//左下角
            case 33:wid.setBottomRight(wid.bottomRight() + ptemp); break;//右下角
            case 12:wid.setTop(wid.top() + ptemp.y()); break;//中上角
            case 21:wid.setLeft(wid.left() + ptemp.x()); break;//中左角
            case 23:wid.setRight(wid.right() + ptemp.x()); break;//中右角
            case 32:wid.setBottom(wid.bottom() + ptemp.y()); break;//中下角
            }
            setGeometry(wid);
        }
        _plast = event->globalPos();//更新位置
    }
}


//获取光标在窗口所在区域的 列  返回行列坐标
int Custom_warning::countFlag(QPoint p,int row)//计算鼠标在哪一列和哪一行
{
    if(p.y()<MARGIN)
        return 10+row;
    else if(p.y()>this->height()-MARGIN)
        return 30+row;
    else
        return 20+row;
}


//获取光标在窗口所在区域的 行   返回行数
int Custom_warning::countRow(QPoint p)
{
    return (p.x()<MARGIN) ? 1 : (p.x()>(this->width() - MARGIN) ? 3 : 2);
}


//根据鼠标所在位置改变鼠标指针形状
void Custom_warning::setCursorType(int flag)
{
    switch(flag)
    {
    case 11:
    case 33:
        setCursor(Qt::SizeFDiagCursor);
        break;
    case 13:
    case 31:
        setCursor(Qt::SizeBDiagCursor);break;
    case 21:
    case 23:
        setCursor(Qt::SizeHorCursor);break;
    case 12:
    case 32:
        setCursor(Qt::SizeVerCursor);break;
    case 22:
        setCursor(Qt::ArrowCursor);
        QApplication::restoreOverrideCursor();//恢复鼠标指针性状
        break;
    }
}


/**
* @brief 重写绘图事件
* @retval None
*/
void Custom_warning::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true); // 设置抗锯齿
//    painter.setPen(QPen(QColor(180, 179, 181), 5)); // 设置边框颜色和宽度为

    // 创建线性渐变色
    QLinearGradient gradient(0, 0, 0, this->height());
    gradient.setColorAt(0, QColor(245, 245, 247)); // 起始颜色
    gradient.setColorAt(1, QColor(240, 239, 238)); // 终止颜色

    // 使用渐变色填充背景
    painter.setBrush(gradient);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(0, 0, this->width(), this->height(), 10, 10); // 绘制圆角矩形
}


3 UI文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Custom_warning</class>
 <widget class="QDialog" name="Custom_warning">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>155</width>
    <height>101</height>
   </rect>
  </property>
  <property name="maximumSize">
   <size>
    <width>16777215</width>
    <height>101</height>
   </size>
  </property>
  <property name="font">
   <font>
    <pointsize>12</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="styleSheet">
   <string notr="true">QPushButton#pushButton{
	border: 2px solid #a7a7a7;
	padding: 2px 20px; /*增加按钮的内边距,使其扩大*/
}

QPushButton{
    border-radius: 4px;
    /*background-color: rgb(225, 225, 225);*/
    padding: 3px 23px; /*增加按钮的内边距,使其扩大*/
}

QPushButton#pushButton_close{
	background-color: rgb(255, 255, 255);
}

QPushButton#pushButton_close:pressed {
	background-color: rgb(173, 180, 189);
}

QPushButton::pressed,QPushButton::checked{
    background-color: rgb(173, 180, 189);/*按钮按下的颜色*/
}
</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <layout class="QVBoxLayout" name="verticalLayout">
     <property name="topMargin">
      <number>0</number>
     </property>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton_close">
         <property name="minimumSize">
          <size>
           <width>23</width>
           <height>23</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>23</width>
           <height>23</height>
          </size>
         </property>
         <property name="text">
          <string/>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,9">
       <property name="topMargin">
        <number>0</number>
       </property>
       <property name="rightMargin">
        <number>0</number>
       </property>
       <property name="bottomMargin">
        <number>0</number>
       </property>
       <item>
        <widget class="QLabel" name="label_icon">
         <property name="font">
          <font>
           <family>Arial</family>
           <pointsize>11</pointsize>
          </font>
         </property>
         <property name="text">
          <string>IOCN</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QLabel" name="label_text">
         <property name="font">
          <font>
           <family>Arial</family>
           <pointsize>11</pointsize>
          </font>
         </property>
         <property name="text">
          <string>TEXT</string>
         </property>
         <property name="alignment">
          <set>Qt::AlignCenter</set>
         </property>
        </widget>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_3">
       <item>
        <spacer name="horizontalSpacer_4">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item>
        <widget class="QPushButton" name="pushButton">
         <property name="font">
          <font>
           <family>Arial</family>
           <pointsize>11</pointsize>
          </font>
         </property>
         <property name="text">
          <string>按钮</string>
         </property>
        </widget>
       </item>
       <item>
        <spacer name="horizontalSpacer_5">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
      </layout>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

4 UI文件 样式表(UI漂亮很重要)

QPushButton#pushButton{
	border: 2px solid #a7a7a7;
	padding: 2px 20px; /*增加按钮的内边距,使其扩大*/
}

QPushButton{
    border-radius: 4px;
    /*background-color: rgb(225, 225, 225);*/
    padding: 3px 23px; /*增加按钮的内边距,使其扩大*/
}

QPushButton#pushButton_close{
	background-color: rgb(255, 255, 255);
}

QPushButton#pushButton_close:pressed {
	background-color: rgb(173, 180, 189);
}

QPushButton::pressed,QPushButton::checked{
    background-color: rgb(173, 180, 189);/*按钮按下的颜色*/
}

六 使用实例

此函数可以返回是否点击确认值

包含头文件之后
Custom_warning::Display("大于72h需要重新激活", Custom_warning::YES);
### 回答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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值