C++设计模式之单例模式讲到单例模式的基本语法,本文给一个qt开发中单例模式常用的例子——消息对话框
1.为了美化用户操作界面,我们需要自定义对话框但是又想像系统QMessageBox一样的用法,直接替换
2.由于消息对话框需要到处调用,且只需出现一个窗口既只需一个对象。所以使用单例模式较为合适。
头文件
#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include <QDialog>
#include <QPoint>
enum IconType
{
Icon_None, //无图标
Icon_Information, //消息图标
Icon_Warning, //警告图标
Icon_Question, //疑问图标
Icon_Error //错误图标
};
enum ButtonType
{
Btn_None, //不显示按钮
Btn_One, //只显示1个按钮
Btn_Two, //只显示2个按钮
Btn_All //所有按钮都显示
};
enum ReturnType
{
TypeLeft = 3,
TypeCenter,
TypeRight,
};
namespace Ui {
class msgBoxDlg;
}
class msgBoxDlg : public QDialog
{
Q_OBJECT
public:
~msgBoxDlg();
/**
* 获得单例对象
* @arg btn 选择显示按钮的数量
* 确定 返回1 取消返回0 退出返回2
* @arg icon 选择显示图标
* @arg title 标题
* @arg content 内容
* @arg leftBtnName 更改确定按钮的显示文本
* @arg centerBtnName 更改取消按钮的显示文本
* @arg rightBtnName 更改退出按钮的显示文本
* @arg highLight 默认高亮按钮
* 0为左边按钮 1为中间按钮 其它值为右边按钮
* @arg parent 父窗口
*/
static msgBoxDlg* getInstance(ButtonType btn = Btn_All, IconType icon = Icon_Information, QByteArray title = "", QByteArray content = "", QByteArray leftBtnName = "确定", QByteArray centerBtnName = "取消", QByteArray rightBtnName = "退出", int highLight = 0, QWidget* parent = nullptr);
//设置标题
void setTitle(QByteArray title = "");
//设置内容
void setContent(QByteArray content = "");
//设置图标显示
void setIcon(IconType icon);
//设置按钮显示
void setButtonEnabled(ButtonType btn);
//确定按钮文本显示
void setLeftButtonText(QByteArray text = "");
//取消按钮文本显示
void setCenterButtonText(QByteArray text = "");
//退出按钮文本显示
void setRightButtonText(QByteArray text = "");
//设置默认高亮按钮
void setHighLightButton(int highLight);
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
private:
explicit msgBoxDlg(QWidget *parent = 0);
void initIcon(IconType icon);
void initButton(ButtonType btn);
private slots:
void on_CloseBtn_clicked();
void on_leftBtn_clicked();
void on_centerBtn_clicked();
void on_rightBtn_clicked();
private:
Ui::msgBoxDlg *ui;
QPoint m_point;
bool isMousePressed;
};
#endif // MSGBOXDLG_H
cpp文件
#include "msgboxdlg.h"
#include "ui_msgboxdlg.h"
msgBoxDlg::msgBoxDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::msgBoxDlg)
{
ui->setupUi(this);
//隐藏系统标题栏
setWindowFlags(Qt::FramelessWindowHint);
//背景设置为透明
// setAttribute(Qt::WA_TranslucentBackground, true);
//初始化变量
isMousePressed = false;
}
void msgBoxDlg::initIcon(IconType icon)
{
switch (icon) {
case Icon_Information:
ui->iconLabel->show();
ui->iconLabel->setStyleSheet("border-image: url(:/resource/icon_info_dark.png);");
break;
case Icon_Warning:
ui->iconLabel->show();
ui->iconLabel->setStyleSheet("border-image: url(:/resource/icon_warning_dark.png);");
break;
case Icon_Error:
ui->iconLabel->show();
ui->iconLabel->setStyleSheet("border-image: url(:/resource/icon_error_dark.png);");
break;
case Icon_Question:
ui->iconLabel->show();
ui->iconLabel->setStyleSheet("border-image: url(:/resource/icon_question_dark.png);");
break;
default:
ui->iconLabel->show();
ui->iconLabel->setStyleSheet("border-image: url(:/resource/logo.ico);");
// ui->iconLabel->hide();
break;
}
}
void msgBoxDlg::initButton(ButtonType btn)
{
switch (btn) {
case Btn_None:
ui->leftBtn->hide();
ui->centerBtn->hide();
ui->rightBtn->hide();
break;
case Btn_One:
ui->leftBtn->show();
ui->centerBtn->hide();
ui->rightBtn->hide();
break;
case Btn_Two:
ui->leftBtn->show();
ui->centerBtn->show();
ui->rightBtn->hide();
break;
default:
ui->leftBtn->show();
ui->centerBtn->show();
ui->rightBtn->show();;
break;
}
}
msgBoxDlg::~msgBoxDlg()
{
delete ui;
}
void msgBoxDlg::mousePressEvent(QMouseEvent *event)
{
isMousePressed = true;
m_point = event->globalPos() - frameGeometry().topLeft();
}
void msgBoxDlg::mouseMoveEvent(QMouseEvent *event)
{
if(isMousePressed)
move(event->globalPos() - m_point);
}
void msgBoxDlg::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event);
isMousePressed = false;
}
msgBoxDlg *msgBoxDlg::getInstance(ButtonType btn, IconType icon, QByteArray title, QByteArray content, QByteArray leftBtnName, QByteArray centerBtnName, QByteArray rightBtnName, int highLight, QWidget* parent)
{
static msgBoxDlg msgBox(parent);
msgBox.setTitle(title);
msgBox.setContent(content);
msgBox.setLeftButtonText(leftBtnName);
msgBox.setCenterButtonText(centerBtnName);
msgBox.setRightButtonText(rightBtnName);
msgBox.setHighLightButton(highLight);
msgBox.initButton(btn);
msgBox.initIcon(icon);
return &msgBox;
}
void msgBoxDlg::setTitle(QByteArray title) { ui->titleLabel->setText(title); }
void msgBoxDlg::setContent(QByteArray content)
{
ui->contentLabel->setText(content);
//ui->contentLabel->adjustSize();
}
void msgBoxDlg::setIcon(IconType icon) { initIcon(icon); }
void msgBoxDlg::setButtonEnabled(ButtonType btn) { initButton(btn); }
void msgBoxDlg::setLeftButtonText(QByteArray text) { ui->leftBtn->setText(text); }
void msgBoxDlg::setCenterButtonText(QByteArray text) { ui->centerBtn->setText(text); }
void msgBoxDlg::setRightButtonText(QByteArray text) { ui->rightBtn->setText(text); }
void msgBoxDlg::setHighLightButton(int highLight)
{
if(highLight == 0)
ui->leftBtn->setFocus();
else if(highLight == 1)
ui->centerBtn->setFocus();
else
ui->rightBtn->setFocus();
}
void msgBoxDlg::on_CloseBtn_clicked() { this->reject();}
void msgBoxDlg::on_leftBtn_clicked() { done(TypeLeft);}
void msgBoxDlg::on_centerBtn_clicked() { done(TypeCenter);}
void msgBoxDlg::on_rightBtn_clicked() { done(TypeRight); }
ui文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>msgBoxDlg</class>
<widget class="QDialog" name="msgBoxDlg">
<property name="windowModality">
<enum>Qt::NonModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>321</width>
<height>179</height>
</rect>
</property>
<property name="windowTitle">
<string>对话框</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="titleBar" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>31</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<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="minimumSize">
<size>
<width>25</width>
<height>25</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>25</width>
<height>25</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_6">
<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="QLabel" name="titleLabel">
<property name="minimumSize">
<size>
<width>200</width>
<height>25</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>25</height>
</size>
</property>
<property name="text">
<string>TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</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>
<item>
<widget class="QToolButton" name="CloseBtn">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>31</width>
<height>31</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../qss/qss.qrc">
<normaloff>:/resource/quit.png</normaloff>:/resource/quit.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,0,0">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Minimum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>28</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="iconLabel">
<property name="minimumSize">
<size>
<width>40</width>
<height>40</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>40</width>
<height>40</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="contentLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="lineWidth">
<number>1</number>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>28</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,0,0,0,0">
<property name="bottomMargin">
<number>31</number>
</property>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>48</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="leftBtn">
<property name="minimumSize">
<size>
<width>55</width>
<height>30</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>30</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>确定</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="centerBtn">
<property name="minimumSize">
<size>
<width>55</width>
<height>30</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>30</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>取消</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="rightBtn">
<property name="minimumSize">
<size>
<width>55</width>
<height>30</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>30</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>退出</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>38</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../qss/qss.qrc"/>
</resources>
<connections/>
</ui>
根据样式表进行美化效果
引用
int ret = msgBoxDlg::getInstance(Btn_All,Icon_Question,"保存","当前捕获数据尚未保存,是否进行保存?","保存","继续,不保存(&w)","取消",1)->exec();
switch (ret)
{
case TypeLeft:
{
//doSomething
}
break;
case TypeCenter:
{
//do nothing
}
break;
case TypeRight:
{
return ;
}
break;
default:
break;
}