Example of qstringlistmodel
The source code is as follows:
Main. cpp
CPP Code
- # Include <qapplication>
- # Include "teamleadersdialog. H"
- IntMain (IntArgc,Char* Argv [])
- {
- Qapplication app (argc, argv );
- // String Array
- Qstringlist leaders;
- Leaders <"stooge viller" <"littleface" <"B-B eyes"
- <"Pruneface" <"mrs. Pruneface" <"the brow"
- <"Vitamin Flintheart" <"flattop Sr." <"Shakey"
- <"Breathless Mahoney" <"Mumbles" <"shoulders"
- <"Sketch Paree ";
- // Dialog box
- Teamleadersdialog Dialog (leaders );
- Dialog. Show ();
- Return app.exe C ();
- }
# Include <qapplication> # include "teamleadersdialog. H "int main (INT argc, char * argv []) {qapplication app (argc, argv); // String Array qstringlist leaders; leaders <"stooge viller" <"littleface" <"B-B eyes" <"Pruneface" <"mrs. pruneface "<" the brow "<" vitamin Flintheart "<" flattop Sr. "<" Shakey "<" Breathless Mahoney "<" Mumbles "<" shoulders "<" sketch Paree "; // dialog box teamleadersdialog DIERS (leaders); dialog. show (); Return app.exe C ();}
Teamleadersdialog. h
CPP Code
- # Ifndef teamleadersdialog_h
- # Define teamleadersdialog_h
- # Include <qdialog>
- Class qdialogbuttonbox;
- Class qlistview;
- Class qstringlistmodel;
- Class teamleadersdialog: Public qdialog
- {
- Q_object
- Public:
- // Constructor
- Teamleadersdialog (const qstringlist & leaders, qwidget * parent = 0 );
- Qstringlist leaders () const;
- Private slots:
- Void insert ();
- Void del ();
- PRIVATE:
- Qlistview * listview;
- Qdialogbuttonbox * buttonbox;
- Qstringlistmodel * model;
- };
- # Endif
# Ifndef example # define example # include <qdialog> class example; Class qlistview; Class qstringlistmodel; Class Example: Public qdialog {q_objectpublic: // const teamleadersdialog (const qstringlist & leaders, qwidget * parent = 0); qstringlist leaders () const; private slots: void insert (); void del (); Private: qlistview * listview; qdialogbuttonbox * buttonbox; qstringlistmodel * model;}; # endif
Teamleadersdialog. cpp
CPP Code
- # Include <qtgui>
- # Include "teamleadersdialog. H"
- Teamleadersdialog: teamleadersdialog (const qstringlist & leaders,
- Qwidget * parent)
- : Qdialog (parent)
- {
- // Create and assemble a qstringlistmodel
- Model = new qstringlistmodel (this );
- Model-> setstringlist (leaders );
- // Create a qlistview
- Listview = new qlistview;
- // Set the Model
- Listview-> setmodel (model );
- // Set the qlistview editing trigger: Enter the string editing status by entering or double-clicking
- Listview-> setedittriggers (q1_actitemview: anykeypressed
- | Qabstractitemview: doubleclicked );
- //
- Buttonbox = new qdialogbuttonbox ();
- Qpushbutton * insertbutton = buttonbox-> addbutton (TR ("& Insert "),
- Qdialogbuttonbox: actionrole );
- Qpushbutton * deletebutton = buttonbox-> addbutton (TR ("& Delete "),
- Qdialogbuttonbox: actionrole );
- Buttonbox-> addbutton (qdialogbuttonbox: OK );
- Buttonbox-> addbutton (qdialogbuttonbox: Cancel );
- // Signal slot binding insert and delete buttons
- Connect (insertbutton, signal (clicked (), this, slot (insert ()));
- Connect (deletebutton, signal (clicked (), this, slot (DEL ()));
- // The OK and cancel events in the snap box
- Connect (buttonbox, signal (accepted (), this, slot (accept ()));
- Connect (buttonbox, signal (rejected (), this, slot (reject ()));
- // Layout listview and buttonbox vertically on the Interface
- Qvboxlayout * mainlayout = new qvboxlayout;
- Mainlayout-> addwidget (listview );
- Mainlayout-> addwidget (buttonbox );
- // Set the current window layout to mainlayout
- Setlayout (mainlayout );
- Setwindowtitle (TR ("team leaders "));
- }
- // Obtain the content in the current model
- Qstringlist teamleadersdialog: Leaders () const
- {
- Return Model-> stringlist ();
- }
- Void teamleadersdialog: insert ()
- {
- // Obtain the number of rows of the current item from the List View
- IntRow = listview-> currentindex (). Row ();
- // Insert a new row into the model and the model automatically updates the list view.
- Model-> insertrows (row, 1 );
- // Obtain the "model index" of the current row in the model"
- Qmodelindex Index = model-> index (ROW );
- // Set the current index of the inserted blank behavior List View
- Listview-> setcurrentindex (INDEX );
- // Set the List View to edit in the current row
- Listview-> edit (INDEX );
- }
- Void teamleadersdialog: del ()
- {
- // Delete a row of model data from the current row and automatically update the list view.
- Model-> removerows (listview-> currentindex (). Row (), 1 );
- }