nigoole |
2015-11-27 16:32 |
自定义消息对话框
首先申明,纯属个人无聊作品,最近项目中需要对话提示框,自己简单模拟了一个。希望大家不要见笑。 先上个图,大伙瞅瞅; [attachment=14560][attachment=14561]
图片、颜色什么的根据自己配置
代码:头文件
- #ifndef MSGBOXBASE_H
#define MSGBOXBASE_H
#include <QDialog> #include <QWidget> #include <QLabel> #include <QPushButton> #include <QVBoxLayout> #include <QHBoxLayout>
class MsgBoxBase : public QDialog { Q_OBJECT public: explicit MsgBoxBase(QWidget*parent=0);
signals: void signalClose(); public slots:
private: QWidget *widgetTitle; QLabel *labelWinIcon; QLabel *labelWinTitle; QPushButton *btnClose; QHBoxLayout *horLayoutTitle;
QWidget *widgetBody; QLabel *labelIcon; QLabel *labelContent; //QVBoxLayout*verLayoutBody; QHBoxLayout *horLayoutBody;
QWidget *widgetButton; QPushButton *btnOk; QPushButton *btnCancel; QHBoxLayout *horLayoutBtn;
QVBoxLayout *verLayoutGlobal;
protected: QPoint dragPosition; void mousePressEvent(QMouseEvent*); void mouseMoveEvent(QMouseEvent*); void paintEvent(QPaintEvent*); };
#endif//MSGBOXBASE_H
msgbox.cpp
- #include"msgboxbase.h"
#include <QFile> #include <QDebug> #include <QPixmap> #include <QMouseEvent> #include <QPainter> #include <QPaintEvent> #include <QStyleOption> #include <QStyle>
MsgBoxBase::MsgBoxBase(QWidget*parent): QDialog(parent) { setFixedSize(340, 155); this->setObjectName("msgBox"); this->setWindowFlags(Qt::FramelessWindowHint);
// 标题栏控件 widgetTitle = new QWidget(this); widgetTitle->setFixedHeight(20);
labelWinIcon = new QLabel(this); labelWinIcon->setAlignment(Qt::AlignCenter); labelWinTitle = new QLabel(this);
btnClose = new QPushButton(this); btnClose->setFocusPolicy(Qt::NoFocus); btnClose->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
// 设置ObjectName,在样式表中需要通过这个名字进行访问该对象 widgetTitle->setObjectName("widgetTitle"); labelWinIcon->setObjectName("labelWinIcon"); labelWinTitle->setObjectName("labelWinTitle"); btnClose->setObjectName("btnClose");
// labelWinIcon->setPixmap(QPixmap(":/resource/title.png")); labelWinIcon->setPixmap(style()->standardPixmap(QStyle::SP_DesktopIcon)); labelWinTitle->setText("提示");
// 标题栏布局管理 horLayoutTitle = new QHBoxLayout(widgetTitle); horLayoutTitle->setContentsMargins(10,0,0,0); horLayoutTitle->setSpacing(4); horLayoutTitle->addWidget(labelWinIcon,0,Qt::AlignCenter); horLayoutTitle->addWidget(labelWinTitle,4,Qt::AlignLeft); horLayoutTitle->addWidget(btnClose,0,Qt::AlignCenter);
// 中间消息内容部分 widgetBody = new QWidget(this); labelIcon = new QLabel(this); labelContent = new QLabel(this); labelContent->setWordWrap(true);
widgetBody->setObjectName("widgetBody"); labelIcon->setObjectName("labelIcon"); labelContent->setObjectName("labelContent");
// labelIcon->setPixmap(QPixmap(":/resource/info.png")); labelIcon->setPixmap(style()->standardPixmap(QStyle::SP_MessageBoxInformation)); labelContent->setText("当前系统登陆错误!请检查网络连接后再试试!!!");
// 中间内容部分布局,一个图片和一个文件label horLayoutBody = new QHBoxLayout(widgetBody); horLayoutBody->setContentsMargins(10,10,10,10); horLayoutBody->setSpacing(10); horLayoutBody->addWidget(labelIcon, 1, Qt::AlignCenter); horLayoutBody->addWidget(labelContent,5);
// 底部按钮 widgetButton = new QWidget(this);
btnCancel = new QPushButton(this); btnCancel->setFocusPolicy(Qt::NoFocus); // 加上图标不好看,个人觉得,自己尝试 // btnCancel->setIcon(style()->standardIcon(QStyle::SP_DialogCancelButton)); btnCancel->setText("取消");
btnOk = new QPushButton(this); btnOk->setText("确定"); btnOk->setFocusPolicy(Qt::NoFocus); // btnOk->setIcon(style()->standardIcon(QStyle::SP_DialogApplyButton));
// 设置ObjectName,在样式表中需要通过这个名字进行访问该对象 widgetButton->setObjectName("widgetButton"); btnCancel->setObjectName("btnCancel"); btnOk->setObjectName("btnOk");
// 下面2个按钮布局关联 horLayoutBtn = new QHBoxLayout(widgetButton); horLayoutBtn->setContentsMargins(10,5,10,5); horLayoutBtn->setSpacing(10); horLayoutBtn->addStretch(); horLayoutBtn->addWidget(btnCancel,0,Qt::AlignCenter); horLayoutBtn->addWidget(btnOk,0,Qt::AlignCenter);
// 全局布局文件 verLayoutGlobal = new QVBoxLayout(this); verLayoutGlobal->setContentsMargins(2,1,2,1); verLayoutGlobal->setSpacing(0); verLayoutGlobal->addWidget(widgetTitle); verLayoutGlobal->addWidget(widgetBody,3); verLayoutGlobal->addWidget(widgetButton);
// 关联信号槽 connect(btnClose,SIGNAL(clicked()),this,SLOT(close())); //connect(btnClose,SIGNAL(clicked()),this,SIGNAL(signalClose())); connect(btnCancel,SIGNAL(clicked()),this,SLOT(reject())); connect(btnOk,SIGNAL(clicked()),this,SLOT(accept()));
// 加载样式表文件 QFile file(":/resource/msgBox.css"); file.open(QIODevice::ReadOnly); this->setStyleSheet(file.readAll()); file.close(); }
// 移动界面 void MsgBoxBase::mousePressEvent(QMouseEvent*event){ if(event->buttons() == Qt::LeftButton){ dragPosition = event->globalPos() - frameGeometry().topLeft(); event->accept(); } }
// 移动 void MsgBoxBase::mouseMoveEvent(QMouseEvent*event){ if(event->buttons() == Qt::LeftButton){ this->move(event->globalPos() - dragPosition); event->accept(); } }
// 添加此属性后父类widget才可以使用样式表 void MsgBoxBase::paintEvent(QPaintEvent*) { QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); }
// 样式表文件
- QWidget#msgBox{
border-radius:3px; border:1px solid lightgray; }
QWidget#widgetTitle{ background: rgb(164, 185, 84); font: 11px; font-family:"微软雅黑"; height: 20px; }
QWidget#widgetButton{ background: rgb(238, 224, 229); font: 10px; font-family:" 微软雅黑"; height: 32px; }
QPushButton#btnClose{ /*border-image: url(:/images/btn_close_nor.ico);*/ border: none; width: 20px; height: 20px; }
QPushButton#btnClose:hover{ background-color:red; }
QPushButton#btnOk, QPushButton#btnCancel{ font-family: "微软雅黑"; border:1px solid rgb(79, 173, 216); border-radius: 2px; width: 60px; height: 22px; }
QPushButton#btnOk:hover, QPushButton#btnCancel:hover{ background-color: rgb(190, 231, 253); }
QLabel#labelContent{ font: 18px; font-family: "MYingHei_C-Medium"; }
|
|