📄 richtext-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 - richtext/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>Richtext</h1><br clear="all"> In this examples it's demonstrated how to display rich text, using Qt, in a widget. In this example some sayings taken from the famous Unix "Fortune" are displayed nicely formatted. <hr> Header file: <pre>/****************************************************************************** $Id: qt/examples/richtext/richtext.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 RICHTEXT_H#define RICHTEXT_H#include <<a href="qvbox-h.html">qvbox.h</a>>class QTextView;class QPushButton;class MyRichText : public QVBox{ Q_OBJECTpublic: MyRichText( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );protected: <a href="qtextview.html">QTextView</a> *view; <a href="qpushbutton.html">QPushButton</a> *bClose, *bNext, *bPrev; int num;protected slots: void prev(); void next();};#endif</pre> <hr> Implementation: <pre>/****************************************************************************** $Id: qt/examples/richtext/richtext.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 "richtext.h"#include <<a href="qhbox-h.html">qhbox.h</a>>#include <<a href="qhbox-h.html">qhbox.h</a>>#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>#include <<a href="qtextview-h.html">qtextview.h</a>>#include <<a href="qbrush-h.html">qbrush.h</a>>#include <<a href="qapplication-h.html">qapplication.h</a>>static const char* sayings[] = { "<b>Saying 1:</b><br>" "<hr><br><br>" "<big>Evil is that which one believes of others. It is a sin to believe evil " "of others, but it is seldom a mistake.</big><br><br>" "<center><i>-- H.L. Mencken</i></center>", "<b>Saying 2:</b><br>" "<hr><br><br>" "<big>A well-used door needs no oil on its hinges.<br>" "A swift-flowing steam does not grow stagnant.<br>" "Neither sound nor thoughts can travel through a vacuum.<br>" "Software rots if not used.<br><br>" "These are great mysteries.</big><br><br>" "<center><i>-- Geoffrey James, \"The Tao of Programming\"</i></center>", "<b>Saying 3:</b><br>" "<hr><br><br>" "<big>Show business is just like high school, except you get paid.</big><br><br>" "<center><i>-- Martin Mull</i></center>", "<b>Saying 4:</b><br>" "<hr><br><br>" "<big><b>The Least Successful Executions</b><br>" "<twocolumn><p> History has furnished us with two executioners worthy of attention. " "The first performed in Sydney in Australia. In 1803 three attempts were " "made to hang a Mr. Joseph Samuels. On the first two of these the rope " "snapped, while on the third Mr. Samuels just hung there peacefully until he " "and everyone else got bored. Since he had proved unsusceptible to capital " "punishment, he was reprieved.</p>" "<p> The most important British executioner was Mr. James Berry who " "tried three times in 1885 to hang Mr. John Lee at Exeter Jail, but on each " "occasion failed to get the trap door open.<!p>" "<p> In recognition of this achievement, the Home Secretary commuted " "Lee's sentence to \"life\" imprisonment. He was released in 1917, emigrated " "to America and lived until 1933.</p></twocolumn></big><br><br>" "<center><i>-- Stephen Pile, \"The Book of Heroic Failures\"</i></center>", "<b>Saying 5:</b><br>" "<hr><br><br>" "<big>If you can, help others. If you can't, at least don't hurt others.</big><br><br>" "<center><i>-- the Dalai Lama</i></center>", "<b>Saying 6:</b><br>" "<hr><br><br>" "<big>Television has brought back murder into the home -- where it belongs.</big><br><br>" "<center><i>-- Alfred Hitchcock</i></center>", "<b>Saying 7:</b><br>" "<hr><br><br>" "<big>I don't know who my grandfather was; I am much more concerned to know " "what his grandson will be.</big><br><br>" "<center><i>-- Abraham Lincoln</i></center>", 0};MyRichText::MyRichText( <a href="qwidget.html">QWidget</a> *parent, const char *name ) : <a href="qvbox.html">QVBox</a>( parent, name ){ <a href="qframe.html#b11d00">setMargin</a>( 5 ); view = new <a href="qtextview.html">QTextView</a>( this ); view->setText( "This is a <b>Test</b> with <i>italic</i> <u>stuff</u>" ); <a href="qbrush.html">QBrush</a> paper; paper.<a href="qbrush.html#406414">setPixmap</a>( <a href="qpixmap.html">QPixmap</a>( "../richtext/marble.png" ) ); view->setPaper( paper ); view->setText( sayings[0] ); view->setMinimumSize( 450, 250 ); <a href="qhbox.html">QHBox</a> *buttons = new <a href="qhbox.html">QHBox</a>( this ); buttons-><a href="qframe.html#b11d00">setMargin</a>( 5 ); bClose = new <a href="qpushbutton.html">QPushButton</a>( "&Close", buttons ); bPrev = new <a href="qpushbutton.html">QPushButton</a>( "<< &Prev", buttons ); bNext = new <a href="qpushbutton.html">QPushButton</a>( "&Next >>", buttons ); bPrev->setEnabled( FALSE ); <a href="qobject.html#fbde73">connect</a>( bClose, SIGNAL( clicked() ), qApp, SLOT( quit() ) ); <a href="qobject.html#fbde73">connect</a>( bPrev, SIGNAL( clicked() ), this, SLOT( <a href=#156>prev</a>() ) ); <a href="qobject.html#fbde73">connect</a>( bNext, SIGNAL( clicked() ), this, SLOT( <a href=#157>next</a>() ) ); num = 0;}void <a name="156"></a>MyRichText::prev(){ if ( num <= 0 ) return; num--; view->setText( sayings[num] ); if ( num == 0 ) bPrev->setEnabled( FALSE ); bNext->setEnabled( TRUE );}void <a name="157"></a>MyRichText::next(){ if ( !sayings[++num] ) return; view->setText( sayings[num] ); if ( !sayings[num + 1] ) bNext->setEnabled( FALSE ); bPrev->setEnabled( TRUE );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/richtext/main.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 "richtext.h"#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>int main( int argc, char **argv ){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); MyRichText richtext; richtext.<a name="resize"></a><a href="qwidget.html#ff9d07">resize</a>( 450, 350 ); richtext.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - Richtext" ); a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &richtext ); richtext.<a name="show"></a><a href="qwidget.html#200ee5">show</a>(); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -