qt自定义MessageBox

头文件

#ifndef HINT_INTERFACE_H
#define HINT_INTERFACE_H

#include <QWidget>
#include <QMouseEvent>

extern bool DataBaseError;

namespace Ui {
class HintInterface;
}

class HintInterface : public QWidget
{
    Q_OBJECT

public:
    explicit HintInterface(QWidget *parent = nullptr);
    ~HintInterface();
    void setWindow(QString title, QString picture, QString content, QPoint location);

protected:
    void mouseMoveEvent(QMouseEvent *e) override;
    void mousePressEvent(QMouseEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;

signals:
    void isclose();

private slots:
    void on_pushButton_clicked();

private:
    Ui::HintInterface *ui;
    QPoint press_point;//鼠标按下去的点
    bool is_move;
};

#endif // HINT_INTERFACE_H

cpp

#include "hint_interface.h"
#include "ui_hint_interface.h"

bool DataBaseError  = false;

HintInterface::HintInterface(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HintInterface)
{
    ui->setupUi(this);
    this->setWindowTitle(QString("通知"));
    this->setWindowIcon(QIcon(QApplication::applicationDirPath() + QString("/")  +  "image/mainframe.ico"));
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    this->setWindowModality(Qt::ApplicationModal);
    this->setAttribute(Qt::WA_DeleteOnClose);
}

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

void HintInterface::setWindow(QString title, QString picture, QString content, QPoint location)
{
    this->ui->label ->setText(title);
    this->setWindowTitle(title);
    this->ui->label_2->setPixmap(QPixmap::fromImage(QImage(picture)));
    this->ui->label_2->setScaledContents(true);
    this->ui->label_3->setText(content);
    this->ui->label_3->setWordWrap(true);
    this->move(location);
    this->show();
}

void HintInterface::mouseMoveEvent(QMouseEvent *e)
{
    if((e->buttons() == Qt::LeftButton) && is_move)
    {
        QWidget::mousePressEvent(e);
        QPoint mouseP = e->globalPos();
        QPoint endP(mouseP.x() - press_point.x(), mouseP.y() - press_point.y());
        this->move(endP);
    }
}

void HintInterface::mousePressEvent(QMouseEvent *e)
{
    press_point = QPoint();
    QWidget::mousePressEvent(e);
    QPoint mouseP = e->globalPos();
    int x = this->geometry().topLeft().x();
    int y = this->geometry().topLeft().y();
    int x_w = mouseP.x() - x;
    int y_w = mouseP.y() - y;
    press_point.setX(x_w);
    press_point.setY(y_w);
    is_move = true;
}

void HintInterface::mouseReleaseEvent(QMouseEvent *e)
{
    if(is_move)
    {
       is_move = false;
    }
}

void HintInterface::on_pushButton_clicked()
{
    emit this->isclose();
}

示例:
 

        HintInterface *hint_interface = new HintInterface();
        QPoint main_point = this->mapToGlobal(QPoint((this->width() - hint_interface->width())/2, (this->height() - hint_interface->height())/2));
        hint_interface->setWindow("提示", QApplication::applicationDirPath() + QString("/") + "image", QString("说明"), main_point);
        return;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值