Qt之自定义用户名输入框 QLineEdit+QLabel+QComboBox 带输入自动提示补全和历史登录用户记忆

前几天美工给设计出了一个用户登录界面,其中用户名输入框样式如下图:

左侧的“用户名”三个字为固定文字,中间可输入用户名,点击右侧的倒三角,可以显示下拉框,下拉框中显示的是历史登录成功的用户名,且在输入用户名时,若下拉框中有完整的用户名时进行补全提示。
本人采用的是最外层为QLineEdit控件,在QLineEdit控件中填充水平布局器,布局器中依次放入QLabel用于填写“用户名”固定数据,QComboBox,设置QComboBox样式为无边框,则样式看起来就跟上面展示的图片一样了,用户名补全的功能也是沿用了QComboBox的补全提示功能,主体代码如下:
.h文件

#ifndef MYLINEEDITCTRL_H
#define MYLINEEDITCTRL_H

#include <QLineEdit>
#include <QHBoxLayout>
#include <QWidget>
#include <QLabel>
#include <QComboBox>
#include <QCompleter>

#define NameMaxLen 20 //限制用户名的最大输入长度,可根据自己需要设置

class UserNameCtrl : public QLineEdit
{
    Q_OBJECT
signals:
    void sig_TextChanged(const QString &sName);//当用户名数据有变化时发出的信号,内容为当前的用户名数据

public:
    explicit UserNameCtrl(QWidget *parent = nullptr);
    ~UserNameCtrl();

public:
    void setTipInfo(QString sTip);//设置“用户名”字样
    void setUserList(QStringList list);//设置历史记忆的多个QComboBox用户名数据
    void setCurText(QString sText);//设置当前的用户名,若用户存在则直接设置,不存在先插入QComboBox再设置
    QString curText();//获取当前设置的用户名

private slots:
    void slot_TextChanged(const QString &text);

private:
    void initData();
    void initConnect();
    void initDisplay();

private:
    QLabel *m_label;
    QComboBox *m_combo;
    QHBoxLayout *mainLayout;
    QHBoxLayout *layout;
    QCompleter *m_comPleter;

private:
    bool _bTime;
};
#endif // MYLINEEDITCTRL_H

.cpp程序

#include "UserNameCtrl.h"
#include <QMessageBox>

UserNameCtrl::UserNameCtrl(QWidget *parent):
    QLineEdit(parent)
{
    this->initData();
    this->initConnect();
    this->initDisplay();
}

UserNameCtrl::~UserNameCtrl()
{

}

void UserNameCtrl::initData()
{
    mainLayout = new QHBoxLayout;
    layout = new QHBoxLayout;
    m_label = new QLabel();
    m_combo = new QComboBox();
    m_combo->setEditable(true);
    _bTime = false;
    m_comPleter = nullptr;
}

void UserNameCtrl::initConnect()
{
    connect(m_combo,SIGNAL(currentTextChanged(const QString &)),this,SLOT(slot_TextChanged(const QString &)));
}

void UserNameCtrl::initDisplay()
{
    mainLayout->addWidget(m_label);
    layout->addWidget(m_combo);
    mainLayout->addLayout(layout);
    mainLayout->setStretch(0,0);
    mainLayout->setStretch(1,1);

    setTextMargins(60,0,0,0);
    setContentsMargins(0,0,0,0);
    setLayout(mainLayout);

    m_combo->setCursor(Qt::PointingHandCursor);
}

void UserNameCtrl::setTipInfo(QString sTip)
{
    m_label->setText(sTip);
}

void UserNameCtrl::setUserList(QStringList list)
{
    m_combo->addItems(list);
    if(nullptr != m_comPleter)
    {
        delete m_comPleter;
        m_comPleter = nullptr;
    }
    m_comPleter = new QCompleter(list, this);
    m_comPleter->setCaseSensitivity(Qt::CaseInsensitive);//补齐大小写不敏感
    m_comPleter->setModelSorting(QCompleter::CaseInsensitivelySortedModel);//大小写不敏感排序
    m_combo->setCompleter(m_comPleter);
}

void UserNameCtrl::setCurText(QString sText)
{
    if(-1 == m_combo->findText(sText))//当前没有该用户
    {
        QStringList list;
        list.clear();
        m_combo->addItem(sText);
    }

    m_combo->setCurrentText(sText);
}

QString UserNameCtrl::curText()
{
    return m_combo->currentText();
}

void UserNameCtrl::slot_TextChanged(const QString &text)
{
    if(text.length() > NameMaxLen)
    {
        m_combo->setCurrentText(text.left(NameMaxLen));
        return ;
    }
    emit sig_TextChanged(text);
}

至此自定的用户名控件已经大功告成,可以通过控件提升的方式,将当做用户名的控件提升为该类,控件提升的方法在此不再多说。
只有该文件只能看到一个粗陋的用户名控件,并不像文章开头那般好看,对应的样式表设置在此不再多说,可以根据自己需要自己设置。

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值