返回介绍

A Tiny Example Featuring QActionGroup

发布于 2019-10-04 14:57:35 字数 4881 浏览 1149 评论 0 收藏 0

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;
    editor.setCaption( "Qt Example - Actiongroup" );
    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$
*/

/* XPM */
static const char * black_xpm[] = {
"32 32 2 1",
"       c None",
".      c #020202",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................"};

/* XPM */
static const char * red_xpm[] = {
"32 32 6 1",
"       c None",
".      c #EE0928",
"+      c #EF0928",
"@      c #EE0A29",
"#      c #EE0B2A",
"$      c #ED0C2B",
"........................+.......",
".+.++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++.+++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
"+++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++.+++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++.+++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++@",
".+++++++++++++++++++++++++++++.#",
".+++++++++++++++++++++++++.+++.$",
".+++++++++++++++++++++++++++++#$",
".+++++++++++++++++++++++++++++.#",
".+++++++++++++++++++++++++++++.#",
".++++++++.+++++++++++++++++++++@",
".++++++.+++++++++++++++++++++++.",
".++++++++++++++++++++++++++++++.",
"..........+.............+......."};


#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( (const char**)black_xpm ),
                                          "Font color: black", CTRL+Key_B,
                                          colors, "blackfontcolor", TRUE );
    setRedFont = new QAction( "red", QPixmap( (const char**)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 );
}

See also QAction Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文