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所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...
随机推荐
- Java_循环
遍历数组: 一个栗子: public class Test01 { public static void main(String[] args) { int[] aa = {19,92,12,03,4 ...
- [LeetCode] Loud and Rich 聒噪与富有
In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...
- mysql 水平分表
新建10张表,user_0,user_1,...user_9,方法不可串用,采用hash或取余法,获取要操作的表名,取值用对应存值的方法 1.hash取余法 public function part_ ...
- VS启动Winform项目提示:不支持互操作调试
64 位平台不支持互操作调试(托管 + 非托管混合模式调试). 在VS中设置项目属性--->调试--->取消选中“启用本地代码调试”. 此问题在.NET FrameWork低版本框架会出现 ...
- unittest中的测试固件
运行下面的两段代码,看看有什么不同? 第一段: import unittest from selenium import webdriver class F2(unittest.TestCase): ...
- [Zephyr] 1、在linux上安装Zephyr-OS并跑DEMO
星期五, 14. 九月 2018 02:18上午 - BEAUTIFULZZZZ 0) 前言 Zephyr™项目是一个采用Apache 2.0协议许可,Linux基金会托管的协作项目.为所有资源受限设 ...
- 1.1 What is the plug-in?
A game center, such as Lianzhong in China, supports hundreds of games such as Chess, Bridges, ...
- 举例子来说明Python引用和对象
今天看到这么一句奇怪的话: python中变量名和对象是分离的:最开始的时候是看到这句话的时候没有反应过来.决定具体搞清楚一下python中变量与对象之间的细节.(其实我感觉应该说 引用和对象分离 更 ...
- [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- CDN边缘节点容器调度实践(上)
又拍云容器云是基于 Docker 的分布式计算资源网,节点分散在全国各地及海外,提供电信.联通.移动和多线网络,融合微服务.DevOps 理念,满足精益开发.运维一体化,大幅降低分布式计算资源构建复杂 ...