QT 自定义模态对话框
新建一个MsgBox类
msgbox.h 代码
#ifndef MSGBOX_H
#define MSGBOX_H #include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QMouseEvent>
#include <QFont>
#include <QPixmap>
#include <QPainter>
#include<QBitmap> class MsgBox : public QDialog
{
Q_OBJECT public:
MsgBox(QWidget *parent = );
~MsgBox(); public:
QPushButton *ok_button;
QPushButton *close_button;
QPushButton *cancel_button;
QLabel *ask_label;
QLabel *msg_label;
QLabel *title_label;
QString ok_text;
QString cancel_text; public:
void setInfo(QString title_info, QString info,QPixmap pixmap, bool is_ok_hidden,QString language);
protected:
QPoint move_point;
bool mouse_press; void paintEvent(QPaintEvent *);
void mousePressEvent( QMouseEvent * event );
void mouseReleaseEvent( QMouseEvent *);
void mouseMoveEvent(QMouseEvent *event);
public slots:
void okOperate();
void cancelOperate();
void closeOperate();
}; #endif // MSGBOX_H
msgbox.cpp 代码
#include "msgbox.h" MsgBox::MsgBox(QWidget *parent)
: QDialog(parent)
{
this->resize(, ); //获取主界面的宽度
int width = this->width();
int height = this->height(); //初始化为未按下鼠标左键
mouse_press = false; //设置标题栏隐藏
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog); close_button = new QPushButton(this); //close_button->loadPixmap(":/image/images/bg.png");
close_button->setGeometry(width-,, , );
close_button->setStyleSheet("border-image:url(:/image/images/closeBtn.png);"); //设置标题
title_label = new QLabel(this);
title_label->setObjectName(QString::fromUtf8("labelOne"));
QFont font = title_label->font();
font.setBold(true);
title_label->setFont(font);
title_label->setGeometry(, , width-, ); //设置提示图片
msg_label = new QLabel(this);
msg_label->setGeometry(, , , );
msg_label->setScaledContents(true); //设置提示信息,让QLabel能够自动判断并换行显示:
ask_label = new QLabel(this);
ask_label->setGeometry(, , width-, *);
ask_label->setWordWrap(true);
ask_label->setAlignment(Qt::AlignTop); cancel_button = new QPushButton(this);
cancel_button->resize(, );
cancel_button->move(width - cancel_button->width() - , height - ); ok_button = new QPushButton(this);
ok_button->resize(, );
ok_button->move(width - ok_button->width() - cancel_button->width() - , height - ); ok_button->setObjectName(QString::fromUtf8("pushButtonTwo"));
cancel_button->setObjectName(QString::fromUtf8("pushButtonTwo")); QObject::connect(ok_button, SIGNAL(clicked()), this, SLOT(okOperate()));
QObject::connect(close_button, SIGNAL(clicked()), this, SLOT(closeOperate()));
QObject::connect(cancel_button, SIGNAL(clicked()), this, SLOT(cancelOperate()));
} MsgBox::~MsgBox()
{ } //设置对话框信息
void MsgBox::setInfo(QString title_info, QString info,QPixmap pixmap,bool is_ok_hidden,QString language)
{
title_label->setText(QString(" ") + title_info); //设置提示信息
ask_label->setText(info);
msg_label->setPixmap(pixmap); //是否隐藏确定按钮
ok_button->setHidden(is_ok_hidden);
if(is_ok_hidden)
{
if(language == "中文")
{
cancel_button->setText(tr("确定"));
}
else if(language == "英文")
{
cancel_button->setText(tr("OK"));
}
else if(language == "泰文")
{
cancel_button->setText(tr("แน่ใจว่า"));
}
}
else
{
if(language == "中文")
{
ok_button->setText(tr("确定"));
cancel_button->setText(tr("取消"));
}
else if(language == "英文")
{
ok_button->setText(tr("OK"));
cancel_button->setText(tr("Cancel"));
}
else if(language == "泰文")
{
ok_button->setText(tr("แน่ใจว่า"));
cancel_button->setText(tr("การยกเลิก"));
}
} //设置默认按钮为取消按钮
cancel_button->setFocus();
} void MsgBox::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(rect(), QPixmap(":/image/images/close.png")); QBitmap bitmap(this->size());
QPainter painter2(&bitmap);
painter2.fillRect(bitmap.rect(), Qt::white);
painter2.setBrush(QColor(, , ));
painter2.drawRoundedRect(rect(), , );
setMask(bitmap);
} void MsgBox::mousePressEvent( QMouseEvent * event )
{
//只能是鼠标左键移动和改变大小
if(event->button() == Qt::LeftButton)
{
mouse_press = true;
} //窗口移动距离
move_point = event->globalPos() - pos();
} void MsgBox::mouseReleaseEvent( QMouseEvent *)
{
mouse_press = false;
} void MsgBox::mouseMoveEvent(QMouseEvent *event)
{
//移动窗口
if(mouse_press)
{
QPoint move_pos = event->globalPos();
move(move_pos - move_point);
}
} //确认操作
void MsgBox::okOperate()
{
this->accept();
} //取消操作
void MsgBox::cancelOperate()
{
this->reject();
} //关闭窗体操作
void MsgBox::closeOperate()
{
close();
}
主函数调用代码:
#include "msgbox.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MsgBox w; w.setInfo(QString("提示:"),QString("是否关机?"),QPixmap(":/image/images/tip.png"),false,QString("中文"));
w.show();
return a.exec();
}
目录构建图:

实现效果图:

QT 自定义模态对话框的更多相关文章
- QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例
QT创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束的简单示例 部分代码: // 创建模态对话框阻塞整个应用程序和非模态对话框唯一性约束 QMenu *pDialog = mBar->ad ...
- C/C++ Qt 自定义Dialog对话框组件应用
在上一篇博文 <C/C++ Qt 标准Dialog对话框组件应用> 中我给大家演示了如何使用Qt中内置的标准对话框组件实现基本的数据输入功能. 但有时候我们需要一次性修改多个数据,使用默认 ...
- Qt的模态对话框和非模态对话框 经常使用setAttribute (Qt::WA_DeleteOnClose)
模态对话框就是指在子对话框弹出时,焦点被强行集中于该子对话框,子对话框不关闭,用户将无法操作其他的窗口.非模态相反,用户仍然可以操作其他的窗口,包括该子对话框的父对话框. 如果从线程角度来讲,模态对话 ...
- 微信小程序 - 自定义模态对话框
更新日期:2018-11-5 微信bug: 在for循环中使用组件时,遮罩层成黑层. 更新时间 2018-9-30 2018-9-30 1.在电脑上调试input超出输入框范围会出现文字模糊以及位移现 ...
- jQuery查找标签--选择器,筛选器,模态对话框, 左侧菜单栏
查找标签 选择器: 基本选择器(同css) id选择器 $("#id") 标签选择器 $('tagName') class选择器 $(".className") ...
- QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)
QT弹出模态对话框做法: 1.新建UI文件时,一定要选择基类是QDialog的,我的选择是:Dialog without Buttons(),如下图: 2.然后在使用的时候: MyDialog dlg ...
- QT模态对话框及非模态对话框
QT模态对话框及非模态对话框 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对 ...
- QT+模态对话框与非模态对话框
#include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAct ...
- QT笔记之模态对话框及非模态对话框
模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...
随机推荐
- vue常用属性
关键词:filters | 自定义过滤器 (首字母大写) <p>{{ msg | capitalize }}</p> filters: { capitalize: funct ...
- android利用ContentResolver访问者获取手机联系人信息
转载自:http://www.jb51.net/article/106379.htm 首先需要在AndroidManifest.xml文件中添加权限: <uses-permission andr ...
- HTTP协议概念与特点,HTTP的状态码,HTTPS是什么?
很多人在打开网页的时候,在浏览器地址栏里都会看到http ,在Java WEB里,HTTP也是个重点内容,今天我们就来详细了解和学习HTTP . HTTP是Hyper Text Transfer P ...
- [Swift]LeetCode277. 寻找名人 $ Find the Celebrity
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- [Swift]LeetCode688. “马”在棋盘上的概率 | Knight Probability in Chessboard
On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K ...
- [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree
Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...
- 【TOGAF】总体认知
TOGAF是什么 TOGAF(The Open Group Architecture Framework)是当前最为流行的企业架构框架理论之一,它为一个企业或组织对于企业架构的接受.创建.使用和维护提 ...
- Android device debug (adb) by Charge Only mode
Android device debug by Charge Only mode Method 1 Connect devices to computer and execute lsusb Find ...
- CoCos2dx开发:更换导出的app名称和图标
要处理的文件路径如下: 1.更换图标: drawable-hdpi.drawable-ldpi.drawable-mdpi三个文件夹分别代表大.小.中三个不同宽高的图片,为了应对手机的不同分辨率,来采 ...
- spring aspect获取抽象基类日志
在实际的项目开发过程中我们其实封装了很多的类似BaseService.BaseDao等的基类,然后在切日志的时候我们一般是指向继承改抽象基类的实现类的,这时候我们就会出现无法切出调用抽象基类方法的日志 ...