QLineEdit(单行文字编辑控件)

创建一个“密码设置”窗口

代码效果图

在这里插入图片描述

代码

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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值