[Qt programming] Automatic completion function, qt programming completion
Recently I wrote a software similar to youdao dictionary for word search, which has an automatic complementing function (that is, when you enter a letter, several candidate items will appear ). This auto-completion function is very common and will appear when Baidu searches for keywords. However, these complementing functions match the first word you enter, and sometimes it is not convenient. For example, if I enter "good", if it is a first-character match, it will appear:
If it is a match in a sentence, this is the case:
You can select a mode based on your requirements. Qt comes with the QCompleter class to implement the above Automatic completion function. Readers can easily learn how to use this class in the demo of Qt.
Next I will talk about constructing a class that is more powerful than QCompleter.. Some people may say, why do they need to write a class on their own if they are ready-made? Because, when I used the QCompleter class, I found that it only has the match mode of the first sentence (maybe I did not read the document carefully and did not know how to change the pattern). Second, when my dictionary is very large, sometimes the drop-down auto-Completion list will not appear, and the specific reasons are unclear. Therefore, I wrote a class to implement the functionality that the QCompleter class does not have. For more information, see the Code directly (the code annotation is more detailed, so I will not explain it carefully, the widget. ui file is not provided, it is an empty Interface): 1. widget. h
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<QMouseEvent>namespace Ui {class Widget;}class Widget : public QWidget{ Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); void mousePressEvent(QMouseEvent *event);private: Ui::Widget *ui;signals: void movesignal();};#endif // WIDGET_H
2. completelineedit. h
# Ifndef define # define COMPLETELINEEDIT_H # include <QLineEdit> # include <QStringList> # include <QFile> # include <QTextCodec> # include <QDebug> class QListView; class QStringListModel; class QModelIndex; class CompleteLineEdit: public QLineEdit {Q_OBJECTpublic: CompleteLineEdit (QStringList words, QWidget * parent = 0); public slots: void setCompleter (const QString & text ); // dynamic display Completion list void completeText (const QModelIndex & index); // click Finish in the list to use this option to automatically complete the input word protected: virtual void keyPressEvent (QKeyEvent * e); virtual void focusOutEvent (QFocusEvent * e); private slots: void replyMoveSignal (); private: QStringList words; // The word QListView * listView for the complete list; // The completed list QStringListModel * model; // The completed list model}; # endif // COMPLETELINEEDIT_H
3. widget. cpp
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this);}Widget::~Widget(){ delete ui;}void Widget::mousePressEvent(QMouseEvent *event){ emit movesignal();}
4. completelineedit. cpp
# Include "CompleteLineEdit. h "# include <QKeyEvent> # include <QListView> # include <QStringListModel> # include <QDebug> CompleteLineEdit: CompleteLineEdit (QStringList words, QWidget * parent): QLineEdit (parent ), words (words) {listView = new QListView (this); // used to display the drop-down list model = new QStringListModel (this); listView-> setWindowFlags (Qt: ToolTip ); // set the style connect (this, SIGNAL (textChanged (const QString &), th Is, SLOT (setCompleter (const QString &); connect (listView, SIGNAL (clicked (const QModelIndex &), this, SLOT (completeText (const QModelIndex &);} void CompleteLineEdit: focusOutEvent (QFocusEvent * e) {// listView-> hide (); // hide the automatically completed drop-down list} void CompleteLineEdit: replyMoveSignal () {listView-> hide () when the input row is not focused, hide auto-completion drop-down list} void CompleteLineEdit: keyPressEvent (QKeyEvent * e) {if (! ListView-> isHidden () {int key = e-> key (); int count = listView-> model ()-> rowCount (); QModelIndex currentIndex = listView-> currentIndex (); if (Qt: Key_Down = key) {// if you press the downward arrow key, int row = currentIndex. row () + 1; if (row> = count) {row = 0;} QModelIndex index = listView-> model ()-> index (row, 0 ); listView-> setCurrentIndex (index);} else if (Qt: Key_Up = key) {// int row = currentIndex when you press the downward arrow key. row ()-1; if (row <0) {row = count-1;} QModelIndex index = listView-> model ()-> index (row, 0 ); listView-> setCurrentIndex (index);} else if (Qt: Key_Escape = key) {// hide the completed list listView-> hide ();} else if (Qt: Key_Enter = key | Qt: Key_Return = key) {// when you press the Enter key, use the items in the Completion list, and hide the complete list if (currentIndex. isValid () {QString text = listView-> currentIndex (). data (). toString (); setText (text);} list View-> hide ();} else {// other situations, hide the Completion list, and press the event listView-> hide (); QLineEdit on the keyboard of QLineEdit :: keyPressEvent (e) ;}} else {QLineEdit: keyPressEvent (e) ;}} void CompleteLineEdit: setCompleter (const QString & text) {if (text. isEmpty () // {listView-> hide (); return;} if (text. length ()> 1 )&&(! ListView-> isHidden () {return;} // if a word in the complete Completion list contains the input text, add the QStringList sl in the list of completion items to be displayed; foreach (QString word, words) {// fill Mode 1 if (word. contains (text) // It is displayed as long as it contains the input content. You can also set case insensitive {sl <word ;}// fill Mode 2 // if (word. indexOf (text, 0, Qt: CaseInsensitive) = 0) // The content must be the same as the first sentence. // sl <word;} model-> setStringList (sl ); listView-> setModel (model); if (model-> rowCount () = 0) {return ;} // set the position and size of the list. listView-> setMinimumWidth (width (); listView-> setMaximumWidth (width (); QPoint p (0, height ()); int x = mapToGlobal (p ). x (); int y = mapToGlobal (p ). y () + 1; listView-> move (x, y); listView-> show ();} void CompleteLineEdit: completeText (const QModelIndex & index) {QString text = index. data (). toString (); setText (text); listView-> hide ();}
5. main. cpp
# Include <QApplication> # include "CompleteLineEdit. h "# include" widget. h "int main (int argc, char * argv []) {QApplication a (argc, argv); QStringList sl; QFile * inFile = new QFile (" input.txt "); // This is your own dictionary if (! InFile-> open (QIODevice: ReadOnly | QIODevice: Text) {qDebug () <"cannot read! ";} While (! InFile-> atEnd () {QByteArray line = inFile-> readLine (); QTextCodec * gbk_codec = QTextCodec: codecForName ("GBK "); QString gbk_string = gbk_codec-> toUnicode (line); if (! Line. isEmpty () sl <gbk_string.trimmed (); // enter the words in the file into sl} inFile-> close (); // close the sl file <"" <"" <"Naive" <"How are you "; widget * w = new Widget (); CompleteLineEdit * edit = new CompleteLineEdit (sl, w); w-> show (); // QObject: connect (w, SIGNAL (movesignal (), edit, SLOT (replyMoveSignal (); return a.exe c ();}
Finally, let's put two auto-completion functions used by the word search software:
How to Set automatic completion of Qt Creator code
Hmm.
Some files in QT are not automatically completed, such as QString and QFile. However, after the header file QtGui is added, it can be automatically completed. Why?
Your version may be faulty. I tried it. As long as the header file containing the QString header file also has the QString auto-completion function. This is only a function of the editor. It will search for the corresponding functions in the header file. However, common functions must be memorized. You can try another QCreator.