Qlineedit is automatically completed

Source: Internet
Author: User
Tags rowcount
------------------------------------- Completelineedit. h -------------------------------------

# Ifndef completelineedit_h

# Define completelineedit_h

 

# Include <qtgui/qlineedit>

# Include <qstringlist>

 

Class qlistview;

Class qstringlistmodel;

Class qmodelindex;

 

Class completelineedit: Public qlineedit {

Q_object

Public:

Completelineedit (qstringlist words, qwidget * parent = 0 );

 

Public slots:

Void setcompleter (const qstring & text); // dynamic display Completion list

Void completetext (const qmodelindex & Index); // click an item in the Completion list to use this item to automatically complete the entered word

 

Protected:

Virtual void keypressevent (qkeyevent * E );

Virtual void focusoutevent (qfocusevent * E );

 

PRIVATE:

Qstringlist words; // The words in the complete list

Qlistview * listview; // complete list

Qstringlistmodel * model; // model for completing the list

};

 

# Endif // completelineedit_h


------------------------------------- Completelineedit. cpp -------------------------------------

# Include "completelineedit. H"

# Include <qkeyevent>

# Include <qtgui/qlistview>

# Include <qtgui/qstringlistmodel>

# Include <qdebug>

 

Completelineedit: completelineedit (qstringlist words, qwidget * parent)

: Qlineedit (parent), words (words ){

Listview = new qlistview (this );

Model = new qstringlistmodel (this );

Listview-> setwindowflags (QT: tooltip );

 

Connect (this, signal (textchanged (const qstring &), this, slot (setcompleter (const qstring &)));

Connect (listview, signal (clicked (const qmodelindex &), this, slot (completetext (const qmodelindex &)));

}

 

Void completelineedit: focusoutevent (qfocusevent * E ){

// Listview-> hide ();

}

 

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 ){

// When you press the downward arrow key, move the cursor to select an item in the next Completion list

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 ){

// Press the downward arrow key and move the cursor to select the item in the previous Completion list.

Int ROW = currentindex. 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 Completion list when you press ESC.

Listview-> hide ();

} Else if (QT: key_enter = Key | QT: key_return = Key ){

// When you press the Enter key, use the items in the completed list and hide the completed list.

If (currentindex. isvalid ()){

Qstring text = listview-> currentindex (). Data (). tostring ();

Settext (text );

}

 

Listview-> hide ();

} Else {

// In other cases, hide the Completion list and press the event on the qlineedit keyboard.

Listview-> hide ();

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 it to the Completion list string to be displayed.

Qstringlist SL;

Foreach (qstring word, words ){

If (word. Contains (text )){

SL <word;

}

}

 

Model-> setstringlist (SL );

Listview-> setmodel (model );

 

If (model-> rowcount () = 0 ){

Return;

}

 

// Position the text edit

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 ();

}


------------------------------------- Main. cpp ----------------------------------

# Include <qtgui/qapplication>

# Include "completelineedit. H"

# Include <qtgui>

# Include <qcompleter>

# Include <qstringlist>

 

Int main (INT argc, char * argv []) {

Qapplication A (argc, argv );

 

Qstringlist SL = qstringlist () <"Biao" <"bin" <"Huang" <"Hua" <"hello" <"Binbin" <"Hallo ";

Qwidget widgetw;

Completelineedit * edit = new completelineedit (SL );

Qpushbutton * button = new qpushbutton ("button ");

Qhboxlayout * layout = new qhboxlayout ();

Layout-> addwidget (edit );

Layout-> addwidget (button );

Widgetw. setlayout (layout );

 

Widgetw. Show ();

 

Completelineedit E (SL );

E. Show ();

 

Return a.exe C ();

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.