技术博客2

来源:互联网 发布:java孤傲苍狼 博客园 编辑:程序博客网 时间:2024/05/20 11:52

简述

通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget、QDialog、QMainWindow。

大多数窗体的实现都是采用控件堆积来完成的,只要思路清晰,再复杂的界面实现起来都游刃有余。

效果

这里写图片描述

这里写图片描述 这里写图片描述

这里写图片描述 这里写图片描述

自定义提示框

message_box.h

#ifndef MESSAGE_BOX#define MESSAGE_BOX#include <QMessageBox>#include <QDialogButtonBox>#include <QGridLayout>#include "custom_window.h"class QLabel;class MessageBox : public CustomWindow{    Q_OBJECTpublic:    explicit MessageBox(QWidget *parent = 0, const QString &title = tr("Tip"), const QString &text = "",                        QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::Ok);    ~MessageBox();    QAbstractButton *clickedButton() const;    QMessageBox::StandardButton standardButton(QAbstractButton *button) const;    // 设置默认按钮    void setDefaultButton(QPushButton *button);    void setDefaultButton(QMessageBox::StandardButton button);    // 设置窗体标题    void setTitle(const QString &title);    // 设置提示信息    void setText(const QString &text);    // 设置窗体图标    void setIcon(const QString &icon);    // 添加控件-替换提示信息所在的QLabel    void addWidget(QWidget *pWidget);protected:    // 多语言翻译    void changeEvent(QEvent *event);private slots:    void onButtonClicked(QAbstractButton *button);private:    void translateUI();    int execReturnCode(QAbstractButton *button);private:    QLabel *m_pIconLabel;    QLabel *m_pLabel;    QGridLayout *m_pGridLayout;    QDialogButtonBox *m_pButtonBox;    QAbstractButton *m_pClickedButton;    QAbstractButton *m_pDefaultButton;};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

message_box.cpp

#include <QLabel>#include <QPushButton>#include <QMessageBox>#include <QCheckBox>#include <QHBoxLayout>#include <QEvent>#include <QApplication>#include "message_box.h"MessageBox::MessageBox(QWidget *parent, const QString &title, const QString &text,                       QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)    : CustomWindow(parent){    setWindowIcon(QIcon(":/Images/logo"));    setWindowTitle(title);    setMinimumSize(300, 130);    setMinimizeVisible(false);    setMaximizeVisible(false);    setWidgetResizable(false);    m_pButtonBox = new QDialogButtonBox(this);    m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));    setDefaultButton(defaultButton);    QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);    if (pYesButton != NULL)    {        pYesButton->setObjectName("blueButton");        pYesButton->setStyle(QApplication::style());    }    m_pIconLabel = new QLabel(this);    m_pLabel = new QLabel(this);    QPixmap pixmap(":/Images/information");    m_pIconLabel->setPixmap(pixmap);    m_pIconLabel->setFixedSize(35, 35);    m_pIconLabel->setScaledContents(true);    m_pLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);    m_pLabel->setObjectName("whiteLabel");    m_pLabel->setOpenExternalLinks(true);    m_pLabel->setText(text);    m_pGridLayout = new QGridLayout();    m_pGridLayout->addWidget(m_pIconLabel, 0, 0, 2, 1, Qt::AlignTop);    m_pGridLayout->addWidget(m_pLabel, 0, 1, 2, 1);    m_pGridLayout->addWidget(m_pButtonBox, m_pGridLayout->rowCount(), 0, 1, m_pGridLayout->columnCount());    m_pGridLayout->setSizeConstraint(QLayout::SetNoConstraint);    m_pGridLayout->setHorizontalSpacing(10);    m_pGridLayout->setVerticalSpacing(10);    m_pGridLayout->setContentsMargins(10, 10, 10, 10);    m_pLayout->addLayout(m_pGridLayout);    translateUI();    connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));}MessageBox::~MessageBox(){}void MessageBox::changeEvent(QEvent *event){    switch (event->type())    {    case QEvent::LanguageChange:        translateUI();        break;    default:        CustomWindow::changeEvent(event);    }}void MessageBox::translateUI(){    QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);    if (pYesButton != NULL)        pYesButton->setText(tr("Yes"));    QPushButton *pNoButton = m_pButtonBox->button(QDialogButtonBox::No);    if (pNoButton != NULL)        pNoButton->setText(tr("No"));    QPushButton *pOkButton = m_pButtonBox->button(QDialogButtonBox::Ok);    if (pOkButton != NULL)        pOkButton->setText(tr("Ok"));    QPushButton *pCancelButton = m_pButtonBox->button(QDialogButtonBox::Cancel);    if (pCancelButton != NULL)        pCancelButton->setText(tr("Cancel"));}QMessageBox::StandardButton MessageBox::standardButton(QAbstractButton *button) const{    return (QMessageBox::StandardButton)m_pButtonBox->standardButton(button);}QAbstractButton *MessageBox::clickedButton() const{    return m_pClickedButton;}int MessageBox::execReturnCode(QAbstractButton *button){    int nResult = m_pButtonBox->standardButton(button);    return nResult;}void MessageBox::onButtonClicked(QAbstractButton *button){    m_pClickedButton = button;    done(execReturnCode(button));}void MessageBox::setDefaultButton(QPushButton *button){    if (!m_pButtonBox->buttons().contains(button))        return;    m_pDefaultButton = button;    button->setDefault(true);    button->setFocus();}void MessageBox::setDefaultButton(QMessageBox::StandardButton button){    setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(button)));}void MessageBox::setTitle(const QString &title){    setWindowTitle(title);}void MessageBox::setText(const QString &text){    m_pLabel->setText(text);}void MessageBox::setIcon(const QString &icon){    m_pIconLabel->setPixmap(QPixmap(icon));}void MessageBox::addWidget(QWidget *pWidget){    m_pLabel->hide();    m_pGridLayout->addWidget(pWidget, 0, 1, 2, 1);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151

接口说明

  • CustomWindow

    主要对界面的无边框可拖动进行了封装

  • MessageBox

    整体界面布局及事件处理参考了QMessageBox源码,接口包含:设置标题、提示信息、默认按钮及事件触发等操作。

二次封装

针对于各种提示框,我们可以再次进行封装,将常用的提取出来,作为全局函数来使用。

QMessageBox::StandardButton showInformation(QWidget *parent, const QString &title,                                            const QString &text, QMessageBox::StandardButtons buttons,                                            QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/Images/information");    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    return msgBox.standardButton(msgBox.clickedButton());}QMessageBox::StandardButton showError(QWidget *parent, const QString &title,                                      const QString &text, QMessageBox::StandardButtons buttons,                                      QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/Images/error");    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    return msgBox.standardButton(msgBox.clickedButton());}QMessageBox::StandardButton showSuccess(QWidget *parent, const QString &title,                                        const QString &text, QMessageBox::StandardButtons buttons,                                        QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/Images/success");    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    return msgBox.standardButton(msgBox.clickedButton());}QMessageBox::StandardButton showQuestion(QWidget *parent, const QString &title,                                         const QString &text, QMessageBox::StandardButtons buttons,                                         QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/Images/question");    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    return msgBox.standardButton(msgBox.clickedButton());}QMessageBox::StandardButton showWarning(QWidget *parent, const QString &title,                                        const QString &text, QMessageBox::StandardButtons buttons,                                        QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/images/warning");    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    return msgBox.standardButton(msgBox.clickedButton());}QMessageBox::StandardButton showCritical(QWidget *parent, const QString &title,                                         const QString &text, QMessageBox::StandardButtons buttons,                                         QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/Images/warning");    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    return msgBox.standardButton(msgBox.clickedButton());}QMessageBox::StandardButton showCheckBoxQuestion(QWidget *parent, const QString &title,                                                 const QString &text, QMessageBox::StandardButtons buttons,                                                 QMessageBox::StandardButton defaultButton){    MessageBox msgBox(parent, title, text, buttons, defaultButton);    msgBox.setIcon(":/Images/question");    QCheckBox *pCheckBox = new QCheckBox(&msgBox);    pCheckBox->setText(text);    msgBox.addWidget(pCheckBox);    if (msgBox.exec() == -1)        return QMessageBox::Cancel;    QMessageBox::StandardButton standardButton = msgBox.standardButton(msgBox.clickedButton());    if (standardButton == QMessageBox::Yes)    {        return pCheckBox->isChecked() ? QMessageBox::Yes : QMessageBox::No;    }    return QMessageBox::Cancel;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

使用方式

showInformation(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Information!"));showQuestion(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Question!"));showSuccess(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Success!"));showError(this, QStringLiteral("提示"), QStringLiteral("这是一个普通的提示框-Error!"));
原创粉丝点击