(1)
(2)
(3)
++
(4)
(5)
(6)
(7)以下源代碼來自于頭文件 qmenubar . h :
#ifndef QMENUBAR_H
#define QMENUBAR_H#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qmenu.h>QT_REQUIRE_CONFIG(menubar);QT_BEGIN_NAMESPACE //說明本菜單欄定義于 Qt的全局命名空間class QMenuBarPrivate;
class QStyleOptionMenuItem;
class QWindowsStyle;
class QPlatformMenuBar;/*
The QMenuBar class provides a horizontal menu bar.不需要設置菜單欄。它會自動將其幾何形狀設置為父控件的頂部,并在父控件重新調整大小時適當地進行更改。Usage:
在大多數主要窗口樣式應用程序中,
您將使用QMainWindow中提供的menuBar()函數,將QMenus添加到菜單欄中,并將QActions添加到彈出菜單中。可以通過使用QWidgetAction類的實例來將小部件添加到菜單中。
然后,可以使用通常的方式將這些動作插入菜單中;請參閱QMenu文檔以獲取詳細信息。Platform Dependent Look and Feel:
不同的平臺對菜單欄的外觀以及用戶與菜單欄交互時的行為有不同的要求。
例如,Windows 系統通常配置為僅在按下 AIt 鍵時顯示帶下劃線的字符助記符,
這些助記符指示菜單欄中項目的鍵盤快捷方式。QMenuBar as a Global Menu Bar :
在macOs和某些Linux桌面環境中,如Ubuntu Unity,
QMenuBar是用于使用系統級菜單欄的包裝器.
如果在同一個對話框中有多個菜單欄,
則最外面的菜單欄(通常位于具有widget標志Qt::Window的widget中)將用于系統級菜單欄。
...........*/class Q_WIDGETS_EXPORT QMenuBar : public QWidget
{Q_OBJECT//此屬性包含彈出窗口的方向. 默認彈出窗口的方向。默認情況下,菜單會“向下”彈出屏幕。//通過將屬性設置為true,菜單將“向上”彈出。您可以將此調用用于位于它們所指向的文檔下方的菜單。//如果菜單無法在屏幕上顯示,則自動使用其他方向。Q_PROPERTY(bool defaultUp //決定下拉菜單的彈出方向,默認向下彈出。READ isDefaultUp WRITE setDefaultUp)Q_PROPERTY(bool nativeMenuBar //本屬性在 windows系統上無用READ isNativeMenuBar WRITE setNativeMenuBar)//這個屬性表示在支持它的平臺上是否將使用菜單欄作為原生菜單欄。//此屬性指定是否應在支持的平臺上將菜單欄用作原生菜單欄。目前支持的平臺是macOS和Linux桌面,//它們使用com,canonical.dbusmeny D-Bus接口(例如Ubuntu Unity)。//如果此屬性為真,則菜單欄在原生菜單欄中使用,不在其父窗口中;如果為假,則菜單欄保留在窗口中。//在其他平臺上,設置此屬性沒有影響,讀取此屬性將始終返回假。//默認情況下,會遵循是否為該應用程序設置了 Qt:AA_DontUseNativeMenuBar屬性。//顯式設置此屬性會覆蓋屬性的存在(或不存在)。private:Q_DECLARE_PRIVATE(QMenuBar)Q_DISABLE_COPY(QMenuBar)Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())Q_PRIVATE_SLOT(d_func(), void _q_internalShortcutActivated(int))Q_PRIVATE_SLOT(d_func(), void _q_updateLayout())friend class QMenu ; //本菜單欄類的友元類是菜單 QMenufriend class QMenuPrivate ;friend class QWindowsStyle;public:explicit QMenuBar(QWidget * parent = nullptr);~QMenuBar();// Q_PROPERTY(bool defaultUp //決定下拉菜單的彈出方向,默認向下彈出。
// READ isDefaultUp WRITE setDefaultUp)bool isDefaultUp() const;void setDefaultUp(bool);// Q_PROPERTY(bool nativeMenuBar //本屬性在 windows系統上無用
// READ isNativeMenuBar WRITE setNativeMenuBar)bool isNativeMenuBar() const;void setNativeMenuBar(bool nativeMenuBar);QPlatformMenuBar * platformMenuBar(); //無注釋//菜單欄里的按鈕是由 QMenu::menuAction()得到的。//總結:以為菜單欄里插入的是菜單,其實插入的是對應代表菜單的按鈕QAction。//Returns the QAction that is currently highlighted, if any, else nullptr.QAction * activeAction() const; //顯示菜單欄上被高亮顯示的按鈕void setActiveAction(QAction * action);//Sets the currently highlighted action to action.//這倆函數的意思是不必為每個按鈕單獨設置觸發與高亮函數,只為按鈕的容器,菜單欄或菜單設計槽函數即可。//Returns the widget on the left of the first or on the right of the last menu item,// depending on corner.//Note: Using a corner other than Qt::TopRightCorner or// Qt::TopLeftCorner will result in a warning.//enum Qt::Corner { TopLeftCorner = 0, TopRightCorner = 1,// BottomLeftCorner = 2, BottomRightCorner = 3 };QWidget * cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const;void setCornerWidget(QWidget * w,Qt::Corner corner = Qt::TopRightCorner);//This sets the given w to be shown directly on the left of the first menu item,//or on the right of the last menu item, depending on corner.//The menu bar takes ownership of widget, reparenting it into the menu bar.//However, if the corner already contains a widget,//this previous widget will no longer be managed and will still be a// visible child of the menu bar.QSize sizeHint() const override;QSize minimumSizeHint() const override;int heightForWidth(int) const override;QRect actionGeometry(QAction *) const; //因為本菜單欄也可以直接管理按鈕QAction * actionAt(const QPoint &) const; //略,這倆函數在 QMenu里也出現過void clear(); //Removes all the actions from the menu bar.//This convenience function inserts menu before action before and returns the// menus menuAction(). //把菜單 menu 放到 before的前面,并返回 menu對應的 QActionQAction * insertMenu(QAction * before, QMenu * menu); //在菜單里也可以插入子菜單QAction * addMenu(QMenu * menu); //在菜單欄的末尾追加菜單,返回菜單對應的按鈕。//Appends menu to the menu bar. Returns the menu's menuAction().//The menu bar does not take ownership of the menu.QMenu * addMenu(const QString & title);//生成一個菜單,其對應的按鈕項叫 title//Appends a new QMenu with title to the menu bar.//The menu bar takes ownership of the menu. Returns the new menu.QMenu * addMenu(const QIcon & icon, const QString & title);//Appends a new QMenu with icon and title to the menu bar.//The menu bar takes ownership of the menu. Returns the new menu.//This convenience function creates a new separator action,//i.e. an action with QAction::isSeparator() returning true.//The function inserts the newly created action into this menu bar's list of// actions before action before and returns it.QAction * insertSeparator(QAction * before); //在菜單欄里 before按鈕的前面插入分隔符QAction * addSeparator(); //Appends a separator to the menu.//經測試,新添加的分隔符的容器父類,就是菜單欄。但經測試,菜單欄里似乎不允許或不顯示添加的分隔符。//該 addAction() 函數的正確用法如下:可見 QMenu::menuAction()這個函數很重要!!// menubar->addAction(menu_F->menuAction()); //為菜單欄添加"文件(&F)"菜單// menu_F ->setTitle(// QCoreApplication::translate("MainWindow", //設置菜單的名字,以及快捷鍵// "\346\226\207\344\273\266(&F)", nullptr));using QWidget::addAction;//void QWidget::addAction(QAction * action);QAction * addAction(const QString & text); //說明菜單欄里可以添加普通按鈕//This convenience function creates a new action with text.//The function adds the newly created action to the menu's list of actions,// and returns it.QAction * addAction(const QString & text, //為菜單欄創建按鈕,并為其指定槽函數。const QObject * receiver, const char * member);// addAction(QString)://Connect to a QObject slot / functor or function pointer (with context)template<typename Obj, typename Func1>inline typename std::enable_if<!std::is_same<const char *, Func1>::value&& QtPrivate::IsPointerToTypeDerivedFromQObject<Obj *>::Value,QAction *>::typeaddAction(const QString & text, const Obj * object, Func1 slot){ //把創建的文本為 text的按鈕的 triggered()信號連接到形參 object的 slot槽函數上QAction * result = addAction(text);connect(result, & QAction::triggered, object, std::move(slot));return result;}// addAction(QString)://Connect to a functor or function pointer (without context)template <typename Func1>inline QAction * addAction(const QString & text, Func1 slot){ //把創建的 text按鈕的信號連接到全局槽函數 slot上。QAction * result = addAction(text);connect(result, & QAction::triggered, std::move(slot));return result;}public Q_SLOTS:void setVisible(bool visible) override; //菜單欄消失,窗體整體上移,工具欄占據了菜單欄的位置。//Reimplements an access function for property: QWidget::visible.Q_SIGNALS://This signal is emitted when an action in a menu belonging to this menubar//is triggered as a result of a mouse click; //注意上面的話:an action in a menu//action is the action that caused the signal to be emitted.//Note: QMenuBar has to have ownership of the QMenu in order this signal to work.//Normally, you connect each menu action to a single slot using QAction::triggered(),//but sometimes you will want to connect several items to a single slot//(most often if the user selects from an array). This signal is useful in such cases.//即使菜單欄里的按鈕被敲擊,也不會觸發 triggered()信號。void triggered(QAction * action); //菜單欄里的菜單里的按鈕被鼠標敲擊,才觸發本信號。void hovered(QAction * action); //菜單欄里的按鈕高亮時觸發本信號。注意這倆信號對按鈕的區別。//This signal is emitted when a menu action is highlighted;//action is the action that caused the event to be sent.//Often this is used to update status information.protected:virtual void initStyleOption(QStyleOptionMenuItem * option,const QAction * action) const;bool eventFilter(QObject *, QEvent *) override;bool event(QEvent *) override;void changeEvent(QEvent *) override;void keyPressEvent(QKeyEvent *) override;void mouseReleaseEvent(QMouseEvent *) override;void mousePressEvent(QMouseEvent *) override;void mouseMoveEvent(QMouseEvent *) override;void leaveEvent(QEvent *) override;void paintEvent(QPaintEvent *) override;void resizeEvent(QResizeEvent *) override;void actionEvent(QActionEvent *) override;void focusOutEvent(QFocusEvent *) override;void focusInEvent(QFocusEvent *) override;void timerEvent(QTimerEvent *) override;}; //完結 class QMenuBar : public QWidgetQT_END_NAMESPACE#endif // QMENUBAR_H
(8)
謝謝