You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/html/listviews-example.html

513 lines
21 KiB

<!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>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&nbsp;Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped&nbsp;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 TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef LISTVIEWS_H
#define LISTVIEWS_H
#include &lt;<a href="tqsplitter-h.html">tqsplitter.h</a>&gt;
#include &lt;<a href="tqstring-h.html">tqstring.h</a>&gt;
#include &lt;<a href="tqobject-h.html">tqobject.h</a>&gt;
#include &lt;<a href="tqdatetime-h.html">tqdatetime.h</a>&gt;
#include &lt;<a href="tqptrlist-h.html">tqptrlist.h</a>&gt;
#include &lt;<a href="tqlistview-h.html">tqlistview.h</a>&gt;
class TQListView;
class TQLabel;
class TQPainter;
class TQColorGroup;
class TQObjectList;
class TQPopupMenu;
// -----------------------------------------------------------------
class MessageHeader
{
public:
MessageHeader( const <a href="tqstring.html">TQString</a> &amp;_sender, const <a href="tqstring.html">TQString</a> &amp;_subject, const <a href="tqdatetime.html">TQDateTime</a> &amp;_datetime )
: msender( _sender ), msubject( _subject ), mdatetime( _datetime )
{}
MessageHeader( const MessageHeader &amp;mh );
MessageHeader &amp;operator=( const MessageHeader &amp;mh );
<a href="tqstring.html">TQString</a> sender() { return msender; }
<a href="tqstring.html">TQString</a> subject() { return msubject; }
<a href="tqdatetime.html">TQDateTime</a> datetime() { return mdatetime; }
protected:
<a href="tqstring.html">TQString</a> msender, msubject;
<a href="tqdatetime.html">TQDateTime</a> mdatetime;
};
// -----------------------------------------------------------------
class Message
{
public:
enum State { Read = 0,
Unread};
Message( const MessageHeader &amp;mh, const <a href="tqstring.html">TQString</a> &amp;_body )
: mheader( mh ), mbody( _body ), mstate( Unread )
{}
Message( const Message &amp;m )
: mheader( m.mheader ), mbody( m.mbody ), mstate( m.mstate )
{}
MessageHeader header() { return mheader; }
<a href="tqstring.html">TQString</a> body() { return mbody; }
void setState( const State &amp;s ) { mstate = s; }
State state() { return mstate; }
protected:
MessageHeader mheader;
<a href="tqstring.html">TQString</a> mbody;
State mstate;
};
// -----------------------------------------------------------------
class Folder : public <a href="tqobject.html">TQObject</a>
{
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
Folder( Folder *parent, const <a href="tqstring.html">TQString</a> &amp;name );
~Folder()
{}
void addMessage( Message *m )
<a name="x131"></a> { lstMessages.<a href="tqptrlist.html#append">append</a>( m ); }
<a href="tqstring.html">TQString</a> folderName() { return fName; }
<a name="x132"></a> Message *firstMessage() { return lstMessages.<a href="tqptrlist.html#first">first</a>(); }
<a name="x133"></a> Message *nextMessage() { return lstMessages.<a href="tqptrlist.html#next">next</a>(); }
protected:
<a href="tqstring.html">TQString</a> fName;
<a href="tqptrlist.html">TQPtrList</a>&lt;Message&gt; lstMessages;
};
// -----------------------------------------------------------------
class FolderListItem : public <a href="tqlistviewitem.html">TQListViewItem</a>
{
public:
FolderListItem( <a href="tqlistview.html">TQListView</a> *parent, Folder *f );
FolderListItem( FolderListItem *parent, Folder *f );
void insertSubFolders( const <a href="tqobjectlist.html">TQObjectList</a> *lst );
Folder *folder() { return myFolder; }
protected:
Folder *myFolder;
};
// -----------------------------------------------------------------
class MessageListItem : public <a href="tqlistviewitem.html">TQListViewItem</a>
{
public:
MessageListItem( <a href="tqlistview.html">TQListView</a> *parent, Message *m );
virtual void paintCell( <a href="tqpainter.html">TQPainter</a> *p, const <a href="tqcolorgroup.html">TQColorGroup</a> &amp;cg,
int column, int width, int alignment );
Message *message() { return myMessage; }
protected:
Message *myMessage;
};
// -----------------------------------------------------------------
class ListViews : public <a href="tqsplitter.html">TQSplitter</a>
{
TQ_OBJECT
public:
ListViews( <a href="tqwidget.html">TQWidget</a> *parent = 0, const char *name = 0 );
~ListViews()
{}
protected:
void initFolders();
void initFolder( Folder *folder, unsigned int &amp;count );
void setupFolders();
<a href="tqlistview.html">TQListView</a> *messages, *folders;
<a href="tqlabel.html">TQLabel</a> *message;
<a href="tqpopupmenu.html">TQPopupMenu</a>* menu;
<a href="tqptrlist.html">TQPtrList</a>&lt;Folder&gt; lstFolders;
protected slots:
void slotFolderChanged( <a href="tqlistviewitem.html">TQListViewItem</a>* );
void slotMessageChanged();
void slotRMB( <a href="tqlistviewitem.html">TQListViewItem</a>*, const <a href="tqpoint.html">TQPoint</a> &amp;, 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 TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "listviews.h"
#include &lt;<a href="tqlabel-h.html">tqlabel.h</a>&gt;
#include &lt;<a href="tqpainter-h.html">tqpainter.h</a>&gt;
#include &lt;<a href="tqpalette-h.html">tqpalette.h</a>&gt;
#include &lt;<a href="tqobjectlist-h.html">tqobjectlist.h</a>&gt;
#include &lt;<a href="tqpopupmenu-h.html">tqpopupmenu.h</a>&gt;
#include &lt;<a href="tqheader-h.html">tqheader.h</a>&gt;
#include &lt;<a href="tqregexp-h.html">tqregexp.h</a>&gt;
// -----------------------------------------------------------------
MessageHeader::MessageHeader( const MessageHeader &amp;mh )
{
msender = mh.msender;
msubject = mh.msubject;
mdatetime = mh.mdatetime;
}
MessageHeader &amp;MessageHeader::operator=( const MessageHeader &amp;mh )
{
msender = mh.msender;
msubject = mh.msubject;
mdatetime = mh.mdatetime;
return *this;
}
// -----------------------------------------------------------------
<a name="f208"></a>Folder::Folder( Folder *parent, const <a href="tqstring.html">TQString</a> &amp;name )
: <a href="tqobject.html">TQObject</a>( parent, name ), fName( name )
{
lstMessages.setAutoDelete( TRUE );
}
// -----------------------------------------------------------------
<a name="f209"></a>FolderListItem::FolderListItem( <a href="tqlistview.html">TQListView</a> *parent, Folder *f )
: <a href="tqlistviewitem.html">TQListViewItem</a>( parent )
{
myFolder = f;
<a href="tqlistviewitem.html#setText">setText</a>( 0, f-&gt;folderName() );
if ( myFolder-&gt;children() )
insertSubFolders( myFolder-&gt;children() );
}
FolderListItem::FolderListItem( FolderListItem *parent, Folder *f )
: <a href="tqlistviewitem.html">TQListViewItem</a>( parent )
{
myFolder = f;
<a href="tqlistviewitem.html#setText">setText</a>( 0, f-&gt;folderName() );
if ( myFolder-&gt;children() )
insertSubFolders( myFolder-&gt;children() );
}
void <a name="f210"></a>FolderListItem::insertSubFolders( const <a href="tqobjectlist.html">TQObjectList</a> *lst )
{
Folder *f;
for ( f = ( Folder* )( ( <a href="tqobjectlist.html">TQObjectList</a>* )lst )-&gt;first(); f; f = ( Folder* )( ( <a href="tqobjectlist.html">TQObjectList</a>* )lst )-&gt;next() )
(void)new FolderListItem( this, f );
}
// -----------------------------------------------------------------
<a name="f218"></a>MessageListItem::MessageListItem( <a href="tqlistview.html">TQListView</a> *parent, Message *m )
: <a href="tqlistviewitem.html">TQListViewItem</a>( parent )
{
myMessage = m;
<a href="tqlistviewitem.html#setText">setText</a>( 0, myMessage-&gt;header().sender() );
<a href="tqlistviewitem.html#setText">setText</a>( 1, myMessage-&gt;header().subject() );
<a href="tqlistviewitem.html#setText">setText</a>( 2, myMessage-&gt;header().datetime().toString() );
}
<a name="x157"></a>void MessageListItem::<a href="tqlistviewitem.html#paintCell">paintCell</a>( <a href="tqpainter.html">TQPainter</a> *p, const <a href="tqcolorgroup.html">TQColorGroup</a> &amp;cg,
int column, int width, int alignment )
{
<a href="tqcolorgroup.html">TQColorGroup</a> _cg( cg );
<a name="x135"></a> <a href="tqcolor.html">TQColor</a> c = _cg.<a href="tqcolorgroup.html#text">text</a>();
if ( myMessage-&gt;state() == Message::Unread )
<a name="x134"></a> _cg.<a href="tqcolorgroup.html#setColor">setColor</a>( TQColorGroup::Text, TQt::red );
TQListViewItem::<a href="tqlistviewitem.html#paintCell">paintCell</a>( p, _cg, column, width, alignment );
_cg.<a href="tqcolorgroup.html#setColor">setColor</a>( TQColorGroup::Text, c );
}
// -----------------------------------------------------------------
<a name="f211"></a>ListViews::ListViews( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
: <a href="tqsplitter.html">TQSplitter</a>( TQt::Horizontal, parent, name )
{
lstFolders.setAutoDelete( TRUE );
folders = new <a href="tqlistview.html">TQListView</a>( this );
<a name="x147"></a> folders-&gt;<a href="tqlistview.html#header">header</a>()-&gt;setClickEnabled( FALSE );
<a name="x141"></a> folders-&gt;<a href="tqlistview.html#addColumn">addColumn</a>( "Folder" );
initFolders();
setupFolders();
<a name="x152"></a> folders-&gt;<a href="tqlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );
<a href="tqsplitter.html#setResizeMode">setResizeMode</a>( folders, TQSplitter::KeepSize );
<a href="tqsplitter.html">TQSplitter</a> *vsplitter = new <a href="tqsplitter.html">TQSplitter</a>( TQt::Vertical, this );
messages = new <a href="tqlistview.html">TQListView</a>( vsplitter );
messages-&gt;<a href="tqlistview.html#addColumn">addColumn</a>( "Sender" );
messages-&gt;<a href="tqlistview.html#addColumn">addColumn</a>( "Subject" );
messages-&gt;<a href="tqlistview.html#addColumn">addColumn</a>( "Date" );
<a name="x150"></a> messages-&gt;<a href="tqlistview.html#setColumnAlignment">setColumnAlignment</a>( 1, TQt::AlignRight );
<a name="x149"></a> messages-&gt;<a href="tqlistview.html#setAllColumnsShowFocus">setAllColumnsShowFocus</a>( TRUE );
<a name="x155"></a> messages-&gt;<a href="tqlistview.html#setShowSortIndicator">setShowSortIndicator</a>( TRUE );
menu = new <a href="tqpopupmenu.html">TQPopupMenu</a>( messages );
for( int i = 1; i &lt;= 10; i++ )
menu-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( TQString( "Context Item %1" ).arg( i ) );
<a name="x143"></a> <a href="tqobject.html#connect">connect</a>(messages, TQ_SIGNAL( <a href="tqlistview.html#contextMenuRequested">contextMenuRequested</a>( <a href="tqlistviewitem.html">TQListViewItem</a> *, const <a href="tqpoint.html">TQPoint</a>&amp; , int ) ),
this, TQ_SLOT( slotRMB( <a href="tqlistviewitem.html">TQListViewItem</a> *, const <a href="tqpoint.html">TQPoint</a> &amp;, int ) ) );
<a name="x161"></a> vsplitter-&gt;<a href="tqsplitter.html#setResizeMode">setResizeMode</a>( messages, TQSplitter::KeepSize );
message = new <a href="tqlabel.html">TQLabel</a>( vsplitter );
message-&gt;<a href="tqlabel.html#setAlignment">setAlignment</a>( TQt::AlignTop );
<a name="x163"></a> message-&gt;<a href="tqwidget.html#setBackgroundMode">setBackgroundMode</a>( PaletteBase );
<a name="x148"></a> <a href="tqobject.html#connect">connect</a>( folders, TQ_SIGNAL( <a href="tqlistview.html#selectionChanged">selectionChanged</a>( <a href="tqlistviewitem.html">TQListViewItem</a>* ) ),
this, TQ_SLOT( slotFolderChanged( <a href="tqlistviewitem.html">TQListViewItem</a>* ) ) );
<a href="tqobject.html#connect">connect</a>( messages, TQ_SIGNAL( <a href="tqlistview.html#selectionChanged">selectionChanged</a>() ),
this, TQ_SLOT( slotMessageChanged() ) );
<a name="x144"></a> <a href="tqobject.html#connect">connect</a>( messages, TQ_SIGNAL( <a href="tqlistview.html#currentChanged">currentChanged</a>( <a href="tqlistviewitem.html">TQListViewItem</a> * ) ),
this, TQ_SLOT( slotMessageChanged() ) );
<a name="x154"></a> messages-&gt;<a href="tqlistview.html#setSelectionMode">setSelectionMode</a>( TQListView::Extended );
// some preparations
<a name="x146"></a> folders-&gt;<a href="tqlistview.html#firstChild">firstChild</a>()-&gt;setOpen( TRUE );
folders-&gt;<a href="tqlistview.html#firstChild">firstChild</a>()-&gt;firstChild()-&gt;setOpen( TRUE );
<a name="x151"></a> folders-&gt;<a href="tqlistview.html#setCurrentItem">setCurrentItem</a>( folders-&gt;<a href="tqlistview.html#firstChild">firstChild</a>()-&gt;firstChild()-&gt;firstChild() );
<a name="x153"></a> folders-&gt;<a href="tqlistview.html#setSelected">setSelected</a>( folders-&gt;<a href="tqlistview.html#firstChild">firstChild</a>()-&gt;firstChild()-&gt;firstChild(), TRUE );
messages-&gt;<a href="tqlistview.html#setSelected">setSelected</a>( messages-&gt;<a href="tqlistview.html#firstChild">firstChild</a>(), TRUE );
messages-&gt;<a href="tqlistview.html#setCurrentItem">setCurrentItem</a>( messages-&gt;<a href="tqlistview.html#firstChild">firstChild</a>() );
message-&gt;<a href="tqframe.html#setMargin">setMargin</a>( 5 );
<a href="tqvaluelist.html">TQValueList</a>&lt;int&gt; lst;
<a name="x160"></a> lst.<a href="tqptrlist.html#append">append</a>( 170 );
<a href="tqsplitter.html#setSizes">setSizes</a>( lst );
}
void <a name="f212"></a>ListViews::initFolders()
{
unsigned int mcount = 1;
for ( unsigned int i = 1; i &lt; 20; i++ ) {
<a href="tqstring.html">TQString</a> str;
str = TQString( "Folder %1" ).arg( i );
Folder *f = new Folder( 0, str );
for ( unsigned int j = 1; j &lt; 5; j++ ) {
<a href="tqstring.html">TQString</a> str2;
str2 = TQString( "Sub Folder %1" ).arg( j );
Folder *f2 = new Folder( f, str2 );
for ( unsigned int k = 1; k &lt; 3; k++ ) {
<a href="tqstring.html">TQString</a> str3;
str3 = TQString( "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 &amp;count )
{
for ( unsigned int i = 0; i &lt; 15; i++, count++ ) {
<a href="tqstring.html">TQString</a> str;
str = TQString( "Message %1 " ).arg( count );
<a name="x137"></a> <a href="tqdatetime.html">TQDateTime</a> dt = TQDateTime::<a href="tqdatetime.html#currentDateTime">currentDateTime</a>();
<a name="x136"></a> dt = dt.<a href="tqdatetime.html#addSecs">addSecs</a>( 60 * count );
MessageHeader mh( "Trolltech &lt;info@trolltech.com&gt; ", str, dt );
<a href="tqstring.html">TQString</a> body;
body = TQString( "This is the message number %1 of this application, \n"
"which shows how to use TQListViews, TQListViewItems, \n"
"TQSplitters and so on. The code should show how easy\n"
"this can be done in TQt." ).arg( count );
Message *msg = new Message( mh, body );
folder-&gt;addMessage( msg );
}
}
void <a name="f214"></a>ListViews::setupFolders()
{
<a name="x142"></a> folders-&gt;<a href="tqlistview.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="tqlistviewitem.html">TQListViewItem</a>* Item, const <a href="tqpoint.html">TQPoint</a> &amp; point, int )
{
if( Item )
<a name="x159"></a> menu-&gt;<a href="tqpopupmenu.html#popup">popup</a>( point );
}
void <a name="f216"></a>ListViews::slotFolderChanged( <a href="tqlistviewitem.html">TQListViewItem</a> *i )
{
if ( !i )
return;
messages-&gt;<a href="tqlistview.html#clear">clear</a>();
<a name="x140"></a> message-&gt;<a href="tqlabel.html#setText">setText</a>( "" );
FolderListItem *item = ( FolderListItem* )i;
for ( Message* msg = item-&gt;folder()-&gt;firstMessage(); msg;
msg = item-&gt;folder()-&gt;nextMessage() )
(void)new MessageListItem( messages, msg );
}
void <a name="f217"></a>ListViews::slotMessageChanged()
{
<a name="x145"></a> <a href="tqlistviewitem.html">TQListViewItem</a> *i = messages-&gt;<a href="tqlistview.html#currentItem">currentItem</a>();
if ( !i )
return;
<a name="x156"></a> if ( !i-&gt;<a href="tqlistviewitem.html#isSelected">isSelected</a>() ) {
message-&gt;<a href="tqlabel.html#setText">setText</a>( "" );
return;
}
MessageListItem *item = ( MessageListItem* )i;
Message *msg = item-&gt;message();
<a href="tqstring.html">TQString</a> text;
<a href="tqstring.html">TQString</a> tmp = msg-&gt;header().sender();
<a name="x162"></a> tmp = tmp.<a href="tqstring.html#replace">replace</a>( "&lt;", "&amp;lt;" );
tmp = tmp.<a href="tqstring.html#replace">replace</a>( "&gt;", "&amp;gt;" );
text = TQString( "&lt;b&gt;&lt;i&gt;From:&lt;/i&gt;&lt;/b&gt; &lt;a href=\"mailto:info@trolltech.com\"&gt;%1&lt;/a&gt;&lt;br&gt;"
"&lt;b&gt;&lt;i&gt;Subject:&lt;/i&gt;&lt;/b&gt; &lt;big&gt;&lt;big&gt;&lt;b&gt;%2&lt;/b&gt;&lt;/big&gt;&lt;/big&gt;&lt;br&gt;"
"&lt;b&gt;&lt;i&gt;Date:&lt;/i&gt;&lt;/b&gt; %3&lt;br&gt;&lt;br&gt;"
"%4" ).
arg( tmp ).arg( msg-&gt;header().subject() ).
arg( msg-&gt;header().datetime().toString() ).arg( msg-&gt;body() );
message-&gt;<a href="tqlabel.html#setText">setText</a>( text );
msg-&gt;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 TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "listviews.h"
#include &lt;<a href="tqapplication-h.html">tqapplication.h</a>&gt;
int main( int argc, char **argv )
{
<a href="tqapplication.html">TQApplication</a> a( argc, argv );
ListViews listViews;
<a name="x166"></a> listViews.<a href="tqwidget.html#resize">resize</a>( 640, 480 );
<a name="x167"></a> listViews.<a href="tqwidget.html#setCaption">setCaption</a>( "TQt Example - Listview" );
a.<a href="tqapplication.html#setMainWidget">setMainWidget</a>( &amp;listViews );
listViews.<a href="tqwidget.html#show">show</a>();
return a.<a href="tqapplication.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 &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>