Let's take a look at a very useful model: qdirmodel. This model allows us to display the directory structure of the operating system in the view. This time, let's take a look at the running results: Is this interface very familiar? However, this is not opened by qfiledialog, which is implemented by ourselves. Qdirmodel and qtreeview are supported. Let's take a look at the code. Mytreeview. h # ifndef mylistview_h
# Define mylistview_h
# Include <qtgui>
Class mytreeview: Public qwidget
{
Q_object
Public:
Mytreeview ();
PRIVATE:
Qdirmodel * model;
Qtreeview * Treeview;
Private slots:
Void mkdir ();
Void RM ();
};
# Endif // mylistview_h mytreeview. cpp # include "mylistview. H"
Mytreeview: mytreeview ()
{
Model = new qdirmodel;
Model-> setreadonly (false );
Model-> setsorting (qdir: dirsfirst | qdir: ignorecase | qdir: Name );
Connect (createbtn, signal (clicked (), this, slot (mkdir ()));
Connect (delbtn, signal (clicked (), this, slot (RM ()));
}
Void mytreeview: mkdir ()
{
Qmodelindex Index = Treeview-> currentindex ();
If (! Index. isvalid ()){
Return;
}
Qstring dirname = qinputdialog: gettext (this,
TR ("create directory "),
TR ("directory name "));
If (! Dirname. isempty ()){
If (! Model-> mkdir (index, dirname). isvalid ()){
Qmessagebox: Information (this,
TR ("create directory "),
TR ("failed to create the directory "));
}
}
}
Void mytreeview: Rm ()
{
Qmodelindex Index = Treeview-> currentindex ();
If (! Index. isvalid ()){
Return;
}
Bool OK;
If (model-> fileinfo (INDEX). isdir ()){
OK = model-> rmdir (INDEX );
} Else {
OK = model-> remove (INDEX );
}
If (! OK ){
Qmessagebox: Information (this,
TR ("Remove "),
TR ("failed to remove % 1"). Arg (model-> filename (INDEX )));
}
} In the constructor, we first created an object of qdirmodel and set readonly to false, which means we can modify it. The next setsorting () function sorts the data based on the clear sorting rules: folder priority (qdir: dirsfirst), Case Insensitive (qdir: ignorecase ), in addition, it is sorted by name (qdir: name ). For more rule combinations, see the API documentation. Then we create a qtreeview instance and set the model to the qdirmodel instance we just created. Then we set the attributes of qtreeview. First, set the stretchlastsection to true. If this attribute is set to true, the width of the last column is automatically expanded to fill the final boundary when the qtreeview width is greater than the width of all columns; otherwise, the width of the last column is kept original. The second setsortindicator () function sets which column to sort. Since we set the model to sort by name, the first parameter passed here is 0, which is the 1st column. The setsortindicatorshown () function sets to display the small arrow of the sorting on the column. Setclickable (true) allows you to click the column header. In this way, our qtreeview settings are complete. Finally, we use qdir: currentpath () to obtain the path for running the current EXE file, and use this path as the path displayed when the program starts. The expand () function is used to expand this path. The scrollto () function is used to scroll the view to the position of this path. The resizecolumntocontents () function is used to adapt the column header to the width of the content, that is, do not generate... symbol. In this way, we have set qtreeview through a series of parameters so that it can display the directory structure for us. As for the two slots that follow, they cannot be understood. The first mkdir () function is to create a folder. Void mytreeview: mkdir ()
{
Qmodelindex Index = Treeview-> currentindex ();
If (! Index. isvalid ()){
Return;
}
Qstring dirname = qinputdialog: gettext (this,
TR ("create directory "),
TR ("directory name "));
If (! Dirname. isempty ()){
If (! Model-> mkdir (index, dirname). isvalid ()){
Qmessagebox: Information (this,
TR ("create directory "),
TR ("failed to create the directory "));
}
}
} As shown in its code, first obtain the selected directory. The following isvalid () judgment is very important, because no directory is selected by default, this path is illegal at this time, in order to avoid program exceptions, this step must be taken. A dialog box is displayed asking about the new folder name. If the folder name fails to be created, a prompt is displayed. Otherwise, the folder is created successfully. At this time, you can look at the actual location on the hard disk! The code for deleting a directory is similar to void mytreeview: Rm ()
{
Qmodelindex Index = Treeview-> currentindex ();
If (! Index. isvalid ()){
Return;
}
Bool OK;
If (model-> fileinfo (INDEX). isdir ()){
OK = model-> rmdir (INDEX );
} Else {
OK = model-> remove (INDEX );
}
If (! OK ){
Qmessagebox: Information (this,
TR ("Remove "),
TR ("failed to remove % 1"). Arg (model-> filename (INDEX )));
}
} Check whether the path is valid. Note that deleting directories and files is not a function and you need to call the isdir () function for detection. This step is clearly described in the Code and will not be repeated here. Note that qdirmodel is not recommended in the latest QT version. This document uses qfilesystemmodel instead. Since the functions are almost the same, the Code is not modified. Unlike qdirmodel, qfilesystemmodel starts its own thread to scan folders. Therefore, it does not block the main thread caused by scanning folders. In addition, both qdirmodel and qfilesystemmodel cache the model results. If you want to refresh the results immediately, the former provides the refresh () function, and the latter notifies the qfilesystemwatcher class.
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.