From ea318d1431c89e647598c510c4245c6571aa5f46 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 26 Jan 2012 23:32:43 -0600 Subject: Update to latest tqt3 automated conversion --- doc/html/simple-application.html | 254 +++++++++++++++++++-------------------- 1 file changed, 127 insertions(+), 127 deletions(-) (limited to 'doc/html/simple-application.html') diff --git a/doc/html/simple-application.html b/doc/html/simple-application.html index f9d0a045a..fd1d669d9 100644 --- a/doc/html/simple-application.html +++ b/doc/html/simple-application.html @@ -34,11 +34,11 @@ body { background: #ffffff; color: black; }

-

This walkthrough shows simple use of TQMainWindow, TQMenuBar, TQPopupMenu, TQToolBar and TQStatusBar - classes that every +

This walkthrough shows simple use of TQMainWindow, TQMenuBar, TQPopupMenu, TQToolBar and TQStatusBar - classes that every modern application window tends to use. (See also Tutorial #2.) -

It also illustrates some aspects of TQWhatsThis (for simple help) and a -typical main() using TQApplication. -

Finally, it shows a typical print function based on TQPrinter. +

It also illustrates some aspects of TQWhatsThis (for simple help) and a +typical main() using TQApplication. +

Finally, it shows a typical print function based on TQPrinter.

The declaration of ApplicationWindow

Here's the header file in full: @@ -55,11 +55,11 @@ typical main() using TQApplication. #ifndef APPLICATION_H #define APPLICATION_H -#include <qmainwindow.h> +#include <ntqmainwindow.h> class TQTextEdit; -class ApplicationWindow: public TQMainWindow +class ApplicationWindow: public TQMainWindow { Q_OBJECT @@ -73,7 +73,7 @@ protected: private slots: void newDoc(); void choose(); - void load( const TQString &fileName ); + void load( const TQString &fileName ); void save(); void saveAs(); void print(); @@ -82,20 +82,20 @@ private slots: void aboutTQt(); private: - TQPrinter *printer; - TQTextEdit *e; - TQString filename; + TQPrinter *printer; + TQTextEdit *e; + TQString filename; }; #endif -

It declares a class that inherits TQMainWindow, with slots and private -variables. The class pre-declaration of TQTextEdit at the beginning +

It declares a class that inherits TQMainWindow, with slots and private +variables. The class pre-declaration of TQTextEdit at the beginning (instead of an include) helps to speed up compilation. With this trick, make depend won't insist on recompiling every .cpp file that -includes application.h when qtextedit.h changes. +includes application.h when ntqtextedit.h changes.

A simple main()

@@ -110,16 +110,16 @@ includes application.h when qtextedit.h ** *****************************************************************************/ -#include <qapplication.h> +#include <ntqapplication.h> #include "application.h" int main( int argc, char ** argv ) { - TQApplication a( argc, argv ); + TQApplication a( argc, argv ); ApplicationWindow *mw = new ApplicationWindow(); - mw->setCaption( "TQt Example - Application" ); - mw->show(); - a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) ); - return a.exec(); + mw->setCaption( "TQt Example - Application" ); + mw->show(); + a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) ); + return a.exec(); } @@ -127,25 +127,25 @@ int main( int argc, char ** argv ) {

    int main( int argc, char ** argv ) {
-        TQApplication a( argc, argv );
+        TQApplication a( argc, argv );
 
-

With the above line, we create a TQApplication object with the usual +

With the above line, we create a TQApplication object with the usual constructor and let it parse argc and argv. TQApplication itself takes care of X11-specific command-line options like -geometry, so the program will automatically behave the way X clients are expected to.

        ApplicationWindow *mw = new ApplicationWindow();
-        mw->setCaption( "TQt Example - Application" );
-        mw->show();
+        mw->setCaption( "TQt Example - Application" );
+        mw->show();
 

We create an ApplicationWindow as a top-level widget, set its window system caption to "Document 1", and show() it.

-

        a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
+
        a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
 

When the application's last window is closed, it should quit. Both -the signal and the slot are predefined members of TQApplication. -

        return a.exec();
+the signal and the slot are predefined members of TQApplication.
+

        return a.exec();
 

Having completed the application's initialization, we start the main event loop (the GUI), and eventually return the error code @@ -171,32 +171,32 @@ 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.

    ApplicationWindow::ApplicationWindow()
-        : TQMainWindow( 0, "example application main window", WDestructiveClose | WGroupLeader )
+        : TQMainWindow( 0, "example application main window", WDestructiveClose | WGroupLeader )
     {
 
-

ApplicationWindow inherits TQMainWindow, the TQt class that provides +

ApplicationWindow inherits TQMainWindow, the TQt class that provides typical application main windows, with menu bars, toolbars, etc. -

        printer = new TQPrinter( TQPrinter::HighResolution );
+

        printer = new TQPrinter( TQPrinter::HighResolution );
 

The application example can print things, and we chose to have a -TQPrinter object lying around so that when the user changes a setting +TQPrinter object lying around so that when the user changes a setting during one printing, the new setting will be the default next time. -

        TQPixmap openIcon, saveIcon, printIcon;
+

        TQPixmap openIcon, saveIcon, printIcon;
 

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. -

        TQToolBar * fileTools = new TQToolBar( this, "file operations" );
+

        TQToolBar * fileTools = new TQToolBar( this, "file operations" );
 

We create a toolbar in this window ... -

        fileTools->setLabel( "File Operations" );
+

        fileTools->setLabel( "File Operations" );
 

... 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.

        openIcon = TQPixmap( fileopen );
         TQToolButton * fileOpen
-            = new TQToolButton( openIcon, "Open File", TQString::null,
+            = new TQToolButton( openIcon, "Open File", TQString::null,
                                this, SLOT(choose()), fileTools, "open file" );
 

Now we create the first tool button for the fileTools toolbar @@ -206,12 +206,12 @@ contains the definition of a pixmap called fileopen. We use this icon to illustrate our first tool button.

        saveIcon = TQPixmap( filesave );
         TQToolButton * fileSave
-            = new TQToolButton( saveIcon, "Save File", TQString::null,
+            = new TQToolButton( saveIcon, "Save File", TQString::null,
                                this, SLOT(save()), fileTools, "save file" );
 
         printIcon = TQPixmap( fileprint );
         TQToolButton * filePrint
-            = new TQToolButton( printIcon, "Print File", TQString::null,
+            = new TQToolButton( printIcon, "Print File", TQString::null,
                                this, SLOT(print()), fileTools, "print file" );
 

In a similar way we create two more tool buttons in this toolbar, each with @@ -228,7 +228,7 @@ function, as its mouse interface is unusual. "You can also select the <b>Open</b> command " "from the <b>File</b> menu.</p>"; - TQWhatsThis::add( fileOpen, fileOpenText ); + TQWhatsThis::add( fileOpen, fileOpenText );

With the above line we add the "What's This?" help-text to the fileOpen button... @@ -241,100 +241,100 @@ saved in fileOpenText) requests an image named "fileopen", the open "You can also select the <b>Save</b> command " "from the <b>File</b> menu.</p>"; - TQWhatsThis::add( fileSave, fileSaveText ); + TQWhatsThis::add( 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::add( filePrint, filePrintText ); + TQWhatsThis::add( filePrint, filePrintText );

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 fileSaveText(), the entire string must be surrounded by <p> and </p>. In filePrintText(), we don't have rich-text elements, so this is not necessary. -

        TQPopupMenu * file = new TQPopupMenu( this );
-        menuBar()->insertItem( "&File", file );
+

        TQPopupMenu * file = new TQPopupMenu( this );
+        menuBar()->insertItem( "&File", file );
 
-

Next we create a TQPopupMenu for the File menu and +

Next we create a TQPopupMenu for the File 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 Alt+F to pop up this menu. -

        file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N );
+

        file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N );
 

Its first entry is connected to the (yet to be implemented) slot newDoc(). When the user chooses this New entry (e.g. by typing the letter N as marked by the ampersand) or uses the Ctrl+N accelerator, a new editor-window will pop up.

        int id;
-        id = file->insertItem( openIcon, "&Open...",
+        id = file->insertItem( openIcon, "&Open...",
                                this, SLOT(choose()), CTRL+Key_O );
-        file->setWhatsThis( id, fileOpenText );
+        file->setWhatsThis( id, fileOpenText );
 
-        id = file->insertItem( saveIcon, "&Save",
+        id = file->insertItem( saveIcon, "&Save",
                                this, SLOT(save()), CTRL+Key_S );
-        file->setWhatsThis( id, fileSaveText );
+        file->setWhatsThis( id, fileSaveText );
 
-        id = file->insertItem( "Save &As...", this, SLOT(saveAs()) );
-        file->setWhatsThis( id, fileSaveText );
+        id = file->insertItem( "Save &As...", this, SLOT(saveAs()) );
+        file->setWhatsThis( id, fileSaveText );
 

We populate the File menu with three more commands (Open, Save and Save As), 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 TQAction and the examples/action +and the menu bar (here). (See TQAction and the examples/action example for a shorter and easier approach.) -

        file->insertSeparator();
+

        file->insertSeparator();
 

Then we insert a separator, ... -

        id = file->insertItem( printIcon, "&Print...",
+

        id = file->insertItem( printIcon, "&Print...",
                                this, SLOT(print()), CTRL+Key_P );
-        file->setWhatsThis( id, filePrintText );
+        file->setWhatsThis( id, filePrintText );
 
-        file->insertSeparator();
+        file->insertSeparator();
 
-        file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W );
-        file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );
+        file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W );
+        file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );
 

... the Print command with "What's This?" help, another separator and two more commands (Close and Quit) without "What's This?" and pixmaps. In case of the Close command, the signal is connected to the close() slot of the respective ApplicationWindow object whilst the Quit command affects the entire application. -

Because ApplicationWindow is a TQWidget, the close() function +

Because ApplicationWindow is a TQWidget, the close() function triggers a call to closeEvent() which we will implement later.

-

        menuBar()->insertSeparator();
+
        menuBar()->insertSeparator();
 

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. -

        TQPopupMenu * help = new TQPopupMenu( this );
-        menuBar()->insertItem( "&Help", help );
+

        TQPopupMenu * help = new TQPopupMenu( this );
+        menuBar()->insertItem( "&Help", help );
 
-        help->insertItem( "&About", this, SLOT(about()), Key_F1 );
-        help->insertItem( "About &TQt", this, SLOT(aboutTQt()) );
-        help->insertSeparator();
-        help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 );
+        help->insertItem( "&About", this, SLOT(about()), Key_F1 );
+        help->insertItem( "About &TQt", this, SLOT(aboutTQt()) );
+        help->insertSeparator();
+        help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 );
 

We create a Help 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. -

        e = new TQTextEdit( this, "editor" );
-        e->setFocus();
-        setCentralWidget( e );
+

        e = new TQTextEdit( this, "editor" );
+        e->setFocus();
+        setCentralWidget( e );
 

Now we create a simple text-editor, set the initial focus to it, and make it the window's central widget. -

TQMainWindow::centralWidget() is the heart of the entire application: +

TQMainWindow::centralWidget() 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. :) -

        statusBar()->message( "Ready", 2000 );
+

        statusBar()->message( "Ready", 2000 );
 

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. -

        resize( 450, 600 );
+

        resize( 450, 600 );
 

Finally it's time to resize the new window to a a nice default size.

    }
@@ -355,8 +355,8 @@ and used in the constructor.
 
    void ApplicationWindow::newDoc()
     {
         ApplicationWindow *ed = new ApplicationWindow;
-        ed->setCaption("TQt Example - Application");
-        ed->show();
+        ed->setCaption("TQt Example - Application");
+        ed->show();
     }
 

This slot, connected to the File|New menu item, simply creates a @@ -364,29 +364,29 @@ new ApplicationWindow and shows it.

    void ApplicationWindow::choose()
     {
-        TQString fn = TQFileDialog::getOpenFileName( TQString::null, TQString::null,
+        TQString fn = TQFileDialog::getOpenFileName( TQString::null, TQString::null,
                                                    this);
-        if ( !fn.isEmpty() )
+        if ( !fn.isEmpty() )
             load( fn );
         else
-            statusBar()->message( "Loading aborted", 2000 );
+            statusBar()->message( "Loading aborted", 2000 );
     }
 

The choose() slot is connected to the Open menu item and -tool button. With a little help from TQFileDialog::getOpenFileName(), it +tool button. With a little help from TQFileDialog::getOpenFileName(), it asks the user for a file name and then either loads that file or gives an error message in the statusbar. -

    void ApplicationWindow::load( const TQString &fileName )
+

    void ApplicationWindow::load( const TQString &fileName )
     {
-        TQFile f( fileName );
-        if ( !f.open( IO_ReadOnly ) )
+        TQFile f( fileName );
+        if ( !f.open( IO_ReadOnly ) )
             return;
 
-        TQTextStream ts( &f );
-        e->setText( ts.read() );
-        e->setModified( FALSE );
-        setCaption( fileName );
-        statusBar()->message( "Loaded document " + fileName, 2000 );
+        TQTextStream ts( &f );
+        e->setText( ts.read() );
+        e->setModified( FALSE );
+        setCaption( fileName );
+        statusBar()->message( "Loaded document " + fileName, 2000 );
     }
 

This function loads a file into the editor. When it's done, it sets the @@ -401,33 +401,33 @@ readable, nothing happens. return; } - TQString text = e->text(); - TQFile f( filename ); - if ( !f.open( IO_WriteOnly ) ) { - statusBar()->message( TQString("Could not write to %1").arg(filename), + TQString text = e->text(); + TQFile f( filename ); + if ( !f.open( IO_WriteOnly ) ) { + statusBar()->message( TQString("Could not write to %1").arg(filename), 2000 ); return; } - TQTextStream t( &f ); + TQTextStream t( &f ); t << text; - f.close(); + f.close();

As its name suggests, this function saves the current file. If no filename has been specified so far, the saveAs() function is called. Unwritable files cause the ApplicationWindow object to provide an error-message in the statusbar. Note that there is more than one way to do this: compare the above statusBar()->message() line with the equivalent code in the load() function. -

        e->setModified( FALSE );
+

        e->setModified( FALSE );
 

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, ApplicationWindow::closeEvent() will ask about it. -

        setCaption( filename );
+

        setCaption( filename );
 

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. -

        statusBar()->message( TQString( "File %1 saved" ).arg( filename ), 2000 );
+

        statusBar()->message( TQString( "File %1 saved" ).arg( filename ), 2000 );
     }
 

With a message in the statusbar, we inform the user that the file @@ -435,13 +435,13 @@ was saved successfully.

    void ApplicationWindow::saveAs()
     {
-        TQString fn = TQFileDialog::getSaveFileName( TQString::null, TQString::null,
+        TQString fn = TQFileDialog::getSaveFileName( TQString::null, TQString::null,
                                                    this );
-        if ( !fn.isEmpty() ) {
+        if ( !fn.isEmpty() ) {
             filename = fn;
             save();
         } else {
-            statusBar()->message( "Saving aborted", 2000 );
+            statusBar()->message( "Saving aborted", 2000 );
         }
     }
 
@@ -450,42 +450,42 @@ and implicitly changes the window system caption to the new name.

    void ApplicationWindow::print()
     {
-        printer->setFullPage( TRUE );
-        if ( printer->setup(this) ) {               // printer dialog
-            statusBar()->message( "Printing..." );
-            TQPainter p;
-            if( !p.begin( printer ) ) {               // paint on printer
-                statusBar()->message( "Printing aborted", 2000 );
+        printer->setFullPage( TRUE );
+        if ( printer->setup(this) ) {               // printer dialog
+            statusBar()->message( "Printing..." );
+            TQPainter p;
+            if( !p.begin( printer ) ) {               // paint on printer
+                statusBar()->message( "Printing aborted", 2000 );
                 return;
             }
 
-            TQPaintDeviceMetrics metrics( p.device() );
-            int dpiy = metrics.logicalDpiY();
+            TQPaintDeviceMetrics metrics( p.device() );
+            int dpiy = metrics.logicalDpiY();
             int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
-            TQRect view( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
-            TQSimpleRichText richText( TQStyleSheet::convertFromPlainText(e->text()),
+            TQRect view( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
+            TQSimpleRichText richText( TQStyleSheet::convertFromPlainText(e->text()),
                                       TQFont(),
-                                      e->context(),
-                                      e->styleSheet(),
-                                      e->mimeSourceFactory(),
-                                      view.height() );
-            richText.setWidth( &p, view.width() );
+                                      e->context(),
+                                      e->styleSheet(),
+                                      e->mimeSourceFactory(),
+                                      view.height() );
+            richText.setWidth( &p, view.width() );
             int page = 1;
             do {
-                richText.draw( &p, margin, margin, view, colorGroup() );
-                view.moveBy( 0, view.height() );
-                p.translate( 0 , -view.height() );
-                p.drawText( view.right() - p.fontMetrics().width( TQString::number( page ) ),
-                            view.bottom() + p.fontMetrics().ascent() + 5, TQString::number( page ) );
-                if ( view.top() - margin >= richText.height() )
+                richText.draw( &p, margin, margin, view, colorGroup() );
+                view.moveBy( 0, view.height() );
+                p.translate( 0 , -view.height() );
+                p.drawText( view.right() - p.fontMetrics().width( TQString::number( page ) ),
+                            view.bottom() + p.fontMetrics().ascent() + 5, TQString::number( page ) );
+                if ( view.top() - margin >= richText.height() )
                     break;
-                printer->newPage();
+                printer->newPage();
                 page++;
             } while (TRUE);
 
-            statusBar()->message( "Printing completed", 2000 );
+            statusBar()->message( "Printing completed", 2000 );
         } else {
-            statusBar()->message( "Printing aborted", 2000 );
+            statusBar()->message( "Printing aborted", 2000 );
         }
     }
 
@@ -493,28 +493,28 @@ and implicitly changes the window system caption to the new name. tool button.

We present the user with the print setup dialog, and abandon printing if they cancel. -

We create a TQSimpleRichText object and give it the text. This object +

We create a TQSimpleRichText 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.

Now let's see what happens when a user wishes to close() an ApplicationWindow.

-

    void ApplicationWindow::closeEvent( TQCloseEvent* ce )
+
    void ApplicationWindow::closeEvent( TQCloseEvent* ce )
     {
 

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. -

        if ( !e->isModified() ) {
+

        if ( !e->isModified() ) {
             ce->accept();
             return;
         }
 

If the text hasn't been edited, we just accept the event. The window -will be closed, and because we used the WDestructiveClose widget flag in the ApplicationWindow() constructor, +will be closed, and because we used the WDestructiveClose widget flag in the ApplicationWindow() constructor, the widget will be deleted. -

        switch( TQMessageBox::information( this, "TQt Application Example",
+

        switch( TQMessageBox::information( this, "TQt Application Example",
                                           "Do you want to save the changes"
                                           " to the document?",
                                           "Yes", "No", "Cancel",
@@ -546,14 +546,14 @@ simple.
 

Last but not least we implement the slots used by the help menu entries.

    void ApplicationWindow::about()
     {
-        TQMessageBox::about( this, "TQt Application Example",
+        TQMessageBox::about( this, "TQt Application Example",
                             "This example demonstrates simple use of "
                             "TQMainWindow,\nTQMenuBar and TQToolBar.");
     }
 
     void ApplicationWindow::aboutTQt()
     {
-        TQMessageBox::aboutTQt( this, "TQt Application Example" );
+        TQMessageBox::aboutTQt( this, "TQt Application Example" );
     }
 

These two slots use ready-made "about" functions to provide some -- cgit v1.2.3