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.
576 lines
30 KiB
576 lines
30 KiB
<!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/doc/application-walkthrough.doc:36 -->
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Walkthrough: A Simple Application</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>Walkthrough: A Simple Application</h1>
|
|
|
|
|
|
|
|
<p>
|
|
<p> This walkthrough shows simple use of <a href="tqmainwindow.html">TQMainWindow</a>, <a href="tqmenubar.html">TQMenuBar</a>, <a href="tqpopupmenu.html">TQPopupMenu</a>, <a href="tqtoolbar.html">TQToolBar</a> and <a href="tqstatusbar.html">TQStatusBar</a> - classes that every
|
|
modern application window tends to use. (See also <a href="tutorial2.html">Tutorial #2</a>.)
|
|
<p> It also illustrates some aspects of <a href="ntqwhatsthis.html">TQWhatsThis</a> (for simple help) and a
|
|
typical <tt>main()</tt> using <a href="ntqapplication.html">TQApplication</a>.
|
|
<p> Finally, it shows a typical print function based on <a href="ntqprinter.html">TQPrinter</a>.
|
|
<p> <h2> The declaration of ApplicationWindow
|
|
</h2>
|
|
<a name="1"></a><p> Here's the header file in full:
|
|
<p> <pre>/****************************************************************************
|
|
** $Id: qt/application.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 APPLICATION_H
|
|
#define APPLICATION_H
|
|
|
|
#include <<a href="tqmainwindow-h.html">tqmainwindow.h</a>>
|
|
|
|
class TQTextEdit;
|
|
|
|
class ApplicationWindow: public <a href="tqmainwindow.html">TQMainWindow</a>
|
|
{
|
|
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
|
|
|
|
public:
|
|
ApplicationWindow();
|
|
~ApplicationWindow();
|
|
|
|
protected:
|
|
void closeEvent( <a href="qcloseevent.html">TQCloseEvent</a>* );
|
|
|
|
private slots:
|
|
void newDoc();
|
|
void choose();
|
|
void load( const <a href="tqstring.html">TQString</a> &fileName );
|
|
void save();
|
|
void saveAs();
|
|
void print();
|
|
|
|
void about();
|
|
void aboutTQt();
|
|
|
|
private:
|
|
<a href="ntqprinter.html">TQPrinter</a> *printer;
|
|
<a href="tqtextedit.html">TQTextEdit</a> *e;
|
|
<a href="tqstring.html">TQString</a> filename;
|
|
};
|
|
|
|
|
|
#endif
|
|
</pre>
|
|
|
|
<p> It declares a class that inherits <a href="tqmainwindow.html">TQMainWindow</a>, with slots and private
|
|
variables. The class pre-declaration of <a href="tqtextedit.html">TQTextEdit</a> at the beginning
|
|
(instead of an include) helps to speed up compilation. With this
|
|
trick, <tt>make depend</tt> won't insist on recompiling every <tt>.cpp</tt> file that
|
|
includes <tt>application.h</tt> when <a href="tqtextedit-h.html">tqtextedit.h</a> changes.
|
|
<p> <a name="simplemain"></a>
|
|
<h2> A simple main()
|
|
</h2>
|
|
<a name="2"></a><p> Here is <tt>main.cpp</tt> in full:
|
|
<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 <<a href="qapplication-h.html">ntqapplication.h</a>>
|
|
#include "application.h"
|
|
|
|
int main( int argc, char ** argv ) {
|
|
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
|
|
ApplicationWindow *mw = new ApplicationWindow();
|
|
mw-><a href="tqwidget.html#setCaption">setCaption</a>( "TQt Example - Application" );
|
|
<a name="x1598"></a> mw-><a href="tqwidget.html#show">show</a>();
|
|
<a name="x1599"></a> a.<a href="tqobject.html#connect">connect</a>( &a, TQ_SIGNAL(<a href="ntqapplication.html#lastWindowClosed">lastWindowClosed</a>()), &a, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
|
|
return a.<a href="ntqapplication.html#exec">exec</a>();
|
|
}
|
|
</pre>
|
|
|
|
<p> Now we'll look at <tt>main.cpp</tt> in detail.
|
|
<p>
|
|
|
|
<pre> int main( int argc, char ** argv ) {
|
|
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
|
|
</pre>
|
|
<p> With the above line, we create a <a href="ntqapplication.html">TQApplication</a> object with the usual
|
|
constructor and let it
|
|
parse <em>argc</em> and <em>argv</em>. TQApplication itself takes care of X11-specific
|
|
command-line options like <em>-geometry</em>, so the program will automatically
|
|
behave the way X clients are expected to.
|
|
<p> <pre> ApplicationWindow *mw = new ApplicationWindow();
|
|
mw-><a href="tqwidget.html#setCaption">setCaption</a>( "TQt Example - Application" );
|
|
<a name="x2115"></a> mw-><a href="tqwidget.html#show">show</a>();
|
|
</pre>
|
|
<p> We create an <em>ApplicationWindow</em> as a top-level widget, set its window
|
|
system caption to "Document 1", and <em>show()</em> it.
|
|
<p> <a name="close"></a>
|
|
<pre> a.<a href="tqobject.html#connect">connect</a>( &a, TQ_SIGNAL(<a href="ntqapplication.html#lastWindowClosed">lastWindowClosed</a>()), &a, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
|
|
</pre>
|
|
<p> When the application's last window is closed, it should quit. Both
|
|
the signal and the slot are predefined members of <a href="ntqapplication.html">TQApplication</a>.
|
|
<p> <pre> return a.<a href="ntqapplication.html#exec">exec</a>();
|
|
</pre>
|
|
<p> Having completed the application's initialization, we start the main
|
|
event loop (the GUI), and eventually return the error code
|
|
that TQApplication returns when it leaves the event loop.
|
|
<p> <pre> }
|
|
</pre>
|
|
<p> <a name="ApplicationWindow"></a>
|
|
<h2> The Implementation of ApplicationWindow
|
|
</h2>
|
|
<a name="3"></a><p>
|
|
|
|
<p> Since the implementation is quite large (almost 300 lines) we
|
|
won't list the whole thing. (The source code is included in the
|
|
examples/application directory.) Before we start with the constructor
|
|
there are three <tt>#include</tt>s worth mentioning:
|
|
<p> <pre> #include "filesave.xpm"
|
|
#include "fileopen.xpm"
|
|
#include "fileprint.xpm"
|
|
</pre>
|
|
<p> The tool buttons in our application wouldn't look good without icons!
|
|
These icons can be found in the XPM files included above. If you ever
|
|
moved a program to a different location and wondered why icons were
|
|
missing afterwards you will probably agree that it is a good idea to
|
|
compile them into the binary. This is what we are doing here.
|
|
<p> <pre> ApplicationWindow::ApplicationWindow()
|
|
: <a href="tqmainwindow.html">TQMainWindow</a>( 0, "example application main window", WDestructiveClose | WGroupLeader )
|
|
{
|
|
</pre>
|
|
<p> <em>ApplicationWindow</em> inherits <a href="tqmainwindow.html">TQMainWindow</a>, the TQt class that provides
|
|
typical application main windows, with menu bars, toolbars, etc.
|
|
<p> <pre> printer = new <a href="ntqprinter.html">TQPrinter</a>( TQPrinter::HighResolution );
|
|
</pre>
|
|
<p> The application example can print things, and we chose to have a
|
|
<a href="ntqprinter.html">TQPrinter</a> object lying around so that when the user changes a setting
|
|
during one printing, the new setting will be the default next time.
|
|
<p> <pre> <a href="ntqpixmap.html">TQPixmap</a> openIcon, saveIcon, printIcon;
|
|
</pre>
|
|
<p> For the sake of simplicity, our example only has a few commands in the
|
|
toolbar. The above variables are used to hold an icon for each of
|
|
them.
|
|
<p> <pre> <a href="tqtoolbar.html">TQToolBar</a> * fileTools = new <a href="tqtoolbar.html">TQToolBar</a>( this, "file operations" );
|
|
</pre>
|
|
<p> We create a toolbar in <em>this</em> window ...
|
|
<p> <pre> fileTools-><a href="tqtoolbar.html#setLabel">setLabel</a>( "File Operations" );
|
|
</pre>
|
|
<p> ... and define a title for it. When a user drags the toolbar out of
|
|
its location and floats it over the desktop, the toolbar-window will
|
|
show "File Operations" as caption.
|
|
<p> <pre> openIcon = TQPixmap( fileopen );
|
|
TQToolButton * fileOpen
|
|
= new <a href="tqtoolbutton.html">TQToolButton</a>( openIcon, "Open File", <a href="tqstring.html#TQString-null">TQString::null</a>,
|
|
this, TQ_SLOT(choose()), fileTools, "open file" );
|
|
</pre>
|
|
<p> Now we create the first tool button for the <em>fileTools</em> toolbar
|
|
with the appropriate icon and the tool-tip text "Open File".
|
|
The <tt>fileopen.xpm</tt> we included at the beginning
|
|
contains the definition of a pixmap called <em>fileopen</em>.
|
|
We use this icon to illustrate our first tool button.
|
|
<p> <pre> saveIcon = TQPixmap( filesave );
|
|
TQToolButton * fileSave
|
|
= new <a href="tqtoolbutton.html">TQToolButton</a>( saveIcon, "Save File", TQString::null,
|
|
this, TQ_SLOT(save()), fileTools, "save file" );
|
|
|
|
printIcon = TQPixmap( fileprint );
|
|
TQToolButton * filePrint
|
|
= new <a href="tqtoolbutton.html">TQToolButton</a>( printIcon, "Print File", TQString::null,
|
|
this, TQ_SLOT(print()), fileTools, "print file" );
|
|
</pre>
|
|
<p> In a similar way we create two more tool buttons in this toolbar, each with
|
|
appropriate icons and tool-tip text. All three buttons are connected
|
|
to appropriate slots in this object; for example, the "Print File" button
|
|
to <a href="#printer">ApplicationWindow::print()</a>.
|
|
<p> <pre> (void)TQWhatsThis::whatsThisButton( fileTools );
|
|
</pre>
|
|
<p> The fourth button in the toolbar is somewhat peculiar: it's the one that
|
|
provides "What's This?" help. This must be set up using a special
|
|
function, as its mouse interface is unusual.
|
|
<p> <pre> const char * fileOpenText = "<p><img source=\"fileopen\"> "
|
|
"Click this button to open a <em>new file</em>.<br>"
|
|
"You can also select the <b>Open</b> command "
|
|
"from the <b>File</b> menu.</p>";
|
|
|
|
TQWhatsThis::<a href="ntqwhatsthis.html#add">add</a>( fileOpen, fileOpenText );
|
|
</pre>
|
|
<p> With the above line we add the "What's This?" help-text to the
|
|
<em>fileOpen</em> button...
|
|
<p> <pre> TQMimeSourceFactory::<a href="tqmimesourcefactory.html#defaultFactory">defaultFactory</a>()->setPixmap( "document-open", openIcon );
|
|
</pre>
|
|
<p> ... and tell the rich-text engine that when a help-text (like the one
|
|
saved in <em>fileOpenText</em>) requests an image named "document-open", the <em>openIcon</em> pixmap is used.
|
|
<p> <pre> const char * fileSaveText = "<p>Click this button to save the file you "
|
|
"are editing. You will be prompted for a file name.\n"
|
|
"You can also select the <b>Save</b> command "
|
|
"from the <b>File</b> menu.</p>";
|
|
|
|
TQWhatsThis::<a href="ntqwhatsthis.html#add">add</a>( fileSave, fileSaveText );
|
|
const char * filePrintText = "Click this button to print the file you "
|
|
"are editing.\n"
|
|
"You can also select the Print command "
|
|
"from the File menu.";
|
|
|
|
TQWhatsThis::<a href="ntqwhatsthis.html#add">add</a>( filePrint, filePrintText );
|
|
</pre>
|
|
<p> The "What's This?" help of the remaining two buttons doesn't make use
|
|
of pixmaps, therefore all we need to do is to add the help-text to the
|
|
button. Be careful though: To invoke the rich-text elements in <tt>fileSaveText()</tt>, the entire string must be surrounded by <p> and
|
|
</p>. In <tt>filePrintText()</tt>, we don't have rich-text elements, so
|
|
this is not necessary.
|
|
<p> <pre> <a href="tqpopupmenu.html">TQPopupMenu</a> * file = new <a href="tqpopupmenu.html">TQPopupMenu</a>( this );
|
|
<a href="tqmainwindow.html#menuBar">menuBar</a>()->insertItem( "&File", file );
|
|
</pre>
|
|
<p> Next we create a <a href="tqpopupmenu.html">TQPopupMenu</a> for the <em>File</em> menu and
|
|
add it to the menu bar. With the ampersand in front of the letter F,
|
|
we allow the user to use the shortcut <em>Alt+F</em> to pop up this menu.
|
|
<p> <pre> file-><a href="tqmenudata.html#insertItem">insertItem</a>( "&New", this, TQ_SLOT(newDoc()), CTRL+Key_N );
|
|
</pre>
|
|
<p> Its first entry is connected to the (yet to be implemented) slot <tt>newDoc()</tt>. When the user chooses this <em>New</em> entry (e.g. by typing the
|
|
letter N as marked by the ampersand) or uses the
|
|
<em>Ctrl+N</em> accelerator, a new editor-window will pop up.
|
|
<p> <pre> int id;
|
|
id = file-><a href="tqmenudata.html#insertItem">insertItem</a>( openIcon, "&Open...",
|
|
this, TQ_SLOT(choose()), CTRL+Key_O );
|
|
file-><a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText );
|
|
|
|
id = file-><a href="tqmenudata.html#insertItem">insertItem</a>( saveIcon, "&Save",
|
|
this, TQ_SLOT(save()), CTRL+Key_S );
|
|
file-><a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
|
|
|
|
id = file-><a href="tqmenudata.html#insertItem">insertItem</a>( "Save &As...", this, TQ_SLOT(saveAs()) );
|
|
file-><a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText );
|
|
</pre>
|
|
<p> We populate the <em>File</em> menu with three more commands (<em>Open</em>, <em>Save</em> and
|
|
<em>Save As</em>), and set "What's This?" help for them. Note in particular
|
|
that "What's This?" help and pixmaps are used in both the toolbar (above)
|
|
and the menu bar (here). (See <a href="tqaction.html">TQAction</a> and the <tt>examples/action</tt>
|
|
example for a shorter and easier approach.)
|
|
<p> <pre> file-><a href="tqmenudata.html#insertSeparator">insertSeparator</a>();
|
|
</pre>
|
|
<p> Then we insert a separator, ...
|
|
<p> <pre> id = file-><a href="tqmenudata.html#insertItem">insertItem</a>( printIcon, "&Print...",
|
|
this, TQ_SLOT(print()), CTRL+Key_P );
|
|
file-><a href="tqmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText );
|
|
|
|
file-><a href="tqmenudata.html#insertSeparator">insertSeparator</a>();
|
|
|
|
file-><a href="tqmenudata.html#insertItem">insertItem</a>( "&Close", this, TQ_SLOT(<a href="tqwidget.html#close">close</a>()), CTRL+Key_W );
|
|
file-><a href="tqmenudata.html#insertItem">insertItem</a>( "&Quit", tqApp, TQ_SLOT( <a href="ntqapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q );
|
|
</pre>
|
|
<p> ... the <em>Print</em> command with "What's This?" help, another separator and
|
|
two more commands (<em>Close</em> and <em>Quit</em>) without "What's This?" and pixmaps.
|
|
In case of the <em>Close</em> command, the signal is connected
|
|
to the <em>close()</em> slot of the respective <em>ApplicationWindow</em> object whilst
|
|
the <em>Quit</em> command affects the entire application.
|
|
<p> Because <em>ApplicationWindow</em> is a <a href="tqwidget.html">TQWidget</a>, the <em>close()</em> function
|
|
triggers a call to <a href="#closeEvent">closeEvent()</a> which we
|
|
will implement later.
|
|
<p> <a name="common_constructor"></a>
|
|
<pre> <a href="tqmainwindow.html#menuBar">menuBar</a>()->insertSeparator();
|
|
</pre>
|
|
<p> Now that we have done the File menu we shift our focus back to the
|
|
menu bar and insert a separator. From now on further menu bar entries
|
|
will be aligned to the right if the windows system style requires it.
|
|
<p> <pre> <a href="tqpopupmenu.html">TQPopupMenu</a> * help = new <a href="tqpopupmenu.html">TQPopupMenu</a>( this );
|
|
<a href="tqmainwindow.html#menuBar">menuBar</a>()->insertItem( "&Help", help );
|
|
|
|
help-><a href="tqmenudata.html#insertItem">insertItem</a>( "&About", this, TQ_SLOT(about()), Key_F1 );
|
|
help-><a href="tqmenudata.html#insertItem">insertItem</a>( "About &TQt", this, TQ_SLOT(aboutTQt()) );
|
|
help-><a href="tqmenudata.html#insertSeparator">insertSeparator</a>();
|
|
help-><a href="tqmenudata.html#insertItem">insertItem</a>( "What's &This", this, TQ_SLOT(<a href="tqmainwindow.html#whatsThis">whatsThis</a>()), SHIFT+Key_F1 );
|
|
</pre>
|
|
<p> We create a <em>Help</em> menu, add it to the menu bar, and insert a few
|
|
commands. Depending on the style it will appear on the right hand
|
|
side of the menu bar or not.
|
|
<p> <pre> e = new <a href="tqtextedit.html">TQTextEdit</a>( this, "editor" );
|
|
e-><a href="tqwidget.html#setFocus">setFocus</a>();
|
|
<a href="tqmainwindow.html#setCentralWidget">setCentralWidget</a>( e );
|
|
</pre>
|
|
<p> Now we create a simple text-editor, set the initial focus to it,
|
|
and make it the window's central widget.
|
|
<p> <a href="tqmainwindow.html#centralWidget">TQMainWindow::centralWidget</a>() is the heart of the entire application:
|
|
It's what menu bar, statusbar and toolbars are all arranged around. Since
|
|
the central widget is a text editing widget, we can now reveal that
|
|
our simple application is a text editor. :)
|
|
<p> <pre> <a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Ready", 2000 );
|
|
</pre>
|
|
<p> We make the statusbar say "Ready" for two seconds at startup, just to
|
|
tell the user that the window has finished initialization and can be
|
|
used.
|
|
<p> <pre> <a href="tqwidget.html#resize">resize</a>( 450, 600 );
|
|
</pre>
|
|
<p> Finally it's time to resize the new window to a a nice default size.
|
|
<p> <pre> }
|
|
</pre>
|
|
<p> We have now finished with the constructor. Now we'll deal with the
|
|
destructor.
|
|
<p> <pre> ApplicationWindow::~ApplicationWindow()
|
|
{
|
|
delete printer;
|
|
}
|
|
</pre>
|
|
<p> The only thing an <em>ApplicationWindow</em> widget needs to do in its
|
|
destructor is to delete the printer it created. All other objects are
|
|
child widgets, which TQt will delete when appropriate.
|
|
<p> Now our task is to implement all the slots mentioned in the header file
|
|
and used in the constructor.
|
|
<p> <a name="newDoc()"></a>
|
|
<pre> void ApplicationWindow::newDoc()
|
|
{
|
|
ApplicationWindow *ed = new ApplicationWindow;
|
|
ed-><a href="tqwidget.html#setCaption">setCaption</a>("TQt Example - Application");
|
|
ed-><a href="tqwidget.html#show">show</a>();
|
|
}
|
|
</pre>
|
|
<p> This slot, connected to the <em>File|New</em> menu item, simply creates a
|
|
new <em>ApplicationWindow</em> and shows it.
|
|
<p> <a name="choose()"></a>
|
|
<pre> void ApplicationWindow::choose()
|
|
{
|
|
<a href="tqstring.html">TQString</a> fn = TQFileDialog::<a href="tqfiledialog.html#getOpenFileName">getOpenFileName</a>( TQString::null, TQString::null,
|
|
this);
|
|
if ( !fn.<a href="tqstring.html#isEmpty">isEmpty</a>() )
|
|
load( fn );
|
|
else
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Loading aborted", 2000 );
|
|
}
|
|
</pre>
|
|
<p> The <em>choose()</em> slot is connected to the <em>Open</em> menu item and
|
|
tool button. With a little help from <a href="tqfiledialog.html#getOpenFileName">TQFileDialog::getOpenFileName</a>(), it
|
|
asks the user for a file name and then either loads that file or gives an
|
|
error message in the statusbar.
|
|
<p> <pre> void ApplicationWindow::load( const <a href="tqstring.html">TQString</a> &fileName )
|
|
{
|
|
<a href="tqfile.html">TQFile</a> f( fileName );
|
|
if ( !f.<a href="tqfile.html#open">open</a>( <a href="tqfile.html#open">IO_ReadOnly</a> ) )
|
|
return;
|
|
|
|
<a href="tqtextstream.html">TQTextStream</a> ts( &f );
|
|
e-><a href="tqtextedit.html#setText">setText</a>( ts.<a href="tqtextstream.html#read">read</a>() );
|
|
e-><a href="tqtextedit.html#setModified">setModified</a>( FALSE );
|
|
<a href="tqwidget.html#setCaption">setCaption</a>( fileName );
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Loaded document " + fileName, 2000 );
|
|
}
|
|
</pre>
|
|
<p> This function loads a file into the editor. When it's done, it sets the
|
|
window system caption to the file name and displays a success message in
|
|
the statusbar for two seconds. With files that exist but are not
|
|
readable, nothing happens.
|
|
<p> <a name="save()"></a>
|
|
<pre> void ApplicationWindow::save()
|
|
{
|
|
if ( filename.isEmpty() ) {
|
|
saveAs();
|
|
return;
|
|
}
|
|
|
|
<a href="tqstring.html">TQString</a> text = e-><a href="tqtextedit.html#text">text</a>();
|
|
<a href="tqfile.html">TQFile</a> f( filename );
|
|
if ( !f.<a href="tqfile.html#open">open</a>( <a href="tqfile.html#open">IO_WriteOnly</a> ) ) {
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( TQString("Could not write to %1").arg(filename),
|
|
2000 );
|
|
return;
|
|
}
|
|
|
|
<a href="tqtextstream.html">TQTextStream</a> t( &f );
|
|
t << text;
|
|
f.<a href="tqfile.html#close">close</a>();
|
|
</pre>
|
|
<p> As its name suggests, this function saves the current file. If no
|
|
filename has been specified so far, the <a href="#saveAs()">saveAs()</a> function is called. Unwritable files cause the <em>ApplicationWindow</em> object to provide an error-message in the statusbar.
|
|
Note that there is more than one way to do this:
|
|
compare the above <tt>statusBar()->message()</tt> line with the equivalent
|
|
code in the <tt>load()</tt> function.
|
|
<p> <pre> e-><a href="tqtextedit.html#setModified">setModified</a>( FALSE );
|
|
</pre>
|
|
<p> Tell the editor that the contents haven't been edited since the last
|
|
save. When the user does some further editing and wishes to close the
|
|
window without explicit saving, <a href="#closeEvent">ApplicationWindow::closeEvent()</a> will ask about it.
|
|
<p> <pre> <a href="tqwidget.html#setCaption">setCaption</a>( filename );
|
|
</pre>
|
|
<p> It may be that the document was saved under a different name than the
|
|
old caption suggests, so we set the window caption just to be sure.
|
|
<p> <pre> <a href="tqmainwindow.html#statusBar">statusBar</a>()->message( TQString( "File %1 saved" ).arg( filename ), 2000 );
|
|
}
|
|
</pre>
|
|
<p> With a message in the statusbar, we inform the user that the file
|
|
was saved successfully.
|
|
<p> <a name="saveAs()"></a>
|
|
<pre> void ApplicationWindow::saveAs()
|
|
{
|
|
<a href="tqstring.html">TQString</a> fn = TQFileDialog::<a href="tqfiledialog.html#getSaveFileName">getSaveFileName</a>( TQString::null, TQString::null,
|
|
this );
|
|
if ( !fn.<a href="tqstring.html#isEmpty">isEmpty</a>() ) {
|
|
filename = fn;
|
|
save();
|
|
} else {
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Saving aborted", 2000 );
|
|
}
|
|
}
|
|
</pre>
|
|
<p> This function asks for a new name, saves the document under that name,
|
|
and implicitly changes the window system caption to the new name.
|
|
<p> <a name="printer"></a>
|
|
<p> <pre> void ApplicationWindow::print()
|
|
{
|
|
printer-><a href="ntqprinter.html#setFullPage">setFullPage</a>( TRUE );
|
|
if ( printer-><a href="ntqprinter.html#setup">setup</a>(this) ) { // printer dialog
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Printing..." );
|
|
<a href="ntqpainter.html">TQPainter</a> p;
|
|
if( !p.<a href="ntqpainter.html#begin">begin</a>( printer ) ) { // paint on printer
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Printing aborted", 2000 );
|
|
return;
|
|
}
|
|
|
|
<a href="ntqpaintdevicemetrics.html">TQPaintDeviceMetrics</a> metrics( p.<a href="ntqpainter.html#device">device</a>() );
|
|
int dpiy = metrics.<a href="ntqpaintdevicemetrics.html#logicalDpiY">logicalDpiY</a>();
|
|
int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
|
|
<a href="ntqrect.html">TQRect</a> view( margin, margin, metrics.<a href="ntqpaintdevicemetrics.html#width">width</a>() - 2*margin, metrics.<a href="ntqpaintdevicemetrics.html#height">height</a>() - 2*margin );
|
|
<a href="ntqsimplerichtext.html">TQSimpleRichText</a> richText( TQStyleSheet::<a href="tqstylesheet.html#convertFromPlainText">convertFromPlainText</a>(e-><a href="tqtextedit.html#text">text</a>()),
|
|
TQFont(),
|
|
e-><a href="tqtextedit.html#context">context</a>(),
|
|
e-><a href="tqtextedit.html#styleSheet">styleSheet</a>(),
|
|
e-><a href="tqtextedit.html#mimeSourceFactory">mimeSourceFactory</a>(),
|
|
view.<a href="ntqrect.html#height">height</a>() );
|
|
richText.<a href="ntqsimplerichtext.html#setWidth">setWidth</a>( &p, view.<a href="ntqrect.html#width">width</a>() );
|
|
int page = 1;
|
|
do {
|
|
richText.<a href="ntqsimplerichtext.html#draw">draw</a>( &p, margin, margin, view, colorGroup() );
|
|
view.<a href="ntqrect.html#moveBy">moveBy</a>( 0, view.<a href="ntqrect.html#height">height</a>() );
|
|
p.<a href="ntqpainter.html#translate">translate</a>( 0 , -view.<a href="ntqrect.html#height">height</a>() );
|
|
p.<a href="ntqpainter.html#drawText">drawText</a>( view.<a href="ntqrect.html#right">right</a>() - p.<a href="ntqpainter.html#fontMetrics">fontMetrics</a>().width( TQString::<a href="tqstring.html#number">number</a>( page ) ),
|
|
view.<a href="ntqrect.html#bottom">bottom</a>() + p.<a href="ntqpainter.html#fontMetrics">fontMetrics</a>().ascent() + 5, TQString::number( page ) );
|
|
if ( view.<a href="ntqrect.html#top">top</a>() - margin >= richText.<a href="ntqsimplerichtext.html#height">height</a>() )
|
|
break;
|
|
printer-><a href="ntqprinter.html#newPage">newPage</a>();
|
|
page++;
|
|
} while (TRUE);
|
|
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Printing completed", 2000 );
|
|
} else {
|
|
<a href="tqmainwindow.html#statusBar">statusBar</a>()->message( "Printing aborted", 2000 );
|
|
}
|
|
}
|
|
</pre>
|
|
<p> <em>print()</em> is called by the <em>File|Print</em> menu item and the <em>filePrint</em>
|
|
tool button.
|
|
<p> We present the user with the print setup dialog, and abandon printing
|
|
if they cancel.
|
|
<p> We create a <a href="ntqsimplerichtext.html">TQSimpleRichText</a> object and give it the text. This object
|
|
is able to format the text nicely as one long page. We achieve
|
|
pagination by printing one paper page's worth of text from the
|
|
TQSimpleRichText page at a time.
|
|
<p> Now let's see what happens when a user wishes to <em>close()</em>
|
|
an <em>ApplicationWindow</em>.
|
|
<p> <a name="closeEvent"></a>
|
|
<pre> void ApplicationWindow::<a href="tqwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">TQCloseEvent</a>* ce )
|
|
{
|
|
</pre>
|
|
<p> This event gets to process window system close events. A close event is
|
|
subtly different from a hide event: hide often means "iconify" whereas
|
|
close means that the window is going away for good.
|
|
<p> <pre> if ( !e-><a href="tqtextedit.html#isModified">isModified</a>() ) {
|
|
ce-><a href="qcloseevent.html#accept">accept</a>();
|
|
return;
|
|
}
|
|
</pre>
|
|
<p> If the text hasn't been edited, we just accept the event. The window
|
|
will be closed, and because we used the <em>WDestructiveClose</em> <a href="ntqt.html#WidgetFlags">widget flag</a> in the <a href="#ApplicationWindow"><i>ApplicationWindow()</i> constructor</a>,
|
|
the widget will be deleted.
|
|
<p> <pre> switch( TQMessageBox::<a href="ntqmessagebox.html#information">information</a>( this, "TQt Application Example",
|
|
"Do you want to save the changes"
|
|
" to the document?",
|
|
"Yes", "No", "Cancel",
|
|
0, 1 ) ) {
|
|
</pre>
|
|
<p> Otherwise we ask the user: What do you want to do?
|
|
<p> <pre> case 0:
|
|
save();
|
|
ce-><a href="qcloseevent.html#accept">accept</a>();
|
|
break;
|
|
</pre>
|
|
<p> If they want to save and then exit, we do that.
|
|
<p> <pre> case 1:
|
|
ce-><a href="qcloseevent.html#accept">accept</a>();
|
|
break;
|
|
</pre>
|
|
<p> If the user doesn't want to exit, we ignore the close event (there is
|
|
a chance that we can't block it but we try).
|
|
<p> <pre> case 2:
|
|
default: // just for sanity
|
|
ce-><a href="qcloseevent.html#ignore">ignore</a>();
|
|
break;
|
|
</pre>
|
|
<p> The last case -- the user wants to abandon the edits and exit -- is very
|
|
simple.
|
|
<p> <pre> }
|
|
}
|
|
</pre>
|
|
<p> Last but not least we implement the slots used by the help menu entries.
|
|
<p> <pre> void ApplicationWindow::about()
|
|
{
|
|
TQMessageBox::<a href="ntqmessagebox.html#about">about</a>( this, "TQt Application Example",
|
|
"This example demonstrates simple use of "
|
|
"TQMainWindow,\nTQMenuBar and TQToolBar.");
|
|
}
|
|
|
|
void ApplicationWindow::aboutTQt()
|
|
{
|
|
TQMessageBox::<a href="ntqmessagebox.html#aboutTQt">aboutTQt</a>( this, "TQt Application Example" );
|
|
}
|
|
</pre>
|
|
<p> These two slots use ready-made "about" functions to provide some
|
|
information about this program and the GUI toolkit it uses. (Although you
|
|
don't need to provide an About TQt in your programs, if you use TQt for free
|
|
we would appreciate it if you tell people what you're using.)
|
|
<p> That was all we needed to write a complete, almost useful application with
|
|
nice help-functions, almost as good as the "editors" some computer vendors
|
|
ship with their desktops, and in less than 300 lines of code!
|
|
<p> <p>See also <a href="step-by-step-examples.html">Step-by-step 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>TQt 3.3.8</div>
|
|
</table></div></address></body>
|
|
</html>
|