📄 dirview-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - dirview/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>A Directory Browser</h1><br clear="all"> This example program demonstrates how to use a ListView and ListView Items to build a multi-column hierarchical, memory- and CPU-efficient directory browser. It also demonstrates how to use Drag'n'Drop in a Listview. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/dirview/dirview.h 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef DIRVIEW_H#define DIRVIEW_H#include <<a href="qlistview-h.html">qlistview.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>#include <<a href="qfile-h.html">qfile.h</a>>#include <<a href="qfileinfo-h.html">qfileinfo.h</a>>#include <<a href="qtimer-h.html">qtimer.h</a>>class QWidget;class QDragEnterEvent;class QDragMoveEvent;class QDragLeaveEvent;class QDropEvent;class FileItem : <a href="qlistviewitem.html">QListViewItem</a>{public: FileItem( <a href="qlistviewitem.html">QListViewItem</a> *parent, const QString &s1, const QString &s2 ) : <a href="qlistviewitem.html">QListViewItem</a>( parent, s1, s2 ), pix( 0 ) {} const QPixmap *pixmap( int i ) const; void setPixmap( <a href="qpixmap.html">QPixmap</a> *p );private: <a href="qpixmap.html">QPixmap</a> *pix;};class Directory : public QListViewItem{public: Directory( <a href="qlistview.html">QListView</a> * parent, const QString& filename ); Directory( Directory * parent, const QString& filename, const QString &col2 ) : <a href="qlistviewitem.html">QListViewItem</a>( parent, filename, col2 ), pix( 0 ) {} Directory( Directory * parent, const QString& filename ); <a href="qstring.html">QString</a> text( int column ) const; <a href="qstring.html">QString</a> fullName(); void setOpen( bool ); void setup(); const QPixmap *pixmap( int i ) const; void setPixmap( <a href="qpixmap.html">QPixmap</a> *p );private: <a href="qfile.html">QFile</a> f; Directory * p; bool readable; bool showDirsOnly; <a href="qpixmap.html">QPixmap</a> *pix;};class DirectoryView : public QListView{ Q_OBJECTpublic: DirectoryView( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0, bool sdo = FALSE ); bool showDirsOnly() { return dirsOnly; }public slots: void setDir( const QString & );signals: void folderSelected( const QString & );protected slots: void slotFolderSelected( <a href="qlistviewitem.html">QListViewItem</a> * ); void openFolder();protected: void contentsDragEnterEvent( <a href="qdragenterevent.html">QDragEnterEvent</a> *e ); void contentsDragMoveEvent( <a href="qdragmoveevent.html">QDragMoveEvent</a> *e ); void contentsDragLeaveEvent( <a href="qdragleaveevent.html">QDragLeaveEvent</a> *e ); void contentsDropEvent( <a href="qdropevent.html">QDropEvent</a> *e ); void contentsMouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ); void contentsMousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e ); void contentsMouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );private: <a href="qstring.html">QString</a> fullPath(<a href="qlistviewitem.html">QListViewItem</a>* item); bool dirsOnly; <a href="qlistviewitem.html">QListViewItem</a> *oldCurrent; <a href="qlistviewitem.html">QListViewItem</a> *dropItem; <a href="qtimer.html">QTimer</a>* autoopen_timer; <a href="qpoint.html">QPoint</a> presspos; bool mousePressed;};#endif</pre> <hr> And here is the main implementation file. Note the way the program scans subdirectories only when it has to. This allows the program to handle very large file systems efficiently. The same technique can be used in any other trees. <pre>/****************************************************************************** $Id: qt/examples/dirview/dirview.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "dirview.h"#include <<a href="qdir-h.html">qdir.h</a>>#include <<a href="qfile-h.html">qfile.h</a>>#include <<a href="qfileinfo-h.html">qfileinfo.h</a>>#include <<a href="qpixmap-h.html">qpixmap.h</a>>#include <<a href="qevent-h.html">qevent.h</a>>#include <<a href="qpoint-h.html">qpoint.h</a>>#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a href="qdragobject-h.html">qdragobject.h</a>>#include <<a href="qmime-h.html">qmime.h</a>>#include <<a href="qstrlist-h.html">qstrlist.h</a>>#include <<a href="qstringlist-h.html">qstringlist.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>#include <<a href="qheader-h.html">qheader.h</a>>static const char* folder_closed_xpm[]={ "16 16 9 1", "g c #808080", "b c #c0c000", "e c #c0c0c0", "# c #000000", "c c #ffff00", ". c None", "a c #585858", "f c #a0a0a4", "d c #ffffff", "..###...........", ".#abc##.........", ".#daabc#####....", ".#ddeaabbccc#...", ".#dedeeabbbba...", ".#edeeeeaaaab#..", ".#deeeeeeefe#ba.", ".#eeeeeeefef#ba.", ".#eeeeeefeff#ba.", ".#eeeeefefff#ba.", ".##geefeffff#ba.", "...##gefffff#ba.", ".....##fffff#ba.", ".......##fff#b##", ".........##f#b##", "...........####."};static const char* folder_open_xpm[]={ "16 16 11 1", "# c #000000", "g c #c0c0c0", "e c #303030", "a c #ffa858", "b c #808080", "d c #a0a0a4", "f c #585858", "c c #ffdca8", "h c #dcdcdc", "i c #ffffff", ". c None", "....###.........", "....#ab##.......", "....#acab####...", "###.#acccccca#..", "#ddefaaaccccca#.", "#bdddbaaaacccab#", ".eddddbbaaaacab#", ".#bddggdbbaaaab#", "..edgdggggbbaab#", "..#bgggghghdaab#", "...ebhggghicfab#", "....#edhhiiidab#", "......#egiiicfb#", "........#egiibb#", "..........#egib#", "............#ee#"};static const char * folder_locked[]={ "16 16 10 1", "h c #808080", "b c #ffa858", "f c #c0c0c0", "e c #c05800", "# c #000000", "c c #ffdca8", ". c None", "a c #585858", "g c #a0a0a4", "d c #ffffff", "..#a#...........", ".#abc####.......", ".#daa#eee#......", ".#ddf#e##b#.....", ".#dfd#e#bcb##...", ".#fdccc#daaab#..", ".#dfbbbccgfg#ba.", ".#ffb#ebbfgg#ba.", ".#ffbbe#bggg#ba.", ".#fffbbebggg#ba.", ".##hf#ebbggg#ba.", "...###e#gggg#ba.", ".....#e#gggg#ba.", "......###ggg#b##", ".........##g#b##", "...........####."};static const char * pix_file []={ "16 16 7 1", "# c #000000", "b c #ffffff", "e c #000000", "d c #404000", "c c #c0c000", "a c #ffffc0", ". c None", "................", ".........#......", "......#.#a##....", ".....#b#bbba##..", "....#b#bbbabbb#.", "...#b#bba##bb#..", "..#b#abb#bb##...", ".#a#aab#bbbab##.", "#a#aaa#bcbbbbbb#", "#ccdc#bcbbcbbb#.", ".##c#bcbbcabb#..", "...#acbacbbbe...", "..#aaaacaba#....", "...##aaaaa#.....", ".....##aa#......", ".......##......."};<a href="qpixmap.html">QPixmap</a> *folderLocked = 0;<a href="qpixmap.html">QPixmap</a> *folderClosed = 0;<a href="qpixmap.html">QPixmap</a> *folderOpen = 0;<a href="qpixmap.html">QPixmap</a> *fileNormal = 0;/***************************************************************************** * * Class Directory * *****************************************************************************/Directory::Directory( Directory * parent, const QString& filename ) : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename), showDirsOnly( parent->showDirsOnly ), pix( 0 ){ p = parent; readable = QDir( fullName() ).isReadable(); if ( !readable ) setPixmap( folderLocked ); else setPixmap( folderClosed );}Directory::Directory( <a href="qlistview.html">QListView</a> * parent, const QString& filename ) : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename), showDirsOnly( ( (DirectoryView*)parent )->showDirsOnly() ), pix( 0 ){ p = 0; readable = QDir( fullName() ).isReadable();}void <a name="462"></a>Directory::setPixmap( <a href="qpixmap.html">QPixmap</a> *px ){ pix = px; <a href=#465>setup</a>(); <a href="qlistviewitem.html#9a4e7f">widthChanged</a>( 0 ); <a href="qlistviewitem.html#88530f">invalidateHeight</a>(); <a href="qlistviewitem.html#2463fa">repaint</a>();}const QPixmap *<a name="463"></a>Directory::pixmap( int i ) const{ if ( i ) return 0; return pix;}void <a name="464"></a>Directory::setOpen( bool o ){ if ( o ) <a href=#462>setPixmap</a>( folderOpen ); else <a href=#462>setPixmap</a>( folderClosed ); if ( o && !childCount() ) { <a href="qstring.html">QString</a> s( <a href=#466>fullName</a>() ); <a href="qdir.html">QDir</a> thisDir( s ); if ( !thisDir.<a href="qdir.html#6dc6f9">isReadable</a>() ) { readable = FALSE; <a href="qlistviewitem.html#725aac">setExpandable</a>( FALSE ); return; } <a href="qlistviewitem.html#5a9872">listView</a>()->setUpdatesEnabled( FALSE ); const QFileInfoList * files = thisDir.<a href="qdir.html#789222">entryInfoList</a>(); if ( files ) { QFileInfoListIterator it( *files ); <a href="qfileinfo.html">QFileInfo</a> * fi;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -