Classes - Annotated - Tree - Functions - Home - Structure

A Tiny Example Featuring QActionGroup

This example program shows how to use an exclusive action group.

Detailed explanations of the code can be found in the walkthrough.


Main:

/*
$Id$
*/

#include <qapplication.h>

#include "editor.h"

int main( int argc, char ** argv)
{
    QApplication app( argc, argv );
    Editor * editor = new Editor;

    app.setMainWidget( editor );
    editor->show();
    return app.exec();
}


Header file:

/*
$Id$
*/

#ifndef EDITOR_H
#define EDITOR_H

#include <qmainwindow.h>

class QTextEdit;
class QAction;

class Editor : public QMainWindow
{
    Q_OBJECT

public:
    Editor();

private slots:
    void setFontColor( QAction * );

private:
    QTextEdit * editor;
    QAction * setRedFont;
};

#endif


Implementation:

/*
$Id$
*/

#include "editor.h"

#include <qtextedit.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qtoolbar.h>
#include <qaction.h>

Editor::Editor()
    : QMainWindow( 0, "main window")
{
    QActionGroup * colors = new QActionGroup( this, "colors", TRUE );

    QAction * setBlackFont = new QAction( "black", QPixmap( "black.xpm" ),
                                          "Font color: black", CTRL+Key_B,
                                          colors, "blackfontcolor", TRUE );
    setRedFont = new QAction( "red", QPixmap( "red.xpm" ), "Font color: red",
                              CTRL+Key_R, colors, "redfontcolor", TRUE );

    QObject::connect( colors, SIGNAL( selected( QAction * ) ),
                      this, SLOT( setFontColor( QAction * ) ) );

    QToolBar * toolbar = new QToolBar( this, "toolbar" );
    colors->addTo( toolbar );

    QPopupMenu * font = new QPopupMenu( this );
    menuBar()->insertItem( "&Font", font );

    colors->setUsesDropDown( TRUE );
    colors->setMenuText( "Font Color" );

    colors->addTo( font );

    editor = new QTextEdit( this, "editor" );
    setCentralWidget( editor );
}


void Editor::setFontColor( QAction * coloraction )
{
    if ( coloraction == setRedFont )
        editor->setColor( red );
    else
        editor->setColor( black );
}


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta2