qt Qcompleter自动补全类

文档:The QCompleter class provides completions based on an item model.

You can use QCompleter to provide auto completions in any Qt widget, such as QLineEdit and QComboBox. When the user starts typing a word, QCompleter suggests possible ways of completing the word, based on a word list. The word list is provided as a QAbstractItemModel. (For simple applications, where the word list is static, you can pass a QStringList to QCompleter’s constructor.)
翻译:qcompleter类提供了基于item model的自动补全。
你可以使用QCompleter去自动补全qt中的任何widget,例如,qlineEdit和qcomboBox.当用户开始敲一个单词的时候,qcompleter会提供基于word list的尽可能合适的自动补全匹配单词。word list基于qabstracitemmodel被提供(对于简单的应用程序,word list是静态的,你可以传递一个QStringlist给Qcompleter的构造函数)

补充小知识点:Qt::CaseSensitivity 为枚举类型, 可取值Qt::CaseSensitive 和 Qt::CaseInsensitive, 表示匹配的灵敏度。
例如:void findPrevious(const QString &str,Qt::CaseSensitivity cs);这个函数想要带一个const QString &str字符串参数,而后面跟的Qt::CaseSensitivity cs则是指输入的大小写要敏感。

QDirModel : public QAbstractItemModel(两者是继承关系)

win8自带截屏工具:在命令行输入snippingTool

以下摘自:http://blog.sina.com.cn/s/blog_a6fb6cc90101gu7w.html
QCompleter补全文件路径
QLineEdit *search_line_edit = new QLineEdit();
QCompleter *completer = new QCompleter();
QDirModel *dir_model = new QDirModel();
completer->setModel(dir_model);
search_line_edit->setCompleter(completer);
效果如下:
qt Qcompleter自动补全类_第1张图片

QCompleter补全单词
QStringList word_list;
word_list<<”Java”<<”C++”<<”C#”<<”PHP”<<”Perl”<<”Python”<<”Delphi”<<”Ruby”;
QLineEdit *search_line_edit = new QLineEdit();
QCompleter *completer = new QCompleter(word_list, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
search_line_edit->setCompleter(completer);
qt Qcompleter自动补全类_第2张图片

QCompleter添加新单词
search_line_edit = new QLineEdit();
completer = new QCompleter();
string_list_model = new QStringListModel();
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setModel(string_list_model);
search_line_edit->setCompleter(completer);
connect(search_line_edit, &QLineEdit::editingFinished, this, &MainWidget::editComplete);
槽函数如下:
void MainWidget::editComplete()
{
QString text = search_line_edit->text();
if(QString::compare(text, QString(“”)) != 0)
{
bool is_contains = word_list.contains(text, Qt::CaseInsensitive);
if(!is_contains)
{
word_list<

#include 
#include "QCompleter"
#include 
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void add_words();
private:
    Ui::MainWindow *ui;
    QStringList word_list;
    QStringListModel *stringlistmodel;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QCompleter *completer = new QCompleter();
    QDirModel *dir_model = new QDirModel();
    completer->setModel(dir_model);
    ui->lineEdit->setCompleter(completer);
    word_list<<"java"<<"c#"<<"c++"<<"python"<<"ruby";
    completer = new QCompleter(word_list, this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    ui->lineEdit->setCompleter(completer);
    stringlistmodel = new QStringListModel();
    completer->setModel(stringlistmodel);
    ui->lineEdit->setCompleter(completer);
    connect(ui->lineEdit, QLineEdit::editingFinished, this, add_words);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::add_words(){
    QString text = ui->lineEdit->text();
    if(!text.isEmpty()){
        if(!word_list.contains(text)){
            word_list<setStringList(word_list);
        }
    }
}

`

常用的方法
void setMaxVisibleItems(int maxItems)
设置最大显示数目
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
设置是否区分大小写
Qt::CaseSensitivity取值如下:
Qt::CaseInsensitive 0 大小写不敏感
Qt::CaseSensitive 1 大小写敏感
默认为:Qt::CaseSensitive
void setModelSorting(ModelSorting sorting)
设置排序方式
QCompleter::ModelSorting取值如下:
QCompleter::UnsortedModel 0 该模型是未排序
QCompleter::CaseSensitivelySortedModel 1 该模型是大小写敏感排序
QCompleter::CaseInsensitivelySortedModel 2 该模型是大小写不敏感排序的

你可能感兴趣的:(qt)