/**
* Author: lwei
* Since: 2016.8.26
* Version: 1.0.0
*/
#include "messagebox.h"
#include "iconhelper.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QGridLayout>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QMouseEvent>
MessageBox::MessageBox(QWidget *parent,
Icon icon,
const QString &title,
const QString &text,
QMessageBox::StandardButtons standButtons,
QMessageBox::StandardButton defaultButton)
:QDialog(parent)
,m_lIconTitle(new QLabel(this))
,m_lTitle(new QLabel(this))
,m_lIconMain(new QLabel(this))
,m_lText(new QLabel(this))
,m_layout(new QVBoxLayout())
,m_pButtonBox(new QDialogButtonBox(this))
,m_mouserPressed(false)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
setFixedSize(QSize(606, 286));
m_layout->setContentsMargins(20, 20, 20, 20);
QHBoxLayout *titleLayout = new QHBoxLayout();
titleLayout->setContentsMargins(8, 0, 0, 0);
titleLayout->setSpacing(20);
titleLayout->addWidget(m_lIconTitle);
titleLayout->addWidget(m_lTitle);
titleLayout->addStretch(1);
m_lIconTitle->setFixedHeight(20);
m_lTitle->setFixedHeight(20);
m_layout->addLayout(titleLayout);
QGroupBox *groupBox = new QGroupBox(this);
groupBox->setFixedHeight(200);
m_layout->addWidget(groupBox);
QVBoxLayout *vBoxLayout = new QVBoxLayout();
groupBox->setLayout(vBoxLayout);
vBoxLayout->setContentsMargins(20, 20, 20, 20);
vBoxLayout->setSpacing(20);
QHBoxLayout *hLayout = new QHBoxLayout();
m_lIconMain->setFixedSize(QSize(90, 90));
m_lText->setFixedHeight(90);
hLayout->addWidget(m_lIconMain);
hLayout->addWidget(m_lText);
hLayout->addStretch(1);
hLayout->setSpacing(10);
vBoxLayout->addLayout(hLayout);
QHBoxLayout *hLayoutButtons = new QHBoxLayout();
hLayoutButtons->addStretch(1);
hLayoutButtons->addWidget(m_pButtonBox);
vBoxLayout->addLayout(hLayoutButtons);
m_pButtonBox->setFixedHeight(55);
m_layout->addWidget(groupBox);
setLayout(m_layout);
m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons((int)standButtons));
setDefaultButton(defaultButton);
QList<QAbstractButton *> buttons = m_pButtonBox->buttons();
for(int i = 0; i < buttons.size(); i++) {
QDialogButtonBox::StandardButton button = m_pButtonBox->standardButton(buttons.at(i));
QPushButton *pushButton = m_pButtonBox->button(button);
pushButton->setFixedSize(QSize(135, 55));
if(button == QDialogButtonBox::Ok || button == QDialogButtonBox::Yes) {
pushButton->setText("确定");
} else {
pushButton->setText("取消");
}
pushButton->setStyleSheet("QPushButton{border:3px solid rgb(176,181,185);"
"border-radius:5px;"
"font-family:微软雅黑;font-size:18px;}");
}
IconHelper::Instance()->SetIcon(m_lIconTitle, QChar(0xf05a), 20);
m_lTitle->setText(title);
m_lTitle->setStyleSheet("font-family:微软雅黑;font-size:20px;");
m_lText->setText(text);
m_lText->setStyleSheet("font-family:微软雅黑;font-size:18px;");
if(icon == Messsage) {
m_lIconMain->setStyleSheet("border-image:url(:/message)");
} else if(icon == Question) {
m_lIconMain->setStyleSheet("border-image:url(:/question)");
} else if(icon == Warning) {
m_lIconMain->setStyleSheet("border-image:url(:/warning)");
} else if(icon == Error) {
m_lIconMain->setStyleSheet("border-image:url(:/error)");
}
this->setStyleSheet("QDialog{border:1px solid black;background-color:rgb(212,217,221);}");
connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this,
SLOT(onButtonClicked(QAbstractButton*)));
}
void MessageBox::setDefaultButton(QPushButton *button)
{
if(!m_pButtonBox->buttons().contains(button)) {
return ;
}
button->setDefault(true);
button->setFocus();
}
void MessageBox::setDefaultButton(QMessageBox::StandardButton defaultButton)
{
setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(defaultButton)));
}
void MessageBox::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
m_mouserPressed = true;
m_point = event->globalPos() - this->pos();
event->accept();
}
}
void MessageBox::mouseMoveEvent(QMouseEvent *event)
{
if(m_mouserPressed && (event->buttons() & Qt::LeftButton)) {
this->move(event->globalPos() - m_point);
event->accept();
}
}
void MessageBox::mouseReleaseEvent(QMouseEvent * /*event*/)
{
m_mouserPressed = false;
}
int MessageBox::execReturnCode(QAbstractButton *button)
{
return m_pButtonBox->standardButton(button);
}
void MessageBox::onButtonClicked(QAbstractButton *button)
{
done(execReturnCode(button));
}
没有合适的资源?快使用搜索试试~ 我知道了~
qt自定义的messagebox
共16个文件
cpp:4个
png:4个
h:3个
5星 · 超过95%的资源 需积分: 50 190 下载量 182 浏览量
2016-08-27
17:35:10
上传
评论 2
收藏 67KB ZIP 举报
温馨提示
qt中自己定义个对话框,改变了原来的样式,比QMessageBox好看多了。
资源推荐
资源详情
资源评论
收起资源包目录
untitled20.zip (16个子文件)
untitled20
iconhelper.cpp 612B
untitled20.pro.user 23KB
iconhelper.h 784B
resource.qrc 361B
messagebox.cpp 5KB
messagebox.h 1KB
resource
question.png 4KB
info.png 3KB
close.png 6KB
fontawesome-webfont.ttf 77KB
error.png 2KB
main.cpp 253B
mainwindow.ui 2KB
mainwindow.cpp 2KB
mainwindow.h 510B
untitled20.pro 503B
共 16 条
- 1
资源评论
- checkylqy2017-03-23值得借鉴,感谢分享
菜园小火车
- 粉丝: 18
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功