A Directory BrowserThis 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&Drop in a listview.
Header file:
/**************************************************************************** ** $Id: qt/dirview.h 3.0.6 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 <qlistview.h> #include <qstring.h> #include <qfile.h> #include <qfileinfo.h> #include <qtimer.h> class QWidget; class QDragEnterEvent; class QDragMoveEvent; class QDragLeaveEvent; class QDropEvent; class FileItem : public QListViewItem { public: FileItem( QListViewItem *parent, const QString &s1, const QString &s2 ) : QListViewItem( parent, s1, s2 ), pix( 0 ) {} const QPixmap *pixmap( int i ) const; void setPixmap( QPixmap *p ); private: QPixmap *pix; }; class Directory : public QListViewItem { public: Directory( QListView * parent, const QString& filename ); Directory( Directory * parent, const QString& filename, const QString &col2 ) : QListViewItem( parent, filename, col2 ), pix( 0 ) {} Directory( Directory * parent, const QString& filename ); QString text( int column ) const; QString fullName(); void setOpen( bool ); void setup(); const QPixmap *pixmap( int i ) const; void setPixmap( QPixmap *p ); private: QFile f; Directory * p; bool readable; bool showDirsOnly; QPixmap *pix; }; class DirectoryView : public QListView { Q_OBJECT public: DirectoryView( QWidget *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( QListViewItem * ); void openFolder(); protected: void contentsDragEnterEvent( QDragEnterEvent *e ); void contentsDragMoveEvent( QDragMoveEvent *e ); void contentsDragLeaveEvent( QDragLeaveEvent *e ); void contentsDropEvent( QDropEvent *e ); void contentsMouseMoveEvent( QMouseEvent *e ); void contentsMousePressEvent( QMouseEvent *e ); void contentsMouseReleaseEvent( QMouseEvent *e ); private: QString fullPath(QListViewItem* item); bool dirsOnly; QListViewItem *oldCurrent; QListViewItem *dropItem; QTimer* autoopen_timer; QPoint presspos; bool mousePressed; }; #endif
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.
/**************************************************************************** ** $Id: qt/dirview.cpp 3.0.6 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 <qdir.h> #include <qfile.h> #include <qfileinfo.h> #include <qpixmap.h> #include <qevent.h> #include <qpoint.h> #include <qmessagebox.h> #include <qdragobject.h> #include <qmime.h> #include <qstrlist.h> #include <qstringlist.h> #include <qapplication.h> #include <qheader.h> 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 * *****************************************************************************/
Main:
/**************************************************************************** ** $Id: qt/main.cpp 3.0.6 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 <qapplication.h> #include <qfileinfo.h> #include <qdir.h> #include "dirview.h" int main( int argc, char ** argv ) { QApplication a( argc, argv ); DirectoryView mw; See also Examples. |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.0 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com