dirview-example.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 739 行 · 第 1/2 页
HTML
739 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/dirview/dirview.doc:5 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>A Directory Browser</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: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A Directory Browser</h1> <p> This example program demonstrates how to use a listview andlistview items to build a multi-column hierarchical, memory- andCPU-efficient directory browser. It also demonstrates how to useDrag&Drop in a listview.<p> <hr><p> Header file:<p> <pre>/****************************************************************************** $Id: qt/dirview.h 3.0.5 edited Oct 12 2001 $**** 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 : public <a href="qlistviewitem.html">QListViewItem</a>{public: FileItem( <a href="qlistviewitem.html">QListViewItem</a> *parent, const <a href="qstring.html">QString</a> &s1, const <a href="qstring.html">QString</a> &s2 ) : <a href="qlistviewitem.html">QListViewItem</a>( parent, s1, s2 ), pix( 0 ) {} const <a href="qpixmap.html">QPixmap</a> *pixmap( int i ) const; void setPixmap( <a href="qpixmap.html">QPixmap</a> *p );private: <a href="qpixmap.html">QPixmap</a> *pix;};class Directory : public <a href="qlistviewitem.html">QListViewItem</a>{public: Directory( <a href="qlistview.html">QListView</a> * parent, const <a href="qstring.html">QString</a>& filename ); Directory( Directory * parent, const <a href="qstring.html">QString</a>& filename, const <a href="qstring.html">QString</a> &col2 ) : <a href="qlistviewitem.html">QListViewItem</a>( parent, filename, col2 ), pix( 0 ) {} Directory( Directory * parent, const <a href="qstring.html">QString</a>& filename ); <a href="qstring.html">QString</a> text( int column ) const; <a href="qstring.html">QString</a> fullName(); void setOpen( bool ); void setup(); const <a href="qpixmap.html">QPixmap</a> *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 <a href="qlistview.html">QListView</a>{ <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public: 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 <a href="qstring.html">QString</a> & );signals: void folderSelected( const <a href="qstring.html">QString</a> & );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(QListViewItem* 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><p> <hr><p> And here is the main implementation file. Note the way the programscans subdirectories only when it has to. This allows the programto handle very large file systems efficiently. The same techniquecan be used in any other trees.<p> <pre>/****************************************************************************** $Id: qt/dirview.cpp 3.0.5 edited Oct 12 2001 $**** 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#......", ".......##......."};QPixmap *folderLocked = 0;QPixmap *folderClosed = 0;QPixmap *folderOpen = 0;QPixmap *fileNormal = 0;/***************************************************************************** * * Class Directory * *****************************************************************************/<a name="f482"></a>Directory::Directory( Directory * parent, const <a href="qstring.html">QString</a>& filename ) : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename), showDirsOnly( parent->showDirsOnly ), pix( 0 ){ p = parent; readable = QDir( fullName() ).isReadable(); if ( !readable ) <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderLocked ); else <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderClosed );}Directory::Directory( <a href="qlistview.html">QListView</a> * parent, const <a href="qstring.html">QString</a>& filename ) : <a href="qlistviewitem.html">QListViewItem</a>( parent ), f(filename), showDirsOnly( ( (DirectoryView*)parent )->showDirsOnly() ), pix( 0 ){ p = 0; readable = QDir( fullName() ).isReadable();}<a name="x1731"></a>void Directory::<a href="qlistviewitem.html#setPixmap">setPixmap</a>( <a href="qpixmap.html">QPixmap</a> *px ){ pix = px; <a href="qlistviewitem.html#setup">setup</a>(); <a href="qlistviewitem.html#widthChanged">widthChanged</a>( 0 ); <a href="qlistviewitem.html#invalidateHeight">invalidateHeight</a>(); <a href="qlistviewitem.html#repaint">repaint</a>();}<a name="x1729"></a>const QPixmap *Directory::<a href="qlistviewitem.html#pixmap">pixmap</a>( int i ) const{ if ( i ) return 0; return pix;}<a name="x1730"></a>void Directory::<a href="qlistviewitem.html#setOpen">setOpen</a>( bool o ){ if ( o ) <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderOpen ); else <a href="qlistviewitem.html#setPixmap">setPixmap</a>( folderClosed ); if ( o && !childCount() ) { <a href="qstring.html">QString</a> s( fullName() ); <a href="qdir.html">QDir</a> thisDir( s );<a name="x1707"></a> if ( !thisDir.<a href="qdir.html#isReadable">isReadable</a>() ) { readable = FALSE; <a href="qlistviewitem.html#setExpandable">setExpandable</a>( FALSE ); return; } <a href="qlistviewitem.html#listView">listView</a>()->setUpdatesEnabled( FALSE );<a name="x1706"></a> const QFileInfoList * files = thisDir.<a href="qdir.html#entryInfoList">entryInfoList</a>(); if ( files ) { QFileInfoListIterator it( *files ); <a href="qfileinfo.html">QFileInfo</a> * fi;<a name="x1734"></a> while( (fi=it.<a href="qlistviewitemiterator.html#current">current</a>()) != 0 ) { ++it;<a name="x1715"></a> if ( fi-><a href="qfileinfo.html#fileName">fileName</a>() == "." || fi-><a href="qfileinfo.html#fileName">fileName</a>() == ".." ) ; // nothing
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?