在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例Qt 图形界面开发 → qt自定义弹出窗体(messagebox)

qt自定义弹出窗体(messagebox)

Qt 图形界面开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.07M
  • 下载次数:50
  • 浏览次数:4266
  • 发布时间:2019-06-11
  • 实例类别:Qt 图形界面开发
  • 发 布 人:chenxiaolan
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Qt MessageBox

实例介绍

【实例简介】qt中自己定义个对话框,改变了原来的样式,比QMessageBox好看多了。

【实例截图】


from clipboard


from clipboard


from clipboard

from clipboard


from clipboard

【核心代码】


/**
 *  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

实例下载地址

qt自定义弹出窗体(messagebox)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警