概述
1.CustomMsgBox.h
#ifndef CUSTOMMSGBOX_H
#define CUSTOMMSGBOX_H
#include <QDialog>
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMouseEvent>
class CustomMsgBox : public QDialog
{
Q_OBJECT
public:
explicit CustomMsgBox(QWidget *parent = 0,
const QString title = QString::fromLocal8Bit("提示"),
const QString text = "",
QMessageBox::StandardButtons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::Ok);
~CustomMsgBox();
void setDefalutButton(QPushButton *button);
void setDefaultButton(QMessageBox::StandardButton defaultButton );
private:
QString m_title;
QString m_content;
QWidget *m_topWgt;
QLabel *m_lblTitle;
QLabel *m_btnIcon;
QPushButton *m_btnClose;
QWidget *m_bottomWgt;
QDialogButtonBox *m_pButtonBox;
QLabel *m_lblContent;
QVBoxLayout *m_bottomBoxVLayout;
QMessageBox::StandardButtons m_standButtons;
bool m_mouserPressed;
QPoint m_point;
private:
void initWgt();
void initTopWgt();
void initBottomWgt();
int execReturnCode(QAbstractButton *button);
bool eventFilter(QObject *, QEvent *);
private slots:
void onButtonClicked(QAbstractButton *button);
void sltOnBtnClose();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};
#endif // CUSTOMMSGBOX_H
2.类CustomMsgBox.cpp
#include "custommsgbox.h"
#define strBtnDarkSty QString::fromLocal8Bit("QPushButton {border:1px solid rgb(97, 172, 240);"
"background-color:rgb(44, 112, 177);font-size:%1px;font-family:Microsoft YaHei UI;color:white;"
"}"
"QPushButton:pressed {"
"background-color: rgb(213, 239, 252);color:black;"
"}"
).arg(int(17))
CustomMsgBox::CustomMsgBox(QWidget *parent,QString title,QString content,
QMessageBox::StandardButtons standButtons,
QMessageBox::StandardButton defaultButton)
: QDialog(parent),m_standButtons(standButtons),m_content(content),m_title(title)
,m_pButtonBox(new QDialogButtonBox(this))
{
initWgt();
}
void CustomMsgBox::initWgt(){
setWindowFlags(Qt::FramelessWindowHint|Qt::Dialog);
setWindowTitle(m_title);
setContentsMargins(1,1,1,1);
QVBoxLayout *m_mainLayout = new QVBoxLayout(this);
m_topWgt = new QWidget(this);
m_topWgt->setContentsMargins(8, 0, 16, 0);
m_topWgt->setFixedSize(430,33);
initTopWgt();
m_bottomWgt = new QWidget(this);
m_bottomWgt->setFixedSize(430,165);
m_bottomWgt->setContentsMargins(8 , 8 , 8 , 8 );
initBottomWgt();
m_mainLayout->setSpacing(0);
m_mainLayout->setContentsMargins(0,0,0,0);
setStyleSheet("border:1px solid rgb(44,112,177);");
m_topWgt->setStyleSheet("QWidget{ margin: 0px;background-color:white;border:0px;}"
"QPushButton{border:0px;margin:0px;}"
"QLabel{border:0px;margin:0px;}");//border:1px solid #92C8E8;
m_bottomWgt->setStyleSheet("QWidget{background:rgb(213, 239, 252);border:0px;}");
m_mainLayout->setSpacing(0);
m_mainLayout->addWidget(m_topWgt);
m_mainLayout->addWidget(m_bottomWgt);
show();
}
void CustomMsgBox::initTopWgt()
{
QHBoxLayout *topHLayout = new QHBoxLayout(m_topWgt);
QWidget * leftTmp = new QWidget(m_topWgt);
leftTmp->setContentsMargins(0, 0, 0, 0);
QHBoxLayout *leftTmpLay = new QHBoxLayout(leftTmp);
leftTmpLay->setContentsMargins(0, 0, 0, 0);
topHLayout->setContentsMargins(0, 0, 0, 0);
m_btnIcon = new QLabel(leftTmp);
m_btnIcon->setPixmap(QPixmap("...configure/icons/tishitubiao.png"));
m_btnIcon->installEventFilter((QObject*) this);
m_lblTitle = new QLabel(leftTmp);
QFont ft = QFont("Microsoft YaHei UI");
ft.setPixelSize(18);
m_lblTitle->setFont(ft);
m_lblTitle->setText(m_title);
leftTmpLay->setSpacing(7);
leftTmpLay->addWidget(m_btnIcon,0,Qt::AlignHCenter);
leftTmpLay->addWidget(m_lblTitle, 0, Qt::AlignHCenter);
m_btnClose = new QPushButton(this);
m_btnClose->setIcon(QIcon("...configure/icons/guanbi.png"));
m_btnClose->setIconSize(QSize(20, 20));
topHLayout->addWidget(leftTmp, 0, Qt::AlignLeft);
topHLayout->addWidget(m_btnClose, 0, Qt::AlignRight);
connect(m_btnClose,SIGNAL(clicked()),this,SLOT(sltOnBtnClose()));
}
void CustomMsgBox::initBottomWgt()
{
QWidget *tmp = new QWidget(m_bottomWgt);
tmp->setStyleSheet("border: 1px solid rgb(44, 112, 177); background : rgb(255, 255, 255);");
QVBoxLayout * tmpL = new QVBoxLayout(m_bottomWgt);
tmpL->setContentsMargins(0, 0, 0, 0);
tmpL->addWidget(tmp);
m_bottomBoxVLayout = new QVBoxLayout(tmp);
m_lblContent = new QLabel(tmp);
m_lblContent->setText(m_content);
m_lblContent->setStyleSheet("border: 0px;font-family:Microsoft YaHei UI;font-size:14px;color:rgb(74,74,74);");
QWidget *hBoxButtins = new QWidget(tmp);
hBoxButtins->setStyleSheet("border:0px");
QHBoxLayout *hLayoutButtons = new QHBoxLayout(hBoxButtins);
hLayoutButtons->setContentsMargins(0,0,0,0);
hLayoutButtons->addWidget(m_pButtonBox,Qt::AlignRight);
m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons((int)m_standButtons));
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(84, 30));
if(button == QDialogButtonBox::Ok || button == QDialogButtonBox::Yes) {
pushButton->setText(QString::fromLocal8Bit("确 定"));
} else if(button == QDialogButtonBox::Cancel){
pushButton->setText(QString::fromLocal8Bit("取 消"));
}
pushButton->setStyleSheet(strBtnDarkSty);
}
m_bottomBoxVLayout->setContentsMargins(48, 30, 48, 10);
m_bottomBoxVLayout->addWidget(m_lblContent,0,Qt::AlignCenter);
m_bottomBoxVLayout->addWidget(hBoxButtins,0,Qt::AlignCenter);
connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this,SLOT(onButtonClicked(QAbstractButton*)));
}
void CustomMsgBox::setDefalutButton(QPushButton *button)
{
if(!m_pButtonBox->buttons().contains(button)) {
return ;
}
button->setDefault(true);
button->setFocus();
}
void CustomMsgBox::setDefaultButton(QMessageBox::StandardButton defaultButton )
{
//setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(defaultButton)));
}
int CustomMsgBox::execReturnCode(QAbstractButton *button)
{
return m_pButtonBox->standardButton(button);
}
void CustomMsgBox::onButtonClicked(QAbstractButton *button)
{
done(execReturnCode(button));
}
void CustomMsgBox::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
m_mouserPressed = true;
m_point = event->globalPos() - this->pos();
event->accept();
}
}
void CustomMsgBox::mouseMoveEvent(QMouseEvent *event)
{
if(m_mouserPressed && (event->buttons() & Qt::LeftButton)) {
this->move(event->globalPos() - m_point);
event->accept();
}
}
void CustomMsgBox::mouseReleaseEvent(QMouseEvent * /*event*/)
{
m_mouserPressed = false;
}
void CustomMsgBox::sltOnBtnClose()
{
this->close();
}
bool CustomMsgBox::eventFilter(QObject* obj, QEvent *event)
{
if (QEvent::MouseButtonDblClick == event->type()) {
if (obj == m_btnIcon) {
close();
return true;
}
}
return false;
}
CustomMsgBox::~CustomMsgBox()
{
}
3.CustomMsgBox调用
custommsgbox = new CustomMsgBox(this,QString::fromLocal8Bit("系统检测到有更新"),QString::fromLocal8Bit("是否现在进行更新?"),QMessageBox::Open|QMessageBox::Cancel|QMessageBox::Ok);
int btn = custommsgbox->exec();
switch (btn) {
case QMessageBox::Ok:
break;
case QMessageBox::Cancel:
break;
case QMessageBox::Open:
break;
}
最后
以上就是迷路美女为你收集整理的Qt软件开发文档17---自定义messagebox窗口的全部内容,希望文章能够帮你解决Qt软件开发文档17---自定义messagebox窗口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复