QT Tree Control Qtreeview

Source: Internet
Author: User
Tags gparted

Look at the example below and you'll see.

Method One: Use Qdirmodel to carry on the reality of the catalogue, Qdirmodel is a model that the QT Secondary school provides for the file directory tree type display. 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 model, simply call Setmodel (), and pass the catalog model as a parameter
//setrootindex () tell the views which directory information to display, which needs to provide a model Index, and then use this
//model index to go to model to get Data
//index () This function is Qdirmodel specific, by using a directory as a parameter, get the required model index
// The other code is just a window show, the event loop into the program is good
qtextcodec::setcodecfortr (Qtextcodec::codecforname ("GBK")); Qtextcodec *codec = Qtextcodec::codecforlocale ();
QString a = codec->tounicode ("Table of Contents");
Splitter->setwindowtitle (a);
Splitter->show ();
return app.exec ();

The results shown are as follows:

Method Two: Set the tree structure by oneself.

"Treeview.h" file

#include <QtGui>
class treeview:p ublic qtreeview
TreeView ();
void Iterateoveritems ();
Qlist<qstandarditem*> Returntheitems ();
void Mousedoubleclickevent (Qmouseevent *event);
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 ();


That's two of the TreeView's model.

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.