• 6226阅读
  • 5回复

自定义消息对话框 [复制链接]

上一主题 下一主题
离线nigoole
 

只看楼主 倒序阅读 楼主  发表于: 2015-11-27
— 本帖被 XChinux 执行加亮操作(2016-04-23) —
首先申明,纯属个人无聊作品,最近项目中需要对话提示框,自己简单模拟了一个。希望大家不要见笑。
先上个图,大伙瞅瞅;


图片、颜色什么的根据自己配置

代码:头文件
  1. #ifndef MSGBOXBASE_H
  2. #define MSGBOXBASE_H
  3. #include <QDialog>
  4. #include <QWidget>
  5. #include <QLabel>
  6. #include <QPushButton>
  7. #include <QVBoxLayout>
  8. #include <QHBoxLayout>
  9. class MsgBoxBase : public QDialog
  10. {
  11.     Q_OBJECT
  12. public:
  13.     explicit MsgBoxBase(QWidget*parent=0);
  14. signals:
  15.     void signalClose();
  16. public slots:
  17. private:
  18.     QWidget *widgetTitle;
  19.     QLabel *labelWinIcon;
  20.     QLabel *labelWinTitle;
  21.     QPushButton *btnClose;
  22.     QHBoxLayout *horLayoutTitle;
  23.     QWidget *widgetBody;
  24.     QLabel *labelIcon;
  25.     QLabel *labelContent;
  26.     //QVBoxLayout*verLayoutBody;
  27.     QHBoxLayout *horLayoutBody;
  28.     QWidget *widgetButton;
  29.     QPushButton *btnOk;
  30.     QPushButton *btnCancel;
  31.     QHBoxLayout *horLayoutBtn;
  32.     QVBoxLayout *verLayoutGlobal;
  33. protected:
  34.     QPoint dragPosition;
  35.     void mousePressEvent(QMouseEvent*);
  36.     void mouseMoveEvent(QMouseEvent*);
  37.     void paintEvent(QPaintEvent*);
  38. };
  39. #endif//MSGBOXBASE_H


msgbox.cpp

  1. #include"msgboxbase.h"
  2. #include <QFile>
  3. #include <QDebug>
  4. #include <QPixmap>
  5. #include <QMouseEvent>
  6. #include <QPainter>
  7. #include <QPaintEvent>
  8. #include <QStyleOption>
  9. #include <QStyle>
  10. MsgBoxBase::MsgBoxBase(QWidget*parent):
  11.     QDialog(parent)
  12. {
  13.     setFixedSize(340, 155);
  14.     this->setObjectName("msgBox");
  15.     this->setWindowFlags(Qt::FramelessWindowHint);
  16.     // 标题栏控件
  17.     widgetTitle = new QWidget(this);
  18.     widgetTitle->setFixedHeight(20);
  19.     labelWinIcon = new QLabel(this);
  20.     labelWinIcon->setAlignment(Qt::AlignCenter);
  21.     labelWinTitle = new QLabel(this);
  22.     btnClose = new QPushButton(this);
  23.     btnClose->setFocusPolicy(Qt::NoFocus);
  24.     btnClose->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
  25.     // 设置ObjectName,在样式表中需要通过这个名字进行访问该对象
  26.     widgetTitle->setObjectName("widgetTitle");
  27.     labelWinIcon->setObjectName("labelWinIcon");
  28.     labelWinTitle->setObjectName("labelWinTitle");
  29.     btnClose->setObjectName("btnClose");
  30. //    labelWinIcon->setPixmap(QPixmap(":/resource/title.png"));
  31.     labelWinIcon->setPixmap(style()->standardPixmap(QStyle::SP_DesktopIcon));
  32.     labelWinTitle->setText("提示");
  33.     // 标题栏布局管理
  34.     horLayoutTitle = new QHBoxLayout(widgetTitle);
  35.     horLayoutTitle->setContentsMargins(10,0,0,0);
  36.     horLayoutTitle->setSpacing(4);
  37.     horLayoutTitle->addWidget(labelWinIcon,0,Qt::AlignCenter);
  38.     horLayoutTitle->addWidget(labelWinTitle,4,Qt::AlignLeft);
  39.     horLayoutTitle->addWidget(btnClose,0,Qt::AlignCenter);
  40.     // 中间消息内容部分
  41.     widgetBody = new QWidget(this);
  42.     labelIcon = new QLabel(this);
  43.     labelContent = new QLabel(this);
  44.     labelContent->setWordWrap(true);
  45.     widgetBody->setObjectName("widgetBody");
  46.     labelIcon->setObjectName("labelIcon");
  47.     labelContent->setObjectName("labelContent");
  48. //    labelIcon->setPixmap(QPixmap(":/resource/info.png"));
  49.     labelIcon->setPixmap(style()->standardPixmap(QStyle::SP_MessageBoxInformation));
  50.     labelContent->setText("当前系统登陆错误!请检查网络连接后再试试!!!");
  51.     // 中间内容部分布局,一个图片和一个文件label
  52.     horLayoutBody = new QHBoxLayout(widgetBody);
  53.     horLayoutBody->setContentsMargins(10,10,10,10);
  54.     horLayoutBody->setSpacing(10);
  55.     horLayoutBody->addWidget(labelIcon, 1, Qt::AlignCenter);
  56.     horLayoutBody->addWidget(labelContent,5);
  57.     // 底部按钮
  58.     widgetButton = new QWidget(this);
  59.     btnCancel = new QPushButton(this);
  60.     btnCancel->setFocusPolicy(Qt::NoFocus);
  61.     // 加上图标不好看,个人觉得,自己尝试
  62. //    btnCancel->setIcon(style()->standardIcon(QStyle::SP_DialogCancelButton));
  63.     btnCancel->setText("取消");
  64.     btnOk = new QPushButton(this);
  65.     btnOk->setText("确定");
  66.     btnOk->setFocusPolicy(Qt::NoFocus);
  67. //    btnOk->setIcon(style()->standardIcon(QStyle::SP_DialogApplyButton));
  68.     // 设置ObjectName,在样式表中需要通过这个名字进行访问该对象
  69.     widgetButton->setObjectName("widgetButton");
  70.     btnCancel->setObjectName("btnCancel");
  71.     btnOk->setObjectName("btnOk");
  72.     // 下面2个按钮布局关联
  73.     horLayoutBtn = new QHBoxLayout(widgetButton);
  74.     horLayoutBtn->setContentsMargins(10,5,10,5);
  75.     horLayoutBtn->setSpacing(10);
  76.     horLayoutBtn->addStretch();
  77.     horLayoutBtn->addWidget(btnCancel,0,Qt::AlignCenter);
  78.     horLayoutBtn->addWidget(btnOk,0,Qt::AlignCenter);
  79.     // 全局布局文件
  80.     verLayoutGlobal = new QVBoxLayout(this);
  81.     verLayoutGlobal->setContentsMargins(2,1,2,1);
  82.     verLayoutGlobal->setSpacing(0);
  83.     verLayoutGlobal->addWidget(widgetTitle);
  84.     verLayoutGlobal->addWidget(widgetBody,3);
  85.     verLayoutGlobal->addWidget(widgetButton);
  86.     // 关联信号槽
  87.     connect(btnClose,SIGNAL(clicked()),this,SLOT(close()));
  88.     //connect(btnClose,SIGNAL(clicked()),this,SIGNAL(signalClose()));
  89.     connect(btnCancel,SIGNAL(clicked()),this,SLOT(reject()));
  90.     connect(btnOk,SIGNAL(clicked()),this,SLOT(accept()));
  91.     // 加载样式表文件
  92.     QFile file(":/resource/msgBox.css");
  93.     file.open(QIODevice::ReadOnly);
  94.     this->setStyleSheet(file.readAll());
  95.     file.close();
  96. }
  97. // 移动界面
  98. void MsgBoxBase::mousePressEvent(QMouseEvent*event){
  99.     if(event->buttons() == Qt::LeftButton){
  100.         dragPosition = event->globalPos() - frameGeometry().topLeft();
  101.         event->accept();
  102.     }
  103. }
  104. // 移动
  105. void MsgBoxBase::mouseMoveEvent(QMouseEvent*event){
  106.     if(event->buttons() == Qt::LeftButton){
  107.         this->move(event->globalPos() - dragPosition);
  108.         event->accept();
  109.     }
  110. }
  111. // 添加此属性后父类widget才可以使用样式表
  112. void MsgBoxBase::paintEvent(QPaintEvent*)
  113. {
  114.     QStyleOption opt;
  115.     opt.init(this);
  116.     QPainter p(this);
  117.     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  118. }



// 样式表文件
  1. QWidget#msgBox{
  2.     border-radius:3px;
  3.     border:1px solid lightgray;
  4. }
  5. QWidget#widgetTitle{
  6.     background: rgb(164, 185, 84);
  7.     font: 11px;
  8.     font-family:"微软雅黑";
  9.     height: 20px;
  10. }
  11. QWidget#widgetButton{
  12.     background: rgb(238, 224, 229);
  13.     font: 10px;
  14.     font-family:" 微软雅黑";
  15.     height: 32px;
  16. }
  17. QPushButton#btnClose{
  18.     /*border-image: url(:/images/btn_close_nor.ico);*/
  19.     border: none;
  20.     width: 20px;
  21.     height: 20px;
  22. }
  23. QPushButton#btnClose:hover{
  24.     background-color:red;
  25. }
  26. QPushButton#btnOk, QPushButton#btnCancel{
  27.     font-family: "微软雅黑";
  28.     border:1px solid rgb(79, 173, 216);
  29.     border-radius: 2px;
  30.     width: 60px;
  31.     height: 22px;
  32. }
  33. QPushButton#btnOk:hover, QPushButton#btnCancel:hover{
  34.     background-color: rgb(190, 231, 253);
  35. }
  36. QLabel#labelContent{
  37.     font: 18px;
  38.     font-family: "MYingHei_C-Medium";
  39. }

有句话说得好:好好学习,天天向上。加油~~!有上船的朋友联系企鹅393320854
离线nigoole

只看该作者 1楼 发表于: 2015-11-27
有的人到处找图片,其实在QStyle里面提供了很多资源图片,可以自己一一去挖掘。随便在加点样式表什么的就可以吧界面做好!
有句话说得好:好好学习,天天向上。加油~~!有上船的朋友联系企鹅393320854
离线xiaoniede

只看该作者 2楼 发表于: 2015-11-28
思路清晰,收藏啦~
离线z609932088

只看该作者 3楼 发表于: 2015-11-28
很好,好好学习,天天向上
有阳光的地方就是青春
离线hezf

只看该作者 4楼 发表于: 2015-12-01
QStyle很好哇
以后都不需要可以去找很多图片了
none
离线bluesky0318

只看该作者 5楼 发表于: 2015-12-11
QStyle很好哇
以后都不需要可以去找很多图片了
快速回复
限100 字节
 
上一个 下一个