diff options
author | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /doc/html/listviews-example.html | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'doc/html/listviews-example.html')
-rw-r--r-- | doc/html/listviews-example.html | 513 |
1 files changed, 513 insertions, 0 deletions
diff --git a/doc/html/listviews-example.html b/doc/html/listviews-example.html new file mode 100644 index 0000000..872aacd --- /dev/null +++ b/doc/html/listviews-example.html @@ -0,0 +1,513 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> +<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/listviews/listviews.doc:4 --> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> +<title>Listviews</title> +<style type="text/css"><!-- +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>Listviews</h1> + + +<p> +This examples shows how to work with listviews (hierarchical and multi-column). +It also shows how to subclass listview items for special reasons. It looks +and works like the main window of a mail client. +<p> <hr> +<p> Header file: +<p> <pre>/**************************************************************************** +** $Id: qt/listviews.h 3.3.8 edited Jan 11 14:37 $ +** +** Copyright (C) 1992-2007 Trolltech ASA. 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 LISTVIEWS_H +#define LISTVIEWS_H + +#include <<a href="qsplitter-h.html">qsplitter.h</a>> +#include <<a href="qstring-h.html">qstring.h</a>> +#include <<a href="qobject-h.html">qobject.h</a>> +#include <<a href="qdatetime-h.html">qdatetime.h</a>> +#include <<a href="qptrlist-h.html">qptrlist.h</a>> +#include <<a href="qlistview-h.html">qlistview.h</a>> + +class QListView; +class QLabel; +class QPainter; +class QColorGroup; +class QObjectList; +class QPopupMenu; + +// ----------------------------------------------------------------- + +class MessageHeader +{ +public: + MessageHeader( const <a href="qstring.html">QString</a> &_sender, const <a href="qstring.html">QString</a> &_subject, const <a href="qdatetime.html">QDateTime</a> &_datetime ) + : msender( _sender ), msubject( _subject ), mdatetime( _datetime ) + {} + + MessageHeader( const MessageHeader &mh ); + MessageHeader &operator=( const MessageHeader &mh ); + + <a href="qstring.html">QString</a> sender() { return msender; } + <a href="qstring.html">QString</a> subject() { return msubject; } + <a href="qdatetime.html">QDateTime</a> datetime() { return mdatetime; } + +protected: + <a href="qstring.html">QString</a> msender, msubject; + <a href="qdatetime.html">QDateTime</a> mdatetime; + +}; + +// ----------------------------------------------------------------- + +class Message +{ +public: + enum State { Read = 0, + Unread}; + + Message( const MessageHeader &mh, const <a href="qstring.html">QString</a> &_body ) + : mheader( mh ), mbody( _body ), mstate( Unread ) + {} + + Message( const Message &m ) + : mheader( m.mheader ), mbody( m.mbody ), mstate( m.mstate ) + {} + + MessageHeader header() { return mheader; } + <a href="qstring.html">QString</a> body() { return mbody; } + + void setState( const State &s ) { mstate = s; } + State state() { return mstate; } + +protected: + MessageHeader mheader; + <a href="qstring.html">QString</a> mbody; + State mstate; + +}; + +// ----------------------------------------------------------------- + +class Folder : public <a href="qobject.html">QObject</a> +{ + <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> + +public: + Folder( Folder *parent, const <a href="qstring.html">QString</a> &name ); + ~Folder() + {} + + void addMessage( Message *m ) +<a name="x131"></a> { lstMessages.<a href="qptrlist.html#append">append</a>( m ); } + + <a href="qstring.html">QString</a> folderName() { return fName; } + +<a name="x132"></a> Message *firstMessage() { return lstMessages.<a href="qptrlist.html#first">first</a>(); } +<a name="x133"></a> Message *nextMessage() { return lstMessages.<a href="qptrlist.html#next">next</a>(); } + +protected: + <a href="qstring.html">QString</a> fName; + <a href="qptrlist.html">QPtrList</a><Message> lstMessages; + +}; + +// ----------------------------------------------------------------- + +class FolderListItem : public <a href="qlistviewitem.html">QListViewItem</a> +{ +public: + FolderListItem( <a href="qlistview.html">QListView</a> *parent, Folder *f ); + FolderListItem( FolderListItem *parent, Folder *f ); + + void insertSubFolders( const <a href="qobjectlist.html">QObjectList</a> *lst ); + + Folder *folder() { return myFolder; } + +protected: + Folder *myFolder; + +}; + +// ----------------------------------------------------------------- + +class MessageListItem : public <a href="qlistviewitem.html">QListViewItem</a> +{ +public: + MessageListItem( <a href="qlistview.html">QListView</a> *parent, Message *m ); + + virtual void paintCell( <a href="qpainter.html">QPainter</a> *p, const <a href="qcolorgroup.html">QColorGroup</a> &cg, + int column, int width, int alignment ); + + Message *message() { return myMessage; } + +protected: + Message *myMessage; + +}; + +// ----------------------------------------------------------------- + +class ListViews : public <a href="qsplitter.html">QSplitter</a> +{ + Q_OBJECT + +public: + ListViews( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 ); + ~ListViews() + {} + +protected: + void initFolders(); + void initFolder( Folder *folder, unsigned int &count ); + void setupFolders(); + + <a href="qlistview.html">QListView</a> *messages, *folders; + <a href="qlabel.html">QLabel</a> *message; + <a href="qpopupmenu.html">QPopupMenu</a>* menu; + + <a href="qptrlist.html">QPtrList</a><Folder> lstFolders; + +protected slots: + void slotFolderChanged( <a href="qlistviewitem.html">QListViewItem</a>* ); + void slotMessageChanged(); + void slotRMB( <a href="qlistviewitem.html">QListViewItem</a>*, const <a href="qpoint.html">QPoint</a> &, int ); + +}; + +#endif +</pre> + +<p> <hr> +<p> Implementation: +<p> <pre>/**************************************************************************** +** $Id: qt/listviews.cpp 3.3.8 edited Jan 11 14:37 $ +** +** Copyright (C) 1992-2007 Trolltech ASA. 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 "listviews.h" + +#include <<a href="qlabel-h.html">qlabel.h</a>> +#include <<a href="qpainter-h.html">qpainter.h</a>> +#include <<a href="qpalette-h.html">qpalette.h</a>> +#include <<a href="qobjectlist-h.html">qobjectlist.h</a>> +#include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>> +#include <<a href="qheader-h.html">qheader.h</a>> +#include <<a href="qregexp-h.html">qregexp.h</a>> + +// ----------------------------------------------------------------- + +MessageHeader::MessageHeader( const MessageHeader &mh ) +{ + msender = mh.msender; + msubject = mh.msubject; + mdatetime = mh.mdatetime; +} + +MessageHeader &MessageHeader::operator=( const MessageHeader &mh ) +{ + msender = mh.msender; + msubject = mh.msubject; + mdatetime = mh.mdatetime; + + return *this; +} + +// ----------------------------------------------------------------- + +<a name="f208"></a>Folder::Folder( Folder *parent, const <a href="qstring.html">QString</a> &name ) + : <a href="qobject.html">QObject</a>( parent, name ), fName( name ) +{ + lstMessages.setAutoDelete( TRUE ); +} + +// ----------------------------------------------------------------- + +<a name="f209"></a>FolderListItem::FolderListItem( <a href="qlistview.html">QListView</a> *parent, Folder *f ) + : <a href="qlistviewitem.html">QListViewItem</a>( parent ) +{ + myFolder = f; + <a href="qlistviewitem.html#setText">setText</a>( 0, f->folderName() ); + + if ( myFolder->children() ) + insertSubFolders( myFolder->children() ); +} + +FolderListItem::FolderListItem( FolderListItem *parent, Folder *f ) + : <a href="qlistviewitem.html">QListViewItem</a>( parent ) +{ + myFolder = f; + + <a href="qlistviewitem.html#setText">setText</a>( 0, f->folderName() ); + + if ( myFolder->children() ) + insertSubFolders( myFolder->children() ); +} + +void <a name="f210"></a>FolderListItem::insertSubFolders( const <a href="qobjectlist.html">QObjectList</a> *lst ) +{ + Folder *f; + for ( f = ( Folder* )( ( <a href="qobjectlist.html">QObjectList</a>* )lst )->first(); f; f = ( Folder* )( ( <a href="qobjectlist.html">QObjectList</a>* )lst )->next() ) + (void)new FolderListItem( this, f ); +} + +// ----------------------------------------------------------------- + +<a name="f218"></a>MessageListItem::MessageListItem( <a href="qlistview.html">QListView</a> *parent, Message *m ) + : <a href="qlistviewitem.html">QListViewItem</a>( parent ) +{ + myMessage = m; + <a href="qlistviewitem.html#setText">setText</a>( 0, myMessage->header().sender() ); + <a href="qlistviewitem.html#setText">setText</a>( 1, myMessage->header().subject() ); + <a href="qlistviewitem.html#setText">setText</a>( 2, myMessage->header().datetime().toString() ); +} + +<a name="x157"></a>void MessageListItem::<a href="qlistviewitem.html#paintCell">paintCell</a>( <a href="qpainter.html">QPainter</a> *p, const <a href="qcolorgroup.html">QColorGroup</a> &cg, + int column, int width, int alignment ) +{ + <a href="qcolorgroup.html">QColorGroup</a> _cg( cg ); +<a name="x135"></a> <a href="qcolor.html">QColor</a> c = _cg.<a href="qcolorgroup.html#text">text</a>(); + + if ( myMessage->state() == Message::Unread ) +<a name="x134"></a> _cg.<a href="qcolorgroup.html#setColor">setColor</a>( QColorGroup::Text, Qt::red ); + + QListViewItem::<a href="qlistviewitem.html#paintCell">paintCell</a>( p, _cg, column, width, alignment ); + + _cg.<a href="qcolorgroup.html#setColor">setColor</a>( QColorGroup::Text, c ); +} + +// ----------------------------------------------------------------- + +<a name="f211"></a>ListViews::ListViews( <a href="qwidget.html">QWidget</a> *parent, const char *name ) + : <a href="qsplitter.html">QSplitter</a>( Qt::Horizontal, parent, name ) +{ + lstFolders.setAutoDelete( TRUE ); + + folders = new <a href="qlistview.html">QListView</a>( this ); +<a name="x147"></a> folders-><a href="qlistview.html#header">header</a>()->setClickEnabled( FALSE ); +<a name="x141"></a> folders-><a href="qlistview.html#addColumn">addColumn</a>( "Folder" ); + + initFolders(); + setupFolders(); + +<a name="x152"></a> folders-><a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE ); + <a href="qsplitter.html#setResizeMode">setResizeMode</a>( folders, QSplitter::KeepSize ); + + <a href="qsplitter.html">QSplitter</a> *vsplitter = new <a href="qsplitter.html">QSplitter</a>( Qt::Vertical, this ); + + messages = new <a href="qlistview.html">QListView</a>( vsplitter ); + messages-><a href="qlistview.html#addColumn">addColumn</a>( "Sender" ); + messages-><a href="qlistview.html#addColumn">addColumn</a>( "Subject" ); + messages-><a href="qlistview.html#addColumn">addColumn</a>( "Date" ); +<a name="x150"></a> messages-><a href="qlistview.html#setColumnAlignment">setColumnAlignment</a>( 1, Qt::AlignRight ); +<a name="x149"></a> messages-><a href="qlistview.html#setAllColumnsShowFocus">setAllColumnsShowFocus</a>( TRUE ); +<a name="x155"></a> messages-><a href="qlistview.html#setShowSortIndicator">setShowSortIndicator</a>( TRUE ); + menu = new <a href="qpopupmenu.html">QPopupMenu</a>( messages ); + for( int i = 1; i <= 10; i++ ) + menu-><a href="qmenudata.html#insertItem">insertItem</a>( QString( "Context Item %1" ).arg( i ) ); +<a name="x143"></a> <a href="qobject.html#connect">connect</a>(messages, SIGNAL( <a href="qlistview.html#contextMenuRequested">contextMenuRequested</a>( <a href="qlistviewitem.html">QListViewItem</a> *, const <a href="qpoint.html">QPoint</a>& , int ) ), + this, SLOT( slotRMB( <a href="qlistviewitem.html">QListViewItem</a> *, const <a href="qpoint.html">QPoint</a> &, int ) ) ); +<a name="x161"></a> vsplitter-><a href="qsplitter.html#setResizeMode">setResizeMode</a>( messages, QSplitter::KeepSize ); + + message = new <a href="qlabel.html">QLabel</a>( vsplitter ); + message-><a href="qlabel.html#setAlignment">setAlignment</a>( Qt::AlignTop ); +<a name="x163"></a> message-><a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( PaletteBase ); + +<a name="x148"></a> <a href="qobject.html#connect">connect</a>( folders, SIGNAL( <a href="qlistview.html#selectionChanged">selectionChanged</a>( <a href="qlistviewitem.html">QListViewItem</a>* ) ), + this, SLOT( slotFolderChanged( <a href="qlistviewitem.html">QListViewItem</a>* ) ) ); + <a href="qobject.html#connect">connect</a>( messages, SIGNAL( <a href="qlistview.html#selectionChanged">selectionChanged</a>() ), + this, SLOT( slotMessageChanged() ) ); +<a name="x144"></a> <a href="qobject.html#connect">connect</a>( messages, SIGNAL( <a href="qlistview.html#currentChanged">currentChanged</a>( <a href="qlistviewitem.html">QListViewItem</a> * ) ), + this, SLOT( slotMessageChanged() ) ); + +<a name="x154"></a> messages-><a href="qlistview.html#setSelectionMode">setSelectionMode</a>( QListView::Extended ); + // some preparations +<a name="x146"></a> folders-><a href="qlistview.html#firstChild">firstChild</a>()->setOpen( TRUE ); + folders-><a href="qlistview.html#firstChild">firstChild</a>()->firstChild()->setOpen( TRUE ); +<a name="x151"></a> folders-><a href="qlistview.html#setCurrentItem">setCurrentItem</a>( folders-><a href="qlistview.html#firstChild">firstChild</a>()->firstChild()->firstChild() ); +<a name="x153"></a> folders-><a href="qlistview.html#setSelected">setSelected</a>( folders-><a href="qlistview.html#firstChild">firstChild</a>()->firstChild()->firstChild(), TRUE ); + + messages-><a href="qlistview.html#setSelected">setSelected</a>( messages-><a href="qlistview.html#firstChild">firstChild</a>(), TRUE ); + messages-><a href="qlistview.html#setCurrentItem">setCurrentItem</a>( messages-><a href="qlistview.html#firstChild">firstChild</a>() ); + message-><a href="qframe.html#setMargin">setMargin</a>( 5 ); + + <a href="qvaluelist.html">QValueList</a><int> lst; +<a name="x160"></a> lst.<a href="qptrlist.html#append">append</a>( 170 ); + <a href="qsplitter.html#setSizes">setSizes</a>( lst ); +} + +void <a name="f212"></a>ListViews::initFolders() +{ + unsigned int mcount = 1; + + for ( unsigned int i = 1; i < 20; i++ ) { + <a href="qstring.html">QString</a> str; + str = QString( "Folder %1" ).arg( i ); + Folder *f = new Folder( 0, str ); + for ( unsigned int j = 1; j < 5; j++ ) { + <a href="qstring.html">QString</a> str2; + str2 = QString( "Sub Folder %1" ).arg( j ); + Folder *f2 = new Folder( f, str2 ); + for ( unsigned int k = 1; k < 3; k++ ) { + <a href="qstring.html">QString</a> str3; + str3 = QString( "Sub Sub Folder %1" ).arg( k ); + Folder *f3 = new Folder( f2, str3 ); + initFolder( f3, mcount ); + } + } + lstFolders.append( f ); + } +} + +void <a name="f213"></a>ListViews::initFolder( Folder *folder, unsigned int &count ) +{ + for ( unsigned int i = 0; i < 15; i++, count++ ) { + <a href="qstring.html">QString</a> str; + str = QString( "Message %1 " ).arg( count ); +<a name="x137"></a> <a href="qdatetime.html">QDateTime</a> dt = QDateTime::<a href="qdatetime.html#currentDateTime">currentDateTime</a>(); +<a name="x136"></a> dt = dt.<a href="qdatetime.html#addSecs">addSecs</a>( 60 * count ); + MessageHeader mh( "Trolltech <[email protected]> ", str, dt ); + + <a href="qstring.html">QString</a> body; + body = QString( "This is the message number %1 of this application, \n" + "which shows how to use QListViews, QListViewItems, \n" + "QSplitters and so on. The code should show how easy\n" + "this can be done in Qt." ).arg( count ); + Message *msg = new Message( mh, body ); + folder->addMessage( msg ); + } +} + +void <a name="f214"></a>ListViews::setupFolders() +{ +<a name="x142"></a> folders-><a href="qlistview.html#clear">clear</a>(); + + for ( Folder* f = lstFolders.first(); f; f = lstFolders.next() ) + (void)new FolderListItem( folders, f ); +} + +void <a name="f215"></a>ListViews::slotRMB( <a href="qlistviewitem.html">QListViewItem</a>* Item, const <a href="qpoint.html">QPoint</a> & point, int ) +{ + if( Item ) +<a name="x159"></a> menu-><a href="qpopupmenu.html#popup">popup</a>( point ); +} + + +void <a name="f216"></a>ListViews::slotFolderChanged( <a href="qlistviewitem.html">QListViewItem</a> *i ) +{ + if ( !i ) + return; + messages-><a href="qlistview.html#clear">clear</a>(); +<a name="x140"></a> message-><a href="qlabel.html#setText">setText</a>( "" ); + + FolderListItem *item = ( FolderListItem* )i; + + for ( Message* msg = item->folder()->firstMessage(); msg; + msg = item->folder()->nextMessage() ) + (void)new MessageListItem( messages, msg ); +} + +void <a name="f217"></a>ListViews::slotMessageChanged() +{ +<a name="x145"></a> <a href="qlistviewitem.html">QListViewItem</a> *i = messages-><a href="qlistview.html#currentItem">currentItem</a>(); + if ( !i ) + return; + +<a name="x156"></a> if ( !i-><a href="qlistviewitem.html#isSelected">isSelected</a>() ) { + message-><a href="qlabel.html#setText">setText</a>( "" ); + return; + } + + MessageListItem *item = ( MessageListItem* )i; + Message *msg = item->message(); + + <a href="qstring.html">QString</a> text; + <a href="qstring.html">QString</a> tmp = msg->header().sender(); +<a name="x162"></a> tmp = tmp.<a href="qstring.html#replace">replace</a>( "<", "&lt;" ); + tmp = tmp.<a href="qstring.html#replace">replace</a>( ">", "&gt;" ); + text = QString( "<b><i>From:</i></b> <a href=\"mailto:[email protected]\">%1</a><br>" + "<b><i>Subject:</i></b> <big><big><b>%2</b></big></big><br>" + "<b><i>Date:</i></b> %3<br><br>" + "%4" ). + arg( tmp ).arg( msg->header().subject() ). + arg( msg->header().datetime().toString() ).arg( msg->body() ); + + message-><a href="qlabel.html#setText">setText</a>( text ); + + msg->setState( Message::Read ); +} +</pre> + +<p> <hr> +<p> Main: +<p> <pre>/**************************************************************************** +** $Id: qt/main.cpp 3.3.8 edited Jan 11 14:37 $ +** +** Copyright (C) 1992-2007 Trolltech ASA. 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 "listviews.h" +#include <<a href="qapplication-h.html">qapplication.h</a>> + +int main( int argc, char **argv ) +{ + <a href="qapplication.html">QApplication</a> a( argc, argv ); + + ListViews listViews; +<a name="x166"></a> listViews.<a href="qwidget.html#resize">resize</a>( 640, 480 ); +<a name="x167"></a> listViews.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Listview" ); + a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &listViews ); + listViews.<a href="qwidget.html#show">show</a>(); + + return a.<a href="qapplication.html#exec">exec</a>(); +} +</pre> + +<p>See also <a href="examples.html">Examples</a>. + +<!-- eof --> +<p><address><hr><div align=center> +<table width=100% cellspacing=0 border=0><tr> +<td>Copyright © 2007 +<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> +<td align=right><div align=right>Qt 3.3.8</div> +</table></div></address></body> +</html> |