概述
Qt自定义MessageBox窗口
必要元素及其功能
- 标题栏(表头,关闭按钮,自由拖动)
- 内容(不同的图片,提示内容,取消确认按钮)
- 状态阻塞式的模态窗口
#pragma once
#include <QDialog>
#include <QObject>
#include <QLabel>
#include <QPushButton>
#include <QToolButton>
#include <QMouseEvent>
class msg_box_t: public QDialog
{
Q_OBJECT
public:
msg_box_t(QString);
~msg_box_t();
protected:
QPoint move_point;
bool mouse_press;
void mousePressEvent(QMouseEvent*);
void mouseReleaseEvent(QMouseEvent*);
void mouseMoveEvent(QMouseEvent*);
public slots:
void ok_btn_press();
void cancle_btn_press();
void close_btn_press();
};
#include "msg_box.hpp"
msg_box_t::msg_box_t(QString text)
{
this->resize(360,160);
this->setObjectName("msg");
int width = this->width();
int height = this->height();
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
auto title_lbl_ = new QLabel(this);
title_lbl_->setObjectName("title_label_");
title_lbl_->setGeometry(0, 0, width, 32);
title_lbl_->setText(u8"提示");
auto close_btn_ = new QPushButton(this);
close_btn_->setObjectName("setting_button");
close_btn_->setToolTip(u8"关闭");
close_btn_->setGeometry(width - 33, 1, 32, 32);
close_btn_->setIcon(QIcon(":/images/close.png"));
auto info_lbl_ = new QLabel(this);
info_lbl_->setObjectName("title_label_");
info_lbl_->setText(text);
info_lbl_->setGeometry(50, 30, width-100, 70);
info_lbl_->setAlignment(Qt::AlignCenter);
info_lbl_->setWordWrap(true);
auto ok_btn_ = new QPushButton(this);
ok_btn_->setText(u8"确定");
ok_btn_->setGeometry(70, height - 20 -32, 100, 32);
ok_btn_->setObjectName("msg_btn");
auto cancle_btn_ = new QPushButton(this);
cancle_btn_->setText(u8"取消");
cancle_btn_->setGeometry(190, height - 20 - 32, 100, 32);
cancle_btn_->setObjectName("msg_btn");
connect(close_btn_, SIGNAL(clicked()), this, SLOT(close_btn_press()));
connect(ok_btn_, SIGNAL(clicked()), this, SLOT(ok_btn_press()));
connect(cancle_btn_, SIGNAL(clicked()), this, SLOT(cancle_btn_press()));
}
msg_box_t::~msg_box_t()
{
}
void msg_box_t::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
mouse_press = true;
move_point = event->pos();
}
}
void msg_box_t::mouseReleaseEvent(QMouseEvent*)
{
mouse_press = false;
}
void msg_box_t::mouseMoveEvent(QMouseEvent* event)
{
if (mouse_press)
{
QPoint move_pos = event->globalPos();
this->move(move_pos - move_point);
}
}
void msg_box_t::ok_btn_press()
{
this->accept();
}
void msg_box_t::cancle_btn_press()
{
this->reject();
}
void msg_box_t::close_btn_press()
{
close();
}
最后
以上就是愤怒花卷为你收集整理的Qt自定义MessageBox窗口的全部内容,希望文章能够帮你解决Qt自定义MessageBox窗口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复