Control qtreeview implementation

Source: Internet
Author: User
Tags gparted

Method 1: qdirmodel is used for directory. qdirmodel is a model provided by QT for Tree display of file directories. The specific usage 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. You only need to call setmodel () and pass the directory model as a parameter.
// Setrootindex () tells views which directory information is displayed. A model index is provided and
// Use the model index to retrieve data from the model.
// The index () function is unique to qdirmodel. You can use a directory as a parameter to obtain the required model index.
// Other code is displayed only in the window. It is enough to enter the program's event loop.
Qtextcodec: setcodecfortr (qtextcodec: codecforname ("GBK"); // Chinese display
Qtextcodec * codec = qtextcodec: codecforlocale ();
Qstring A = codec-> tounicode ("directory ");
Splitter-> setwindowtitle ();
Splitter-> show ();
Return app.exe C ();
}
The result is as follows:


Method 2: Set the tree structure.

"Treeview. H" File

# Include <qtgui>
Class Treeview: Public 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 ("th ");
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 (q1_actitemview: noedittriggers );
View. header ()-> setresizemode (qheaderview: resizetocontents );
View. Resize (300,280 );
View. iterateoveritems ();
View. setwindowtitle (qobject: TR ("Linux Service Management "));
View. Show ();
Return app.exe C ();

}

Display:

The above are the two models of Treeview, with O (operator _ operator) O ~

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.