前几天美工给设计出了一个用户登录界面,其中用户名输入框样式如下图:
左侧的“用户名”三个字为固定文字,中间可输入用户名,点击右侧的倒三角,可以显示下拉框,下拉框中显示的是历史登录成功的用户名,且在输入用户名时,若下拉框中有完整的用户名时进行补全提示。
本人采用的是最外层为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);
}
至此自定的用户名控件已经大功告成,可以通过控件提升的方式,将当做用户名的控件提升为该类,控件提升的方法在此不再多说。
只有该文件只能看到一个粗陋的用户名控件,并不像文章开头那般好看,对应的样式表设置在此不再多说,可以根据自己需要自己设置。