Menus Example (ActiveQt)▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"menus.h"
#include <QAction>
#include <QAxFactory>
#include <QMenuBar>
#include <QMessageBox>
#include <QTextEdit>
#include <QPixmap>
#include
"fileopen.xpm"
#include
"filesave.xpm"
QMenus::
QMenus(QWidget *
parent)
:
QMainWindow(parent, 0
) // QMainWindow's default flag is WType_TopLevel
{
QAction *
action;
QMenu *
file =
new
QMenu(this
);
action =
new
QAction(QPixmap((const
char
**
)fileopen), tr("&Open"
), this
);
action-&
gt;setShortcut(tr("CTRL+O"
));
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
fileOpen);
file-&
gt;addAction(action);
action =
new
QAction(QPixmap((const
char
**
)filesave), tr("&Save"
), this
);
action-&
gt;setShortcut(tr("CTRL+S"
));
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
fileSave);
file-&
gt;addAction(action);
QMenu *
edit =
new
QMenu(this
);
action =
new
QAction(tr("&Normal"
), this
);
action-&
gt;setShortcut(tr("CTRL+N"
));
action-&
gt;setToolTip(tr("Normal"
));
action-&
gt;setStatusTip(tr("Toggles Normal"
));
action-&
gt;setCheckable(true
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
editNormal);
edit-&
gt;addAction(action);
action =
new
QAction(tr("&Bold"
), this
);
action-&
gt;setShortcut(tr("CTRL+B"
));
action-&
gt;setCheckable(true
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
editBold);
edit-&
gt;addAction(action);
action =
new
QAction(tr("&Underline"
), this
);
action-&
gt;setShortcut(tr("CTRL+U"
));
action-&
gt;setCheckable(true
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
editUnderline);
edit-&
gt;addAction(action);
QMenu *
advanced =
new
QMenu(this
);
action =
new
QAction(tr("&Font..."
), this
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
editAdvancedFont);
advanced-&
gt;addAction(action);
action =
new
QAction(tr("&Style..."
), this
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
editAdvancedStyle);
advanced-&
gt;addAction(action);
edit-&
gt;addMenu(advanced)-&
gt;setText(tr("&Advanced"
));
edit-&
gt;addSeparator();
action =
new
QAction(tr("Una&vailable"
), this
);
action-&
gt;setShortcut(tr("CTRL+V"
));
action-&
gt;setCheckable(true
);
action-&
gt;setEnabled(false
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
editUnderline);
edit-&
gt;addAction(action);
QMenu *
help =
new
QMenu(this
);
action =
new
QAction(tr("&About..."
), this
);
action-&
gt;setShortcut(tr("F1"
));
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
helpAbout);
help-&
gt;addAction(action);
action =
new
QAction(tr("&About Qt..."
), this
);
connect(action, &
amp;QAction::
triggered, this
, &
amp;QMenus::
helpAboutQt);
help-&
gt;addAction(action);
if
(!
QAxFactory::
isServer())
menuBar()-&
gt;addMenu(file)-&
gt;setText(tr("&File"
));
menuBar()-&
gt;addMenu(edit)-&
gt;setText(tr("&Edit"
));
menuBar()-&
gt;addMenu(help)-&
gt;setText(tr("&Help"
));
m_editor =
new
QTextEdit(this
);
setCentralWidget(m_editor);
statusBar();
}
void
QMenus::
fileOpen()
{
m_editor-&
gt;append(tr("File Open selected."
));
}
void
QMenus::
fileSave()
{
m_editor-&
gt;append(tr("File Save selected."
));
}
void
QMenus::
editNormal()
{
m_editor-&
gt;append(tr("Edit Normal selected."
));
}
void
QMenus::
editBold()
{
m_editor-&
gt;append(tr("Edit Bold selected."
));
}
void
QMenus::
editUnderline()
{
m_editor-&
gt;append(tr("Edit Underline selected."
));
}
void
QMenus::
editAdvancedFont()
{
m_editor-&
gt;append(tr("Edit Advanced Font selected."
));
}
void
QMenus::
editAdvancedStyle()
{
m_editor-&
gt;append(tr("Edit Advanced Style selected."
));
}
void
QMenus::
helpAbout()
{
QMessageBox::
about(this
, tr("About QMenus"
),
tr("This example implements an in-place ActiveX control with menus and status messages."
));
}
void
QMenus::
helpAboutQt()
{
QMessageBox::
aboutQt(this
);
}