创建一个“密码设置”窗口
代码效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5f8863a22f57f5f1c4c51867028629e5.png)
代码
PasswordInputDialog.h
#ifndef PASSWORDINPUTDIALOG_H
#define PASSWORDINPUTDIALOG_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;
class PasswordInputDialog : public QDialog
{
Q_OBJECT
public:
explicit PasswordInputDialog(QWidget *parent = 0);
~PasswordInputDialog();
private slots:
void OkButtonClicked(); //“确认”按钮的响应槽
private:
QLineEdit *m_pInputLineEdit;
QLineEdit *m_pAgainLineEdit;
QPushButton *m_pOkButton;
void DrawWindow(); //绘制界面
};
#endif // PASSWORDINPUTDIALOG_H
PasswordInputDialog.cpp
#include "PasswordInputDialog.h"
#include "ui_PasswordInputDialog.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "qpushbutton.h"
PasswordInputDialog::PasswordInputDialog(QWidget *parent) : QDialog(parent)
{
DrawWindow();
this->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint); //标题栏只显示关闭按钮
m_pInputLineEdit->setEchoMode(QLineEdit::Password); //设置为“密码”模式
m_pAgainLineEdit->setEchoMode(QLineEdit::Password);
connect(m_pOkButton, SIGNAL(clicked(bool)), this, SLOT(OkButtonClicked()));
}
PasswordInputDialog::~PasswordInputDialog()
{
delete m_pInputLineEdit;
delete m_pAgainLineEdit;
delete m_pOkButton;
}
//绘制界面
void PasswordInputDialog::DrawWindow()
{
QHBoxLayout *pInputHBoxLayout = new QHBoxLayout;
QLabel *pInputLabel = new QLabel(QString::fromUtf8("请输入密码"));
pInputLabel->setFixedWidth(100);
m_pInputLineEdit = new QLineEdit;
m_pInputLineEdit->setPlaceholderText(QString::fromUtf8("请输入密码")); //QLineEdit设置背景文字
pInputHBoxLayout->addWidget(pInputLabel);
pInputHBoxLayout->addWidget(m_pInputLineEdit);
QHBoxLayout *pAgainHBoxLayout = new QHBoxLayout;
QLabel *pAgainLabel = new QLabel(QString::fromUtf8("再次输入密码"));
pAgainLabel->setFixedWidth(100);
m_pAgainLineEdit = new QLineEdit;
m_pAgainLineEdit->setPlaceholderText(QString::fromUtf8("再次输入密码"));
pAgainHBoxLayout->addWidget(pAgainLabel);
pAgainHBoxLayout->addWidget(m_pAgainLineEdit);
QHBoxLayout *pButtonsHBoxLayout = new QHBoxLayout;
m_pOkButton = new QPushButton(QString::fromUtf8("确 定"));
pButtonsHBoxLayout->addStretch();
pButtonsHBoxLayout->addWidget(m_pOkButton);
QVBoxLayout *pVBoxLayout = new QVBoxLayout(this);
pVBoxLayout->addLayout(pInputHBoxLayout);
pVBoxLayout->addLayout(pAgainHBoxLayout);
pVBoxLayout->addLayout(pButtonsHBoxLayout);
QWidget *pWidget = new QWidget(this);
pWidget->setLayout(pVBoxLayout);
this->setWindowTitle(QString::fromUtf8("设置密码")); //设置窗口名称
this->setFixedSize(270, 90);
}