Study, it is said that Qtreewidget simple and easy to use, do not need to set the model and view alone, not delegate. Signal and slots should work correctly (not verified, supplemented later).
#include<QtGui/QApplication>#include<QTreeWidget>#include<QDebug>//define struct body, including only four pointerstypedefstruct { Char* sect_id, * title, *Page_num; Char*parent_id; } Sectioninfo; //Note 1, depending on the content (the last item) to set up a subordinate relationship//Note 2, they're not in order.Sectioninfo directory[] = { {"Chapter 1","The Prehistory of C + +"," +",""}, {"Chapter 2","C with Classes"," -",""}, {"Section 1.1","Simula and distributed Systems"," +","Chapter 1"}, {"Section 1.2","C and Systems programming"," A","Chapter 1"}, {"Section 2.4","run-time Efficiency"," +","Chapter 2"}, {"Section 2.4.1","inlining"," -","Section 2.4"}, }; intMainintargcChar*argv[]) {qapplication app (argc, argv); Qtreewidget* Treewidget =NewQtreewidget (); //set the number of columns (bare simple rules of how many columns of the situation is rare AH)Treewidget->setcolumncount (3); //set up the table headerqstringlist headers; Headers<<" Section number"<<"Title"<<"Page Number"; Treewidget-setheaderlabels (headers); //The loop executes six times altogether for(intI=0; i<sizeof(directory)/sizeof(directory[0]); i++) {Sectioninfo info= Directory[i];//Assigning a value to a C + + struct is quite ingenious.Qtreewidgetitem * item=NULL; if(strcmp (info.parent_id,"")==0) {Item=NewQtreewidgetitem (Treewidget);//set a line attached to the whole}Else{QString parent_id (info.parent_id);//gets the text of the current row parent node used to compareQtreewidgetitemiterator it (treewidget);//Gets the iteration subkey of the whole treewidget, note that it is a line iteration//until i=4, will not enter the while loop, 0 and 1 will not enter else,2 and 3 The first comparison will not be established, so will not enter while while((*it)->text (0) = parent_id) {//text (0) is a function of Qtreewidgetitem, that is, the text of column 0thQdebug () << i <<" "<< (*it)->text (0); ++it; } Item=NewQtreewidgetitem (*it);//Turn the last line of empty iterations into a row and set the found node as the parent node good } //with the item and can set the content if(item) {Item->settext (0, info.sect_id); Item->settext (1, Info.title); Item->settext (2, Info.page_num); }} treewidget->resize ( -, $); Treewidget-Show (); returnapp.exec (); }
Reference:
Http://book.51cto.com/art/201207/347905.htm
An example of Qtreewidget is that the C + + syntax and the item iteration loops are both ingenious