Qt学习:QLineEdit的程序示例

学习了上一篇博客关于QLineEdit的一些重要的成员函数和信号的用法之后,我们写一个小程序来熟练下这些函数.


这里是程序完成后的图片.
这里写图片描述

首先,我们现在ui设计师里拖拽出以下的布局:
注意箭头处还有个QLabel部件.
这里写图片描述


以下是”c.cpp”下的代码:

#include "c.h"

c::c(QWidget *parent)
: QWidget(parent)
{
    ui.setupUi(this);
    //设置标题为"QLineEdit"
    this->setWindowTitle("QQ");
    //设置图标.
    this->setWindowIcon(QIcon("pixmap.jpg"));
    //设置为伙伴.
    ui.accountLabel->setBuddy(ui.accountLineEdit);
    ui.passwordLabel->setBuddy(ui.passwordLineEdit);
    //设置头像.
    ui.pixmapLabel->setPixmap(QPixmap("pixmap.jpg"));
    //让label完整的放下图片.根据比例缩减.
    ui.pixmapLabel->setScaledContents(true);
    //设置密码输入框的显示模式为:密码显示模式.
    ui.passwordLineEdit->setEchoMode(QLineEdit::Password);
    //设置激活两个输入框的清空按钮.
    ui.accountLineEdit->setClearButtonEnabled(true);
    ui.passwordLineEdit->setClearButtonEnabled(true);
    //设置两个输入框的最大长度
    ui.accountLineEdit->setMaxLength(10);
    ui.passwordLineEdit->setMaxLength(10);
    //设置账号输入框允许拖拽.
    ui.accountLineEdit->setDragEnabled(true);
    //设置密码输入框不接受拖拽的文本.
    ui.passwordLineEdit->setAcceptDrops(false);

    //设置两个输入框的占位符.
    ui.accountLineEdit->setPlaceholderText(QString::fromLocal8Bit("必须为纯数字"));
    ui.passwordLineEdit->setPlaceholderText(QString::fromLocal8Bit("两位字母+纯数字"));

    //设置账号输入框的整数验证器,并且设定输入的范围.
    QIntValidator *account = new QIntValidator;
    account->setBottom(0);
    ui.accountLineEdit->setValidator(account);

    //设置密码输入框的验证器,用了正则表达式来规定:前面1-2个必须是字母,后面8-9个必须是0-9内的数字.
    QRegExp password("[A-Za-z]{2}[0-9]{8,9}");
    ui.passwordLineEdit->setValidator(new QRegExpValidator(password, this));

    //设置密码输入框中,按鼠标右键无菜单显示.和QQ一样.有心的小伙伴可以去试一下.
    ui.passwordLineEdit->setContextMenuPolicy(Qt::NoContextMenu);
    //但是对于Ctrl+V粘贴的快捷操作还没有屏蔽.有兴趣的可以去自己试试.这里不展开了.

    //连接信号与槽.
    connect(ui.showAccountButton, SIGNAL(clicked()), this, SLOT(showAccountNameSlot()));
    connect(ui.showPasswordButton, SIGNAL(clicked()), this, SLOT(showPasswordSlot()));
    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(showEntryDialogSlot()));
    connect(ui.passwordLineEdit, SIGNAL(returnPressed()), ui.pushButton, SIGNAL(clicked()));
}

c::~c()
{

}
void c::showAccountNameSlot()
{
    QMessageBox::information(this, QString::fromLocal8Bit("账号:"), ui.accountLineEdit->text());
}
void c::showPasswordSlot()
{
    //注意text()返回的是本身的文本,而displayText()返回的是显示的文本.
    QMessageBox::information(this, QString::fromLocal8Bit("密码:"), ui.passwordLineEdit->text());
}
void c::showEntryDialogSlot()
{
    //这里只是为了演示QLineEdit的用法,就没有实现注册,登录等功能.所以这是简单的登陆下.
    if ((ui.accountLineEdit->text() == "123456") && (ui.passwordLineEdit->text() == "qw123456"))
    {
        QMessageBox::information(this, QString::fromLocal8Bit("登录"), QString::fromLocal8Bit("登录成功"));
    }
    else
    {
        QMessageBox::warning(this, QString::fromLocal8Bit("登录"), QString::fromLocal8Bit("登录失败"));
    }
}

以下是”c.h”内的代码:

#ifndef C_H
#define C_H

#include <QtWidgets/QWidget>
#include "ui_c.h"
#include <QMessageBox>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QValidator>
#include <QRegExp>
#include <QMessageBox>
#include <QPixmap>

class c : public QWidget
{
    Q_OBJECT

public:
    c(QWidget *parent = 0);
    ~c();
private slots:
    void showAccountNameSlot();
    void showPasswordSlot();
    void showEntryDialogSlot();

private:
    Ui::cClass ui;
};

#endif // C_H

最后是”main.cpp”内的代码:

#include "c.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    c w;
    w.show();
    return a.exec();
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义QLineEdit的右键菜单,可以通过重写QLineEdit的contextMenuEvent()函数来实现。下面是一个示例代码: ```cpp CustomLineEdit::CustomLineEdit(QWidget *parent) : QLineEdit(parent) { setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &CustomLineEdit::customContextMenuRequested, this, &CustomLineEdit::showCustomContextMenu); } void CustomLineEdit::showCustomContextMenu(const QPoint &pos) { QMenu menu(this); QAction *action = menu.addAction("Custom Action"); connect(action, &QAction::triggered, this, &CustomLineEdit::onCustomActionTriggered); menu.exec(mapToGlobal(pos)); } void CustomLineEdit::onCustomActionTriggered() { // 处理自定义操作 } ``` 在上面的代码中,我们首先通过setContextMenuPolicy()函数将QLineEdit的上下文菜单策略设置为Qt::CustomContextMenu,然后在构造函数中连接customContextMenuRequested信号到我们自己的槽函数showCustomContextMenu()。 在showCustomContextMenu()函数中,我们创建一个QMenu对象,并添加我们自己的自定义操作。这里我们只添加了一个名为"Custom Action"的操作,当用户点击这个操作时,会触发onCustomActionTriggered()槽函数。 最后,我们在showCustomContextMenu()函数中调用menu.exec()函数来显示菜单。由于我们需要将菜单显示在鼠标点击的位置,所以需要使用mapToGlobal()函数将QPoint对象转换为全局坐标系。 在onCustomActionTriggered()函数中,我们可以处理我们自己的自定义操作。这里我们只是简单地打印一条消息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值