Qlistview usage Examples

Source: Internet
Author: User
Tags file copy

Model has the following kinds:
Qstringlistmodel Store a set of strings
Qstandarditemmodel store data of any hierarchy
Qdirmodel File System Encapsulation
Qsqlquerymodel SQL query result set encapsulation
Qsqltablemodel to encapsulate a table in SQL
Qsqlrelationaltablemodel The SQL table with foreign key is encapsulated
Qsortfilterproxymodel performs sort and/or filter on another model
Model is stored in each of the data has a corresponding "model index", by the Qmodelindex class to represent.
Each index is made up of three parts: Row,column and a pointer to the model that it belongs to. For the one-dimensional list Model,column part is always 0.
Here are two examples (this example is appropriate to change), introducing examples of Qstringlistmodel,qstandarditemmodel,qdirmodel using Qtreeview and Qlistview
Build a CPP file copy the following code
#include <QApplication>
#include <QWidget>
#include <QTreeView>
#include <QDirModel>
#include <QHBoxLayout>
#include <QSplitter>
#include <QStringListModel>
#include <QListView>
#include <QStandardItemModel>
int main (int argc, char *argv[]) {
/*
Qapplication app (argc, argv);


Here is the simple use of Qstandarditemmodel, just replace the view model for Qstmodel can see the effect
Qstandarditemmodel *qstmodel=new Qstandarditemmodel ();
qstandarditem* item1 = new Qstandarditem ("Item1");
qstandarditem* item2 = new Qstandarditem ("item2");
qstandarditem* item3 = new Qstandarditem ("Item3");
Item1->appendrow (ITEM2);
Qstmodel->appendrow (ITEM1);
Qstmodel->appendrow (ITEM3);
The following is an example of Stringlistmodel, where you can see the effect by simply replacing the view's model mod, Qlistview also applies
Qstringlist names;
names<< "A" << "B";
Qabstractitemmodel *mod=new Qstringlistmodel (names);
Note that the Stringlistmodel is used as a qabstractitemmodel here. So we can
Use the abstract interface in model, and if we replace the current model with another model, the code will work as well.
The list view provided by Qlistview is sufficient to meet the needs of the current model.
Qdirmodel *model=new Qdirmodel;
Qtreeview *view=new Qtreeview;
The model can be set here
View->setmodel (model);
View->setselectionmode (qabstractitemview::singleselection);
View->setautofillbackground (TRUE);
To set whether a column is hidden
View->setcolumnhidden (1,true);
View->setcolumnhidden (2,true);
View->setcolumnhidden (3,true);
Widget->setautofillbackground (TRUE);
Qhboxlayout *blayout=new qhboxlayout;
Blayout->addwidget (view);
Blayout->addstretch ();
Qsplitter *splitter = new Qsplitter;
Splitter->setlayout (blayout);
Splitter->addwidget (view);
Splitter->show ();
Widget->show ();
return App.exec ();
*/

Qapplication app (argc, argv);
Qsplitter *splitter = new Qsplitter;
Create data from the default directory

Qdirmodel *model = new Qdirmodel;

Qtreeview *tree = new Qtreeview (splitter);
Configure a view to display the data in model, simply call Setmodel () and pass the catalog model as a parameter
Tree->setmodel (model);
Setrootindex () tells the views which directory to display information, which needs to provide a model index, and then use this
Model index to the model to get data, where the default is the current path
Tree->setrootindex (Model->index (Qdir::currentpath ()));
Qlistview *list = new Qlistview (splitter);
List->setmodel (model);
Index () This function is Qdirmodel specific, by the use of a directory as a parameter to get the required model index
The other code is just a window show, go to the program's event loop just fine
List->setrootindex (Model->index (Qdir::currentpath ()));

Splitter->setwindowtitle ("Two views onto the same directory model");
Splitter->show ();
return App.exec ();
}

void Auditctl_win::save ()
{
/* if (!listview->firstchild ())
Return

Qfile f (filename);
if (!f.open (io_writeonly))
Return

Qtextstream T (&f);

Qlistviewitemiterator it (ListView);

for (; It.current (); ++it)
for (unsigned int i = 0; i < i++)
T << it.current ()->text (i) << "\ n";

F.close ();
}

void Auditctl_win::load (const QString &filename)
{
Listview->clear ();
Qfile f (filename);
if (!f.open (qiodevice::readonly))
Return
Qtextstream T (&f);
while (!t.atend ())
{
QString string = static_cast<qstring> (T.readline ());
Qstandarditem *item = new Qstandarditem (string);
Standarditemmodel->appendrow (item);
Listview->setmodel (Standarditemmodel);
}

Implement Color Marquee
Standarditemmodel = new Qstandarditemmodel (this);
/*qstringlist strlist;
Strlist.append ("string1");
Strlist.append ("string2");
Strlist.append ("String3");
Strlist.append ("String4");
Strlist.append ("String5");
Strlist.append ("String6");
Strlist.append ("String7");
Strlist << "String8";
Strlist + = "STRING9";
int ncount = Strlist.size ();
for (int i = 0; i < ncount; i++)
{
QString string = static_cast<qstring> (strlist.at (i));
Qstandarditem *item = new Qstandarditem (string);
if (i% 2 = 1)
{
Qlineargradient Lineargrad (qpointf (0, 0), qpointf (200, 200));
Lineargrad.setcolorat (0, Qt::d arkgreen);
Lineargrad.setcolorat (1, Qt::yellow);
Qbrush Brush (Lineargrad);
Item->setbackground (brush);
}
Standarditemmodel->appendrow (item);
}
Listview->setmodel (Standarditemmodel); */
Listview->setfixedsize (200,300); F.close ();
}

Qlistview provides a tree-like display element that displays multiple columns and a tree structure, often used in conjunction with Qlistviewitem or Qchecllistitem, and the simplest way to use Qlistview is to take Qlistview as the root, and use the AddColumn () method to add the bars, and then use Qlistviewitem or Qchecklistitem to specify this qlistview as the parent node, for example:
Qlistview *root;

Root->addcolumn ("Name");
Root->addcolumn ("Size");

Qlistviewitem element = new Qlistviewitem (Root, "FName", "fsize");

Above is a method of adding a qlistviewitem to the Qlistview. We can also specify a qlistviewitem as the parent node, and the child nodes are automatically scaled within the parent node, and there is usually a rule to add to the tree or tree. For example, HTML or XML files, we use loops to add to these items, such as the following fragment adding a new item to the previous element child node as its child section:
for (int i = 0; i < attributes.length (); i++) {
New Qlistviewitem (element, Attributes.qname (i), Attributes.uri (i));
}


The following program we complex the FileInfo and Qdir in front of us, using the recursion to refer to all the records and files in the user's home catalogue, but without the link between the hidden files and the symbols, we will refer to the results as a record and file, and use the Qlistview type of tree structure to display:
#include <qapplication.h> #include <qlistview.h> #include <qdir.h> #include <qfileinfo.h> #

Include <qpixmap.h> void Dirlist (qlistviewitem*, qfileinfo*);

    int main (int argc, char **argv) {qapplication app (argc, argv);
    Qlistview *root = new Qlistview ();
    Root->addcolumn ("Name");

    Root->addcolumn ("Size");
    Qdir d = qdir::home (); D.setfilter (Qdir::files | Qdir::D IRS |
    Qdir::nosymlinks); D.setsorting (qdir::D Irsfirst |

    Qdir::name);
    Const Qfileinfolist *list = D.entryinfolist ();
    Qfileinfolistiterator it (*list);
    Qfileinfo *fi;

    Qlistviewitem *child;
        while (fi = It.current ())!= 0) {if (fi->filename () = = "." | | fi->filename () = = "...");
            else if (Fi->isdir ()) {child = new Qlistviewitem (root, Fi->filename (). Latin1 (), "dir");
            Child->setpixmap (0, Qpixmap ("dirclosed.xpm"));
        Dirlist (Child, FI); else {child = new QLiStviewitem (Root, Fi->filename (). Latin1 (), Qstring::number (Fi->size ()));
        Child->setpixmap (0, Qpixmap ("file.xpm"));
    } ++it;
    } app.setmainwidget (root);

    Root->show ();
return App.exec ();

    } void Dirlist (Qlistviewitem *parent, Qfileinfo *pfi) {Qdir D;
    D.setpath (Pfi->filepath ()); D.setfilter (Qdir::files | Qdir::D IRS |
    Qdir::nosymlinks); D.setsorting (qdir::D Irsfirst |

    Qdir::name);
    Const Qfileinfolist *list = D.entryinfolist ();
    Qfileinfolistiterator it (*list);
    Qfileinfo *fi;

    Qlistviewitem *child;
        while (fi = It.current ())!= 0) {if (fi->filename () = = "." | | fi->filename () = = "...");
            else if (Fi->isdir ()) {child = new Qlistviewitem (parent, Fi->filename (). Latin1 (), "dir");
            Child->setpixmap (0, Qpixmap ("dirclosed.xpm"));
        Dirlist (Child, FI); else {child = new Qlistviewitem (parENT, Fi->filename (), Latin1 (), Qstring::number (Fi->size ()));
        Child->setpixmap (0, Qpixmap ("file.xpm"));
    } ++it; }
}

The use of the Qlistview itself is basically simple, and the program itself is the part of the loop that we're looking at. and "." Without any processing, and if the search is to be recorded, put it into the qlistview tree structure, and then use the name as a basis, and then call Dirlist () again to conduct the search.

For the purpose of distinguishing between records or files, we simply use the two maps to make the difference, and you can also step into the event processing, so that the recorded picture will be opened and closed after it is pressed, so please try it yourself.

The following is the time when the program is executed:



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.