(2) example of using the predefined model qstringlistmodel

Source: Internet
Author: User

 

Example of qstringlistmodel

 

The source code is as follows:

Main. cpp

 

CPP Code
 
  1. # Include <qapplication>
  2. # Include "teamleadersdialog. H"
  3. IntMain (IntArgc,Char* Argv [])
  4. {
  5. Qapplication app (argc, argv );
  6. // String Array
  7. Qstringlist leaders;
  8. Leaders <"stooge viller" <"littleface" <"B-B eyes"
  9. <"Pruneface" <"mrs. Pruneface" <"the brow"
  10. <"Vitamin Flintheart" <"flattop Sr." <"Shakey"
  11. <"Breathless Mahoney" <"Mumbles" <"shoulders"
  12. <"Sketch Paree ";
  13. // Dialog box
  14. Teamleadersdialog Dialog (leaders );
  15. Dialog. Show ();
  16. Return app.exe C ();
  17. }
# 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
 
  1. # Ifndef teamleadersdialog_h
  2. # Define teamleadersdialog_h
  3. # Include <qdialog>
  4. Class qdialogbuttonbox;
  5. Class qlistview;
  6. Class qstringlistmodel;
  7. Class teamleadersdialog: Public qdialog
  8. {
  9. Q_object
  10. Public:
  11. // Constructor
  12. Teamleadersdialog (const qstringlist & leaders, qwidget * parent = 0 );
  13. Qstringlist leaders () const;
  14. Private slots:
  15. Void insert ();
  16. Void del ();
  17. PRIVATE:
  18. Qlistview * listview;
  19. Qdialogbuttonbox * buttonbox;
  20. Qstringlistmodel * model;
  21. };
  22. # 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
 
  1. # Include <qtgui>
  2. # Include "teamleadersdialog. H"
  3. Teamleadersdialog: teamleadersdialog (const qstringlist & leaders,
  4. Qwidget * parent)
  5. : Qdialog (parent)
  6. {
  7. // Create and assemble a qstringlistmodel
  8. Model = new qstringlistmodel (this );
  9. Model-> setstringlist (leaders );
  10. // Create a qlistview
  11. Listview = new qlistview;
  12. // Set the Model
  13. Listview-> setmodel (model );
  14. // Set the qlistview editing trigger: Enter the string editing status by entering or double-clicking
  15. Listview-> setedittriggers (q1_actitemview: anykeypressed
  16. | Qabstractitemview: doubleclicked );
  17. //
  18. Buttonbox = new qdialogbuttonbox ();
  19. Qpushbutton * insertbutton = buttonbox-> addbutton (TR ("& Insert "),
  20. Qdialogbuttonbox: actionrole );
  21. Qpushbutton * deletebutton = buttonbox-> addbutton (TR ("& Delete "),
  22. Qdialogbuttonbox: actionrole );
  23. Buttonbox-> addbutton (qdialogbuttonbox: OK );
  24. Buttonbox-> addbutton (qdialogbuttonbox: Cancel );
  25. // Signal slot binding insert and delete buttons
  26. Connect (insertbutton, signal (clicked (), this, slot (insert ()));
  27. Connect (deletebutton, signal (clicked (), this, slot (DEL ()));
  28. // The OK and cancel events in the snap box
  29. Connect (buttonbox, signal (accepted (), this, slot (accept ()));
  30. Connect (buttonbox, signal (rejected (), this, slot (reject ()));
  31. // Layout listview and buttonbox vertically on the Interface
  32. Qvboxlayout * mainlayout = new qvboxlayout;
  33. Mainlayout-> addwidget (listview );
  34. Mainlayout-> addwidget (buttonbox );
  35. // Set the current window layout to mainlayout
  36. Setlayout (mainlayout );
  37. Setwindowtitle (TR ("team leaders "));
  38. }
  39. // Obtain the content in the current model
  40. Qstringlist teamleadersdialog: Leaders () const
  41. {
  42. Return Model-> stringlist ();
  43. }
  44. Void teamleadersdialog: insert ()
  45. {
  46. // Obtain the number of rows of the current item from the List View
  47. IntRow = listview-> currentindex (). Row ();
  48. // Insert a new row into the model and the model automatically updates the list view.
  49. Model-> insertrows (row, 1 );
  50. // Obtain the "model index" of the current row in the model"
  51. Qmodelindex Index = model-> index (ROW );
  52. // Set the current index of the inserted blank behavior List View
  53. Listview-> setcurrentindex (INDEX );
  54. // Set the List View to edit in the current row
  55. Listview-> edit (INDEX );
  56. }
  57. Void teamleadersdialog: del ()
  58. {
  59. // Delete a row of model data from the current row and automatically update the list view.
  60. Model-> removerows (listview-> currentindex (). Row (), 1 );
  61. }

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.