Introduction
Why not through this article, directly play qtreewidget stump form it. Brief Introduction
Operating system: WINDOW7 x64
Programming IDE:QT Creator 4.2.1
QT Version: 5.8.0
Last modified: May 3, 2017
All rights reserved:_oe_, reprint please specify the Source: Http://blog.csdn.net/csnd_ayo
Introduction to the introduction of the tree list of the creation of the basic tree list to find a tree-like look into a multi-column tree diagram a simple custom list header operation it's too late. Sorry to add logo qtreewidget for subkeys
Base
Here is only responsible for the basic content, the most superficial, most commonly used content. Tree-like list creation
Association
The Association header file for this class, don't think anyone knows, there are people who do not know, I am serious.
#include <QTreeWidget>
List of shirtless
Once we have the header file, we can build a list of what's not in the following sentence, although the list looks ridiculous.
qtreewidget* tree = new Qtreewidget ();
Tree->show ();
Don't look at him now as a sibuxiang, the embryo has come out, the tree-like list is still far away.
Here's 1 is the title of the tree, and we don't have to ignore him for the time being.
Put on a list of clothes
Let's meet the next most important class.
Qtreewidgetitem obviously his name has already explained his role, we want to insert a piece of data through this class.
/**
* @brief: constructor-Generates a subkey
* @param: View associated form, or translation parent node
* @param: Strings column (column) data (later will be detailed)
* @param: Type temporarily ignores
/Qtreewidgetitem (qtreewidget *view,
const qstringlist &strings, int type = type);
Let's build our own list.
qtreewidget* tree = new Qtreewidget ();
Tree->show ();
Add one level node
qtreewidgetitem *items1 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_1"));
Qtreewidgetitem *items2 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_2"));
Qtreewidgetitem *items3 = new Qtreewidgetitem (tree,
qstringlist (QString ("Num_3"));
We have a tree column, a for, we want to have how many lines of line.
At this point, the basis of the list is already complete. deletion of tree-like lists
Savage's Delete
The deletion of the tree list is actually very interesting, a delete is done, although this is not elegant.
The reason is also simple, a delete triggers a destructor.
The reason why I said he was barbaric was that we could not keep the variable items2 item3 .... In the process of complex operation.
So this kind of writing, can only be seen as savage, no brain.
how elegant.
qtreewidget* tree = new Qtreewidget ();
Tree->show ();
Add one level node
qtreewidgetitem *items1 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_1"));
Qtreewidgetitem *items2 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_2"));
Qtreewidgetitem *items3 = new Qtreewidgetitem (tree,
qstringlist (QString ("Num_3"));
Tree->taketoplevelitem (1);
Hey, here's the function name Taketoplevelitem has explained his role to take away the top items.
Index is subscript, we give 1 here you can think about, which child here will disappear.
Most commonly used deletions
We generally delete an item, are specified by the user through the mouse, so we can actually through the corresponding specific events (example: Right-click menu, Delete KeyDown).
This function obtains the currently selected item
qtreewidget* tree = new Qtreewidget ();
Tree->currentitem ();
If CurrentItem returns empty, it means that the subkeys in the list are not selected. finding tree-like graphs
The crowd searched him for thousands of years. He just had a twitch.
qtreewidget* tree = new Qtreewidget ();
Tree->show ();
Add one level node
qtreewidgetitem *items1 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_1"));
Qtreewidgetitem *items2 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_1"));
Qtreewidgetitem *items3 = new Qtreewidgetitem (tree,
qstringlist (QString ("Num_3"));
Finds items that are exactly the same as the num_1 and returns.
qlist<qtreewidgetitem*> s = Tree->finditems ("Num_1", qt::matchflag::matchexactly);
ruthlessly Delete for
(auto item:s) {
delete item;
}
Deep
Multi-level tree chart
The multi-level tree chart, in fact also very good understanding, is to replace the parent node, who is the parent node, then the item is mounted under WHO.
Qtreewidgetitem *items3 = new Qtreewidgetitem (tree,
qstringlist (QString ("num_13"));
Qtreewidgetitem *item104 = new Qtreewidgetitem (ITEMS3, Qstringlist (QString
("item104"));
Levels are based on this routine, multistage tree diagram arises spontaneously. Multi-column tree graph
No information temporarily
Simple Customization
When using this function, you need to define a variable in the global or class
Qmap<qstring, Qtreewidgetitem*> ITEMMAP_, records unique key values for all tree columns, remembering to be unique.
/**
* @brief: Adding subkeys to the tree
* @param: Item's content
* @param: Parentkey Unique identification of his parent node
* @param: Thekey His node unique identification
*/
int Customtreewidget::additem (qstringlist item, QString Parentkey, QString thekey) {
qtreewidgetitem* Temp_item;
Generated to the parent node
qmap<qstring,qtreewidgetitem*>::iterator iter_item = Itemmap_.find (parentkey);
if (Iter_item = = Itemmap_.end ()) {
Temp_item = new Qtreewidgetitem (this, item);
}
else {
Temp_item = new Qtreewidgetitem (Iter_item.value (), item);
}
Determine
if (!thekey.isempty ()) {
Itemmap_.insert (thekey,temp_item);
}
}
Here is simply to give you a train of thought, we boldly to inherit Qtreewidget class, play the imagination read Dry bar.
For example, by a value to get this item, combined with the foundation above, can do a lot of things, right? Action table header action for list header
/**
* @brief: Table header hidden
* @param: Hide [true: Hide header false: Show header]/
void Qtreeview::setheaderhidde N (bool hide);
To set the table header
/**
* @brief: Set Header content
* @param: Item specific item item
/void Qtreeview::setheaderitem (Qtreewidgetitem * Item);
You can also directly give the literal
void setheaderlabels (const qstringlist &labels);
inline void Setheaderlabel (const QString &label);
the color of the speech
Qstringlist head;
Head << "name";
qtreewidget* tree = new Qtreewidget ();
Tree->show ();
Set Header
Tree->setheaderlabels (head);
Add one level node
head.clear ();
Head << "Wang Foundation";
Qtreewidgetitem *items1 = new Qtreewidgetitem (tree, head
);
Head.clear ();
Head << "Li Hammer";
Qtreewidgetitem *items3 = new Qtreewidgetitem (tree, head
);
Sets the background color
items3->setbackground (0,qbrush (Qt::green));
Head.clear ();
Head << "Li Siki (not Biological)";
Qtreewidgetitem *item104 = new Qtreewidgetitem (ITEMS3, head
);
It 's too late, there's no update here, sorry.
Add a logo to a subkey
No introduction
Qtreewidget's new clothes.
No introduction