当前位置:小鱼儿玄机二站 > 编程应用 > 漂亮的提示信息效果代码,自定义模态对话框

漂亮的提示信息效果代码,自定义模态对话框

文章作者:编程应用 上传时间:2019-09-23

新建一个MsgBox类

图片 1

msgbox.h 代码

图片 2

#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_OBJECTpublic:    MsgBox(QWidget *parent = 0);    ~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

CSS样式:

msgbox.cpp 代码

复制代码 代码如下:

#include "msgbox.h"MsgBox::MsgBox(QWidget *parent)    : QDialog{      this->resize(320, 160);      //获取主界面的宽度      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-30,5, 20, 20);      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;      title_label->setGeometry(0, 0, width-50, 30);      //设置提示图片      msg_label = new QLabel(this);      msg_label->setGeometry(20, 50, 36, 36);      msg_label->setScaledContents(true);      //设置提示信息,让QLabel能够自动判断并换行显示:      ask_label = new QLabel(this);      ask_label->setGeometry(65, 60, width-50, 25*2);      ask_label->setWordWrap(true);      ask_label->setAlignment(Qt::AlignTop);      cancel_button = new QPushButton(this);      cancel_button->resize(70, 25);      cancel_button->move(width - cancel_button->width() - 10, height - 35);      ok_button = new QPushButton(this);      ok_button->resize(70, 25);      ok_button->move(width - ok_button->width() - cancel_button->width() - 20, height - 35);      ok_button->setObjectName(QString::fromUtf8("pushButtonTwo"));      cancel_button->setObjectName(QString::fromUtf8("pushButtonTwo"));      QObject::connect(ok_button, SIGNAL), this, SLOT(okOperate;      QObject::connect(close_button, SIGNAL), this, SLOT(closeOperate;      QObject::connect(cancel_button, SIGNAL), 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;  msg_label->setPixmap;  //是否隐藏确定按钮  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, QPixmap(":/image/images/close.png"));  QBitmap bitmap(this->size;  QPainter painter2(&bitmap);  painter2.fillRect(bitmap.rect(), Qt::white);  painter2.setBrush(QColor(0, 0, 0));  painter2.drawRoundedRect, 4, 4);  setMask;}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();}

.zeng_msgbox_layer,
.zeng_msgbox_layer .gtl_ico_succ,
.zeng_msgbox_layer .gtl_ico_fail,
.zeng_msgbox_layer .gtl_ico_hits,
.zeng_msgbox_layer .gtl_ico_clear,
.zeng_msgbox_layer .gtl_end{display:inline-block;height:54px;line-height:54px;font-weight:bold;font-size:14px;color:#606060;background-image:url("gb_tip_layer.png");_background-image:url("gb_tip_layer_ie6.png");background-repeat:no-repeat;}
.zeng_msgbox_layer_wrap{width:100%;position:fixed;_position:absolute;top:46%;left:0;text-align:center;z-index:65533;}
.zeng_msgbox_layer{background-position:0 -161px;background-repeat:repeat-x;padding:0 18px 0 9px;margin:0 auto;position:relative;}
.zeng_msgbox_layer .gtl_ico_succ{background-position:-6px 0;left:-45px;top:0;width:45px;position:absolute;}
.zeng_msgbox_layer .gtl_end{background-position:0 0;position:absolute;right:-6px;top:0;width:6px;}
.zeng_msgbox_layer .gtl_ico_fail{background-position:-6px -108px;position:absolute;left:-45px;top:0;width:45px;}
.zeng_msgbox_layer .gtl_ico_hits{background-position:-6px -54px;position:absolute;left:-45px;top:0;width:45px;}
.zeng_msgbox_layer .gtl_ico_clear{background-position:-6px 0;left:-5px;width:5px;position:absolute;top:0;}
.zeng_msgbox_layer img{float:left;margin:19px 10px 0 5px ;}

主函数调用代码:

JAVASCRIPT:

#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();}

复制代码 代码如下:

目录构建图:

window.ZENG=window.ZENG || {};
ZENG.dom = {getById: function(id) {
return document.getElementById(id);
},get: function(e) {
return (typeof (e) == "string") ? document.getElementById(e) : e;
},createElementIn: function(tagName, elem, insertFirst, attrs) {
var _e = (elem = ZENG.dom.get(elem) || document.body).ownerDocument.createElement(tagName || "div"), k;
if (typeof (attrs) == 'object') {
for (k in attrs) {
if (k == "class") {
_e.className = attrs[k];
} else if (k == "style") {
_e.style.cssText = attrs[k];
} else {
_e[k] = attrs[k];
}
}
}
insertFirst ? elem.insertBefore(_e, elem.firstChild) : elem.appendChild(_e);
return _e;
},getStyle: function(el, property) {
el = ZENG.dom.get(el);
if (!el || el.nodeType == 9) {
return null;
}
var w3cMode = document.defaultView && document.defaultView.getComputedStyle, computed = !w3cMode ? null : document.defaultView.getComputedStyle(el, ''), value = "";
switch (property) {
case "float":
property = w3cMode ? "cssFloat" : "styleFloat";
break;
case "opacity":
if (!w3cMode) {
var val = 100;
try {
val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
} catch (e) {
try {
val = el.filters('alpha').opacity;
} catch (e) {
}
}
return val / 100;
} else {
return parseFloat((computed || el.style)[property]);
}
break;
case "backgroundPositionX":
if (w3cMode) {
property = "backgroundPosition";
return ((computed || el.style)[property]).split(" ")[0];
}
break;
case "backgroundPositionY":
if (w3cMode) {
property = "backgroundPosition";
return ((computed || el.style)[property]).split(" ")[1];
}
break;
}
if (w3cMode) {
return (computed || el.style)[property];
} else {
return (el.currentStyle[property] || el.style[property]);
}
},setStyle: function(el, properties, value) {
if (!(el = ZENG.dom.get(el)) || el.nodeType != 1) {
return false;
}
var tmp, bRtn = true, w3cMode = (tmp = document.defaultView) && tmp.getComputedStyle, rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i;
if (typeof (properties) == 'string') {
tmp = properties;
properties = {};
properties[tmp] = value;
}
for (var prop in properties) {
value = properties[prop];
if (prop == 'float') {
prop = w3cMode ? "cssFloat" : "styleFloat";
} else if (prop == 'opacity') {
if (!w3cMode) {
prop = 'filter';
value = value >= 1 ? '' : ('alpha(opacity=' + Math.round(value * 100) + ')');
}
} else if (prop == 'backgroundPositionX' || prop == 'backgroundPositionY') {
tmp = prop.slice(-1) == 'X' ? 'Y' : 'X';
if (w3cMode) {
var v = ZENG.dom.getStyle(el, "backgroundPosition" + tmp);
prop = 'backgroundPosition';
typeof (value) == 'number' && (value = value + 'px');
value = tmp == 'Y' ? (value + " " + (v || "top")) : ((v || 'left') + " "

图片 3

  • value);
    }
    }
    if (typeof el.style[prop] != "undefined") {
    el.style[prop] = value + (typeof value === "number" && !rexclude.test(prop) ? 'px' : '');
    bRtn = bRtn && true;
    } else {
    bRtn = bRtn && false;
    }
    }
    return bRtn;
    },getScrollTop: function(doc) {
    var _doc = doc || document;
    return Math.max(_doc.documentElement.scrollTop, _doc.body.scrollTop);
    },getClientHeight: function(doc) {
    var _doc = doc || document;
    return _doc.compatMode == "CSS1Compat" ? _doc.documentElement.clientHeight : _doc.body.clientHeight;
    }
    };
    ZENG.string = {RegExps: {trim: /^s+|s+$/g,ltrim: /^s+/,rtrim: /s+$/,nl2br: /n/g,s2nb: /[x20]{2}/g,URIencode: /[x09x0Ax0Dx20x21-x29x2Bx2Cx2Fx3A-x3Fx5B-x5Ex60x7B-x7E]/g,escHTML: {re_amp: /&/g,re_lt: /</g,re_gt: />/g,re_apos: /x27/g,re_quot: /x22/g},escString: {bsls: /\/g,sls: ///g,nl: /n/g,rt: /r/g,tab: /t/g},restXHTML: {re_amp: /&/g,re_lt: /</g,re_gt: />/g,re_apos: /&(?:apos|#0?39);/g,re_quot: /"/g},write: /{(d{1,2})(?::([xodQqb]))?}/g,isURL: /^(?:ht|f)tp(?:s)?://(?:[w-.]+).w+/i,cut: /[x00-xFF]/,getRealLen: {r0: /[^x00-xFF]/g,r1: /[x00-xFF]/g},format: /{([dw.]+)}/g},commonReplace: function(s, p, r) {
    return s.replace(p, r);
    },format: function(str) {
    var args = Array.prototype.slice.call(arguments), v;
    str = String(args.shift());
    if (args.length == 1 && typeof (args[0]) == 'object') {
    args = args[0];
    }
    ZENG.string.RegExps.format.lastIndex = 0;
    return str.replace(ZENG.string.RegExps.format, function(m, n) {
    v = ZENG.object.route(args, n);
    return v === undefined ? m : v;
    });
    }};
    ZENG.object = {
    routeRE: /([dw_]+)/g,
    route: function(obj, path) {
    obj = obj || {};
    path = String(path);
    var r = ZENG.object.routeRE, m;
    r.lastIndex = 0;
    while ((m = r.exec(path)) !== null) {
    obj = obj[m[0]];
    if (obj === undefined || obj === null) {
    break;
    }
    }
    return obj;
    }};
    var ua = ZENG.userAgent = {}, agent = navigator.userAgent;
    ua.ie = 9 - ((agent.indexOf('Trident/5.0') > -1) ? 0 : 1) - (window.XDomainRequest ? 0 : 1) - (window.XMLHttpRequest ? 0 : 1);
    if (typeof (ZENG.msgbox) == 'undefined') {
    ZENG.msgbox = {};
    }
    ZENG.msgbox._timer = null;
    ZENG.msgbox.loadingAnimationPath = ZENG.msgbox.loadingAnimationPath || ("loading.gif");
    ZENG.msgbox.show = function(msgHtml, type, timeout, opts) {
    if (typeof (opts) == 'number') {
    opts = {topPosition: opts};
    }
    opts = opts || {};
    var _s = ZENG.msgbox,
    template = '<span class="zeng_msgbox_layer" style="display:none;z-index:10000;" id="mode_tips_v2"><span class="gtl_ico_{type}"></span>{loadIcon}{msgHtml}<span class="gtl_end"></span></span>', loading = '<img src="' + (opts.customIcon || _s.loadingAnimationPath) + '" alt="" />', typeClass = [0, 0, 0, 0, "succ", "fail", "clear"], mBox, tips;
    _s._loadCss && _s._loadCss(opts.cssPath);
    mBox = ZENG.dom.get("q_Msgbox") || ZENG.dom.createElementIn("div", document.body, false, {className: "zeng_msgbox_layer_wrap"});
    mBox.id = "q_Msgbox";
    mBox.style.display = "";
    mBox.innerHTML = ZENG.string.format(template, {type: typeClass[type] || "hits",msgHtml: msgHtml || "",loadIcon: type == 6 ? loading : ""});
    _s._setPosition(mBox, timeout, opts.topPosition);
    };
    ZENG.msgbox._setPosition = function(tips, timeout, topPosition) {
    timeout = timeout || 5000;
    var _s = ZENG.msgbox, bt = ZENG.dom.getScrollTop(), ch = ZENG.dom.getClientHeight(), t = Math.floor(ch / 2) - 40;
    ZENG.dom.setStyle(tips, "top", ((document.compatMode == "BackCompat" || ZENG.userAgent.ie < 7) ? bt : 0) + ((typeof (topPosition) == "number") ? topPosition : t) + "px");
    clearTimeout(_s._timer);
    tips.firstChild.style.display = "";
    timeout && (_s._timer = setTimeout(_s.hide, timeout));
    };
    ZENG.msgbox.hide = function(timeout) {
    var _s = ZENG.msgbox;
    if (timeout) {
    clearTimeout(_s._timer);
    _s._timer = setTimeout(_s._hide, timeout);
    } else {
    _s._hide();
    }
    };
    ZENG.msgbox._hide = function() {
    var _mBox = ZENG.dom.get("q_Msgbox"), _s = ZENG.msgbox;
    clearTimeout(_s._timer);
    if (_mBox) {
    var _tips = _mBox.firstChild;
    ZENG.dom.setStyle(_mBox, "display", "none");
    }
    };

贯彻效果与利益图:

调用:

图片 4

复制代码 代码如下:

ZENG.msgbox.show("设置成功!", 4, 三千);
ZENG.msgbox.show("服务器繁忙,请稍后再试。", 1, 三千);
ZENG.msgbox.show("数据拉取失利", 5, 三千);
ZENG.msgbox.show(" 正在加载中,请稍后...", 6,八千);

示范和下载体验: TencentUED-美丽的提醒音讯

复制代码 代码如下: .zeng_msgbox_layer, .zeng_msgbox_layer .gtl_ico_succ, .zeng_msgbox_layer .gtl_ico_fail, .zeng_msgbox_layer .gtl_ico_hits, .zeng_msgbox_layer...

本文由小鱼儿玄机二站发布于编程应用,转载请注明出处:漂亮的提示信息效果代码,自定义模态对话框

关键词: