主窗口MainWindow需要菜单栏QMenuBar及菜单QMenu来组成自身,一般应用程序的所有功能都能在菜单中找到。接下来就来说说它们。
————————————————————————————————————————————————————————————————
QMenu
它添加了很多动作QAction,并用自身组成了菜单栏QMenuBar上的菜单,当然也可用于基它地方,如上下文菜单等。
公有函数如下:
QMenu(QWidget * parent = 0)
QMenu(const QString & title, QWidget * parent = 0)
~QMenu()
QAction * addAction(const QString & text)//添加动作
QAction * addAction(const QIcon & icon, const QString & text)
QAction * addAction(const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0)//尝试失败,求教const char * member该如何?
QAction * addAction(const QIcon & icon, const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0)
void addAction(QAction * action)
QAction * addMenu(QMenu * menu)//添加菜单
QMenu * addMenu(const QString & title)
QMenu * addMenu(const QIcon & icon, const QString & title)
QAction * insertMenu(QAction * before, QMenu * menu)
QAction * addSection(const QString & text)//
QAction * addSection(const QIcon & icon, const QString & text)
QAction * addSeparator()//添加分隔线
QAction * insertSection(QAction * before, const QString & text)
QAction * insertSection(QAction * before, const QIcon & icon, const QString & text)
QAction * insertSeparator(QAction * before)
void popup(const QPoint & p, QAction * atAction = 0)//该菜单在p坐标点弹出
QAction * exec()//该菜单在0,0 坐标出现,执行后阻塞程序,返回点击的动作或0(什么也没点,如按Esc键)
QAction * exec(const QPoint & p, QAction * action = 0)//上同,位置一般为:1,鼠标位置exec(QCursor::pos());2,与某部件对齐exec(somewidget.mapToGlobal
(QPoint(0, 0)));3,通过QMouseEvent *e,exec(e->globalPos());
void hideTearOffMenu()
void clear()//清除菜单中的所有项
void setActiveAction(QAction * act)
void setAsDockMenu()
void setDefaultAction(QAction * act)
void setIcon(const QIcon & icon)
void setSeparatorsCollapsible(bool collapse)
void setTearOffEnabled(bool)
void setTitle(const QString & title)
void setToolTipsVisible(bool visible)
QIcon icon() const
QAction * defaultAction() const
QString title() const
NSMenu * toNSMenu()
bool toolTipsVisible() const
QAction * actionAt(const QPoint & pt) const
QRect actionGeometry(QAction * act) const
QAction * activeAction() const
bool isEmpty() const
bool isTearOffEnabled() const
bool isTearOffMenuVisible() const
QAction * menuAction() const
bool separatorsCollapsible() const
Signals
void | aboutToHide() |
void | aboutToShow() |
void | hovered(QAction * action) |
void | triggered(QAction * action) |
静态成员函数Static Public Members,可用它将一些actions组合成一个菜单
QAction * | exec(QList |
————————————————————————————————————————————————————————————————
QMenuBar
它是主窗口存放菜单的地方。
在windows上可认为它就是那个大理石颜色的一栏
函数如下:
QMenuBar(QWidget * parent = 0)
~QMenuBar()
QAction * addMenu(QMenu * menu)//以下几个添加菜单的比较常用
QMenu * addMenu(const QString & title)
QMenu * addMenu(const QIcon & icon, const QString & title)
void setActiveAction(QAction * act)
void setCornerWidget(QWidget * widget, Qt::Corner corner = Qt::TopRightCorner)//在菜单条的最左或最右添加个小部件,corner可以为Qt::TopRightCorner(最右)或
Qt::TopLeftCorner(最左)
void setDefaultAction(QAction * act)//只支持Windows Mobile,手机上的“左软键”
void setDefaultUp(bool)//默认false,菜单正常地下拉显示,如设成false,则菜单往上拉显示
void setNativeMenuBar(bool nativeMenuBar)//菜单本地化,只支持Mac OS X和Windows CE。我想,如ubuntu上,当窗口最大化时,菜单附着于最上一栏一样吧……(On t hese platforms if this property is true, the menubar is used in the native menubar and is not in the window of its parent, if false the me nubar remains in the window.On other platforms the value of this attribute has no effect.)
void clear()//清除所有action
QWidget * cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const
QAction * defaultAction() const
bool isDefaultUp() const
bool isNativeMenuBar() const
NSMenu * toNSMenu()
QAction * actionAt(const QPoint & pt) const
QRect actionGeometry(QAction * act) const
QAction * activeAction() const
QAction * addAction(const QString & text)//以下四个添加动作或菜单,该动作将直接显示在菜单条上,感觉四个函数都比较让人无语,想不出官方添加它们的用意!
QAction * addAction(const QString & text, const QObject * receiver, const char * member)
void addAction(QAction * action)
QAction * insertMenu(QAction * before, QMenu * menu)
QAction * addSeparator()//在菜单条上添加分隔线,试了下,没看到效果,和以上四个一样让人无语
QAction * insertSeparator(QAction * before)
Public Slots
virtual void | setVisible(bool visible) |
Signals
void | hovered(QAction * action) |
void | triggered(QAction * action)//点击触发的动作,可用它判断是哪个动作触发,代替信号槽的使用 |
—————————————————————————————————————————————————————————————————
简单测试示例如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMenu *menu1= new QMenu("hello");
menu1->addAction("菜单项1");
menu1->addAction("菜单项2");
if(QAction *act=menu1->exec(QCursor::pos()))
{
qDebug()<< act->text() <<"triggered";
}
file_menu=new QMenu("文件");
file_menu->addAction("打开");//可用返回的QAction 连接槽函数
//file_menu->addAction("退出",this,exit,QKeySequence("Ctrl+E"));//失败
file_menu->addSeparator();
file_menu->addSection("Section");//貌似无效果
file_menu->addMenu(menu1);
ui->menuBar->addMenu(file_menu);
QPushButton *btn=new QPushButton("hello");
ui->menuBar->setCornerWidget(btn,Qt::TopLeftCorner);
ui->menuBar->setDefaultUp(true);
}
MainWindow::~MainWindow()
{
delete ui;
}