Qt Widgets——动作类与小部件菜单项

时间:2024-03-24 23:03:32

本文主要涉及以下三个类:

QAction

——QWidgetAction

QActionGroup

QAction可称为动作类,它一般可当作菜单中的项组成菜单,也可作为工具栏上的按钮,它主要由图标、文本及快捷键三部分组成。QActionGroup用于将QAction分组,设置组内各QAction的互斥性质(exclusive ),设置后,组内的动作,在外观上形成多选框(不互斥)或单选框(互斥)。QWidgetAction继承自QAction,它可将自定义的小部件插入到菜单项中,用于QSystemTrayIcon(托盘图标)的右键菜单似乎很合适。

QAction

公有函数如下:

 QAction(QObject * parent)
QAction(const QString & text, QObject * parent)
QAction(const QIcon & icon, const QString & text, QObject * parent)
~QAction() void setText(const QString & text)//设置文本
void setSeparator(bool b)//设成分割线,为深灰色的线,用于分割功能区
void setStatusTip(const QString & statusTip)//鼠标放至图标上时,状态栏提示文本
void setToolTip(const QString & tip)//鼠标放到上方时显示的提示文本
void setWhatsThis(const QString & what)
void setIcon(const QIcon & icon)//设置图标
void setIconText(const QString & text)//如果没有setText(),则这个会显示
void setIconVisibleInMenu(bool visible)//默认图标在菜单中是可见的
void setMenu(QMenu * menu)//本action形成右方有小箭头的子菜单
void setMenuRole(MenuRole menuRole)//菜单角色设定,由于Mac OS X系统的一些特性,Qt 会对一些菜单项进行自动排列。比如,如果你的菜单是“关于”、“设置”、“首选项”、“退出”等等,我们可以给它们分配一个角色,Qt 则会根据这些角色对菜单项的顺序作出正确的排列。枚举值见1
void setCheckable(bool)//设置是否可以勾选
void setData(const QVariant & userData)//
void setFont(const QFont & font)
void setPriority(Priority priority)//设置优先级,该优先级用于表明action在用户界面上的优先级。如,当工具栏设置了Qt::ToolButtonTextBesideIcon模式,那么低优先级的actions将不会显示出标签。枚举值见2
void setActionGroup(QActionGroup * group)//将本动作添加到某组
void setAutoRepeat(bool)//当长按快捷键时,是否不断触发,默认是
void setShortcut(const QKeySequence & shortcut)//设置快捷键
void setShortcutContext(Qt::ShortcutContext context)
void setShortcuts(const QList<QKeySequence> & shortcuts)
void setShortcuts(QKeySequence::StandardKey key) QActionGroup * actionGroup() const
void activate(ActionEvent event)
QList<QGraphicsWidget *> associatedGraphicsWidgets() const
QList<QWidget *> associatedWidgets() const
bool autoRepeat() const
QVariant data() const
QFont font() const
QIcon icon() const
QString iconText() const
bool isCheckable() const
bool isChecked() const
bool isEnabled() const
bool isIconVisibleInMenu() const
bool isSeparator() const
bool isVisible() const
QMenu * menu() const
MenuRole menuRole() const
QWidget * parentWidget() const
Priority priority() const
QKeySequence shortcut() const
Qt::ShortcutContext shortcutContext() const
QList<QKeySequence> shortcuts() const
bool showStatusText(QWidget * widget = 0)
QString statusTip() const
QString text() const
QString toolTip() const
QString whatsThis() const

Public Slots

void hover()
void setChecked(bool)//只有设为true,才能选中
void setDisabled(bool b)
void setEnabled(bool)
void setVisible(bool)
void toggle()
void trigger()

Signals

void changed()
void hovered()
void toggled(bool checked)//切换
void triggered(bool checked = false)//触发

1,enum QAction::MenuRole

Constant Value Description
QAction::NoRole 0 This action should not be put into the application menu
QAction::TextHeuristicRole 1 This action should be put in the application menu based on the action's text as described in the QMenuBar documentation.
QAction::ApplicationSpecificRole 2 This action should be put in the application menu with an application specific role
QAction::AboutQtRole 3 This action matches handles the "About Qt" menu item.
QAction::AboutRole 4 This action should be placed where the "About" menu item is in the application menu. The text of the menu item will be set to "About <application name>". The application name is fetched from the Info.plistfile in the application's bundle (See Deploying an Application on Mac OS X).
QAction::PreferencesRole 5 This action should be placed where the "Preferences..." menu item is in the application menu.
QAction::QuitRole 6 This action should be placed where the Quit menu item is in the application menu.

2,enum QAction::Priority

Constant Value Description
QAction::LowPriority 0 低优先级
QAction::NormalPriority 128
QAction::HighPriority 256

————————————————————————————————————————————————————————————————————

QWidgetAction

公有函数如下:

 QWidgetAction(QObject * parent)
virtual ~QWidgetAction()
QWidget * defaultWidget() const
void releaseWidget(QWidget * widget)
QWidget * requestWidget(QWidget * parent)
void setDefaultWidget(QWidget * widget)//主要用这个设置自定义部件

————————————————————————————————————————————————————————————————————

QActionGroup

   QActionGroup(QObject * parent)
~QActionGroup()
QList<QAction *> actions() const
QAction * addAction(QAction * action)//返回的是原action
QAction * addAction(const QString & text)
QAction * addAction(const QIcon & icon, const QString & text)
QAction * checkedAction() const//返回当前被选中的action
bool isEnabled() const
bool isExclusive() const
bool isVisible() const
void removeAction(QAction * action)

Public Slots

void setDisabled(bool b)
void setEnabled(bool)
void setExclusive(bool)//设置互斥性,默认互斥
void setVisible(bool)

Signals

void hovered(QAction * action)
void triggered(QAction * action)

————————————————————————————————————————————————————————————————————

测试示例,主菜单文件中测试了QActionGroup各函数效果,工具栏测试QAction各函数,系统托盘中测试QWidgetAction,加了个列表进去。

效果如图:

Qt Widgets——动作类与小部件菜单项Qt Widgets——动作类与小部件菜单项Qt Widgets——动作类与小部件菜单项

代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QListWidget>
#include <QWidgetAction>
#include <QAction>
#include <QMenu>
#include <QIcon>
#include <QKeySequence>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
menu=new QMenu("菜单");
menu->addAction("子菜单");
//QAction
ac1=new QAction(this);
ac1->setText("setText");
//ac1->setSeparator(true);
ac1->setStatusTip("setStatusTip");
ac1->setToolTip("setToolTip");
ac1->setWhatsThis("setWhatsThis");
ac1->setIcon(QIcon("://tray.ico"));
ac1->setIconText("setIconText");
ac1->setMenu(menu);
ac1->setCheckable(true);
ac1->setData(QString("setData"));
ac1->setPriority(QAction::HighPriority);
ac1->setShortcut(QKeySequence(tr("Ctrl+P"))); ui->mainToolBar->addAction(ac1);
//QActionGroup
main_ac1=new QAction("左对齐",this);//创建动作
main_ac2=new QAction("右对齐",this);
main_ac3=new QAction("居中对齐",this);
main_ac1->setCheckable(true);//设置是否可选中
main_ac2->setCheckable(true);
main_ac3->setCheckable(true);
QActionGroup *alignmentGroup = new QActionGroup(this);
alignmentGroup->addAction(main_ac1);//将动作加入组
alignmentGroup->addAction(main_ac2);
alignmentGroup->addAction(main_ac3);
main_ac2->setChecked(true);//设置一个默认选中的 QActionGroup *Group = new QActionGroup(this);
QAction *ac_1 = Group->addAction("粗体");
QAction *ac_2 = Group->addAction("倾斜");
ac_1->setCheckable(true);
ac_2->setCheckable(true);
Group->setExclusive(false);
//connect(ac,SIGNAL(triger)…… QMenu *menu2=new QMenu("设置",this);
menu2->addAction(main_ac1);//将动作加入菜单
menu2->addAction(main_ac2);
menu2->addAction(main_ac3);
menu2->addAction(ac_1);
menu2->addAction(ac_2);
ui->menuBar->addMenu(menu2); //QWidgetAction
//构建个自定义小部件
QListWidget *lw=new QListWidget;
lw->addItem("列表项1");
lw->addItem("列表项2");
lw->addItem("列表项3");
//将部件加入QWidgetAction
QWidgetAction *a1=new QWidgetAction(this);
a1->setDefaultWidget(lw);
//将QWidgetAction加入到QMenu
QMenu *menu3=new QMenu(this);
menu3->addAction(a1);
//建立托盘图标,将QMenu加入到托盘图标右键菜单中
QIcon icon("://tray.ico");
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(icon);
trayIcon->setContextMenu(menu3);
trayIcon->setVisible(true);
} MainWindow::~MainWindow()
{
delete ui;
}