Qt浅谈之四十一QLineEdit的新样式和补全历史记录

一、简介

       利用背景图片设计出QLineEdit新的样式,起到美化界面的效果,并增加自动补全历史记录的功能,就可以作为一个完整的库。
 

二、详解

1、知识点

(1)切换QLineEdit的背景

void InnerLineEdit::setNormal()
{
    setText("");
    setTextMargins(-5,0,this->height(),0);    //solve the cursor problem
    setStyleSheet("background-image:url(:/searchInput.png);border:1px solid #536874;");
}

void InnerLineEdit::setSearching()
{
    setTextMargins(0,0,this->height(),0);
    setStyleSheet("background-image:url(:/searchInput_click.png);border:1px solid #536874;");
}
(2)QLineEdit模糊匹配
    QCompleter *completer = new QCompleter(this);
    listModel = new QStringListModel(valueList, this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    completer->setModel(listModel);
    this->setCompleter(completer);
(3)记录历史输入
connect(this, SIGNAL(editingFinished()), this, SLOT(editComplete()));
void InnerLineEdit::editComplete()
{
    QString text = this->text();
    if(QString::compare(text, QString("")) != 0) {
        bool flag = valueList.contains(text, Qt::CaseInsensitive);
        if(!flag) {
            addValue(text);
        }
    }
}

2、完整代码

(1)searchlineedit.h
#ifndef SEARCHLINEEDIT_H
#define SEARCHLINEEDIT_H

#include <QtGui>
class InnerLineEdit;
class SearchLineEdit : public QWidget
{
    Q_OBJECT

public:
    SearchLineEdit(QWidget *parent = 0);
    ~SearchLineEdit();

protected:
    void resizeEvent(QResizeEvent *event);

signals:
    void searchTextChanged(QString);

private slots:
    void textChange(QString text);
    void editClicked();
    void btnClicked();

private:
    InnerLineEdit *lineEdit;
    QPushButton *btn;
};

class InnerLineEdit : public QLineEdit
{
    Q_OBJECT

 public:
    explicit InnerLineEdit(QWidget *parent = 0);
    void setNormal();
    void setSearching();
    void addValue(const QString &value);

protected:
   void focusInEvent(QFocusEvent *event);
   void focusOutEvent(QFocusEvent *event);

 signals:
    void pressed();

private slots:
    void editComplete();

private:
    QStringList valueList;
    QStringListModel *listModel;
};

#endif // SEARCHLINEEDIT_H
(2)searchlineedit.cpp
#include "searchlineedit.h"

SearchLineEdit::SearchLineEdit(QWidget *parent)
    : QWidget(parent)
{
    lineEdit = new InnerLineEdit(this);
    lineEdit->move(8,38);
    lineEdit->resize(168,23);
    connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(textChange(QString)));
    connect(lineEdit,SIGNAL(pressed()),this,SLOT(editClicked()));

    btn = new QPushButton(this);
    btn->setFocusPolicy(Qt::NoFocus);
    btn->setCursor(Qt::PointingHandCursor);
    btn->resize(14,14);
    btn->move(157,42);
    btn->setStyleSheet("QPushButton{background-color:transparent;border:0px;}");
    btn->setFocusPolicy(Qt::NoFocus);
    btn->setVisible(false);
    connect(btn,SIGNAL(clicked()),this,SLOT(btnClicked()));

    this->setFocus();
    lineEdit->addValue("C");
    lineEdit->addValue("C++");
    lineEdit->addValue("Python");
    lineEdit->addValue("Php");
    lineEdit->addValue("Perl");
    lineEdit->addValue("Qt");
}

SearchLineEdit::~SearchLineEdit()
{

}

void SearchLineEdit::resizeEvent(QResizeEvent *event)
{
    move((QApplication::desktop()->width() - width())/2,  (QApplication::desktop()->height() - height())/2);
    QWidget::resizeEvent(event);
}

void SearchLineEdit::textChange(QString text)
{
    emit searchTextChanged(text);
}

void SearchLineEdit::editClicked()
{
    lineEdit->setSearching();
    btn->setVisible(true);
}

void SearchLineEdit::btnClicked()
{
    lineEdit->setNormal();
    btn->setVisible(false);
    this->setFocus();
}


/*******************************/
InnerLineEdit::InnerLineEdit(QWidget *parent)
   : QLineEdit(parent)
{
    setTextMargins(0,0,this->height(),0);
    setStyleSheet("background-image:url(:/searchInput.png);border:1px solid #536874;");

    QCompleter *completer = new QCompleter(this);
    listModel = new QStringListModel(valueList, this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    completer->setModel(listModel);
    this->setCompleter(completer);
    connect(this, SIGNAL(editingFinished()), this, SLOT(editComplete()));

}

void InnerLineEdit::setNormal()
{
    setText("");
    setTextMargins(-5,0,this->height(),0);    //solve the cursor problem
    setStyleSheet("background-image:url(:/searchInput.png);border:1px solid #536874;");
}

void InnerLineEdit::setSearching()
{
    setTextMargins(0,0,this->height(),0);
    setStyleSheet("background-image:url(:/searchInput_click.png);border:1px solid #536874;");
}

void InnerLineEdit::addValue(const QString &value)
{
    valueList.append(value);
    listModel->setStringList(valueList);
}

void InnerLineEdit::focusInEvent(QFocusEvent *event)
{
    emit pressed();
    QLineEdit::focusInEvent(event);
}

void InnerLineEdit::focusOutEvent(QFocusEvent *event)
{
    QLineEdit::focusInEvent(event);
}

void InnerLineEdit::editComplete()
{
    QString text = this->text();
    if(QString::compare(text, QString("")) != 0) {
        bool flag = valueList.contains(text, Qt::CaseInsensitive);
        if(!flag) {
            addValue(text);
        }
    }
}
(3)main.cpp
#include "searchlineedit.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    SearchLineEdit w;
    w.show();

    return a.exec();
}

(4)编译运行

Qt浅谈之四十一QLineEdit的新样式和补全历史记录_第1张图片
       其中的默认值有C、C++、Python、Php、Perl、Qt,输入第一个字符会自动不区分大小写的匹配判断,并显示在下拉列表中。若输入一个不存在的字符串,按回车后会保存下来,下次便会提示该字符串。

三、总结

(1)上述代码已上传到CSDN:http://download.csdn.net/detail/taiyang1987912/9409756
(2)若有问题或建议,请留言,在此感谢!

你可能感兴趣的:(linux,qt)