Http://hi.baidu.com/%B3%BE%C3%CE%C8%F4%D0%E9/blog/item/d162a0828c0b16a80df4d27a.html
Tree control Qtreeview Implementation 2009-11-23 11:50
Method One: The use of Qdirmodel for the reality of the directory, Qdirmodel is the QT Secondary School for the file directory tree display of a model. The specific use is as follows: #include <QtGui> int main (int argc, char *argv[]) { Qapplication app (argc, argv); Qsplitter *splitter = new Qsplitter; Qdirmodel *model = new Qdirmodel; Create data from the default directory Qtreeview *tree = new Qtreeview (splitter); Tree->setmodel (model); Tree->setrootindex (Model->index ("c:\\")); Configure a view to display the data in the model simply by calling Setmodel () and passing the catalog model as a parameter Setrootindex () tells the view which directory information is displayed, which needs to provide a model index, and then use this Model index go to model to get the data Index () This function is unique to Qdirmodel, by taking a directory as a parameter, get the desired model index The other code just window show out and go into the program event loop just fine QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GBK")); Chinese display Qtextcodec *codec = Qtextcodec::codecforlocale (); QString a = Codec->tounicode ("directory"); Splitter->setwindowtitle (a); Splitter->show (); return App.exec (); } The display results are as follows:
Method Two: Set the tree structure by itself. "Treeview.h" file #include <QtGui> Class TreeView:p ublic Qtreeview { Public TreeView (); void Iterateoveritems (); Qlist<qstandarditem*> Returntheitems (); void Mousedoubleclickevent (Qmouseevent *event); Private Qstandarditemmodel *model; }; "Treeview.cpp" file #include "Treeview.h" Treeview::treeview (): Qtreeview () { Qtextcodec::setcodecfortr ( Qtextcodec::codecforname ("GBK")); model = new Qstandarditemmodel (4,2); Model->setheaderdata (0, Qt::horizontal, tr ("Service")); Model->setheaderdata (1, qt::horizontal, tr ("Details")); Qstandarditem *item1 = new Qstandarditem ("Avahi-daemon"); Item1->seticon (Qicon ("gparted.png")); Qstandarditem *item2 = new Qstandarditem ("Bluetooth"); Item2->seticon (Qicon ("gparted.png")); Qstandarditem *item3 = new Qstandarditem ("Crond"); Item3->seticon (Qicon ("gparted.png")); Qstandarditem *item4 = new Qstandarditem (" Cups "); Item4->seticon (Qicon ("gparted.png")); Model->setitem (0, 0, item1); Model->setitem (1, 0, ITEM2); Model->setitem (2, 0, item3); Model->setitem (3, 0, ITEM4); Qstandarditem *ITEM5 = new Qstandarditem ("fifth"); Item4->appendrow (ITEM5); Qmodelindex parent; for (int i = 0; i < 4; ++i) { Parent = Model->index (0, 0, parent); Model->insertrows (0, 1, parent); Model->insertcolumns (0, 1, parent); Qmodelindex index = model->index (0, 0, parent); Model->setdata (index, i); } This->setmodel (model); } Qlist<qstandarditem*> Treeview::returntheitems () { Return Model->finditems ("*", Qt::matchwildcard | qt::matchrecursive); } void Treeview::iterateoveritems () { qlist<qstandarditem*> list = Returntheitems (); foreach (qstandarditem* item, list) { Qdebug () << item->text (); } } void Treeview::mousedoubleclickevent (Qmouseevent *event) { if (Event->button () = = Qt::leftbutton) { Qmodelindex index0 = Currentindex (); Qdebug () << index0.data (). toString (); } } "Main.cpp" file #include <QtGui/QApplication> #include "Treeview.h" int main (int argc, char *argv[]) { Qapplication app (argc, argv); TreeView view; View.setedittriggers (qabstractitemview::noedittriggers); View.header ()->setresizemode (qheaderview::resizetocontents); View.resize (300,280); View.iterateoveritems (); View.setwindowtitle (QOBJECT::TR ("Linux Service Management")); View.show (); return App.exec (); } Show: The above is the two model of the TreeView O (∩_∩) o~ |
qt does a file browser project requires a file browser , requiring the file to be displayed in a list, when clicked on a directory, will go to the lower directory, with Qt implementation.
Method 1: use Qfilesystemmodel or Qdir to do Model,qtableview as the displayed view.
The advantage of this approach is that Qfilesystemmodel and Qdir can automatically read the file information in the current directory. Including file size, type, and so on, as long as the following settings can be displayed in the current directory of the file list.
P_mtvfilelist->setrootindex (Dirmodel->setrootpath (filename)); Qtableview setting the current root directory
To view information about a file or folder in a view, just know the index of the file, as follows
Dirmodell->fileinfo (P_mtvfilelist->currentindex ()); View file information for the current file
But the Qfilesystemmodel and Qdir show are in English. You can change the header to Chinese by rewriting Qfilesystemmodel's headerdata, but the Chinese information about the file information that is displayed in it is not yet known how to become Chinese.
method Two : Use Qlistwidget and Qdir to realize the entryinfolist. Entryinfolist is a powerful feature. File information in the read-out directory that can be filtered
The principle of this approach is to read the list of files with Entryinfolist. Displays the contents of the file list as a Qlistwidgetitem item on the Qlistwidget, remembers the current directory, and, when you click on an item in list, gets the file name by Item->text (). Thus, through the entryinfolist into the next level of the directory, in this way can only display a column
method Three: use Qstandarditemmodel and Qtableview realization, with Qdir entryinfolist get file information, will get to data step by step to fill in the model to display.
When you click on an item, you get the data for a column in this row by Index.sibling (Index.row (), 0). toString () to get the file name
Method Four: Use Dir's entryinfolist to traverse the entire folder, the result of the traversal is placed in a qlist inside, Qlist is the file of some information, as follows
typedef struct DIRNODE {char filename[200]; BOOL Isdir; Long parent; Long child; Long Next; }dirnode, *pdirnode;
String each file folder together into a linked list
The individual codes are as follows:
The Filenode.rar of/files/chenxuelian/with linked list
/files/chenxuelian/qfilesystemmodel implementation of Fileview.rar
/files/chenxuelian/General model and TableView's Fileview.rar
/files/chenxuelian/qlistwidget implementation of Fileview.rar