当前位置: 首页 > news >正文

QT6 源(113)篇二:阅读与注释工具栏 QToolBar,给出源码

(9)本源码来自于头文件 qtoolbar . h

#ifndef QDYNAMICTOOLBAR_H
#define QDYNAMICTOOLBAR_H#include <QtWidgets/qtwidgetsglobal.h>
#include <QtGui/qaction.h>
#include <QtWidgets/qwidget.h>QT_REQUIRE_CONFIG(toolbar);QT_BEGIN_NAMESPACEclass QToolBarPrivate;class QAction;
class QIcon;
class QMainWindow;
class QStyleOptionToolBar;/*
The QToolBar class provides a movable panel that contains a set of controls.工具栏按钮是通过添加操作、使用addAction()或insertAction()来添加的。
可以使用addSeparator()或insertSeparator()分隔按钮组。
如果工具栏按钮不合适,可以使用addWidget()或insertWidget()插入一个控件。
合适的控件示例包括QSpinBox、QDoubleSpinBox和QComboBox。
当按下工具栏按钮时,它会发出actionTriggered()信号。工具栏可以固定在特定区域(例如窗口顶部),或者可以在工具栏区域之间移动;
请参setMovable()、isMovable()、alowedAreas()和isAreaAllowed()。当工具栏的尺寸调整到太小以至于无法显示其中包含的所有项目时,
扩展按钮将作为工具栏中的最后一个项目出现。按下扩展按钮将弹出一个包含当前不适合工具栏的项目的菜单。当一个QToolBar不是QMainWindow的子项时,
它就会失去使用addWidget()添加到工具栏的扩展弹出窗口填充小部件的能力。
请使用通过继承OWidgetAction并实现QWidgetAction::createWidget()创建的小部件动作。enum    ToolButtonStyle { //本属性描述工具栏的按钮风格ToolButtonIconOnly,ToolButtonTextOnly,ToolButtonTextBesideIcon,ToolButtonTextUnderIcon ,ToolButtonFollowStyle};enum      ToolBarArea {  //本属性用于描述工具栏可以摆放的位置NoToolBarArea  =   0,LeftToolBarArea  = 0x1,RightToolBarArea  = 0x2,TopToolBarArea  = 0x4,BottomToolBarArea  = 0x8,ToolBarArea_Mask  = 0xf,AllToolBarAreas = ToolBarArea_Mask};Q_DECLARE_FLAGS(ToolBarAreas, ToolBarArea)Q_DECLARE_OPERATORS_FOR_FLAGS(ToolBarAreas)
*/class Q_WIDGETS_EXPORT QToolBar : public QWidget //工具栏可装入按钮、分隔符,还有控件
{Q_OBJECT//工具栏的方向。 默认值是 Qt::Horizontal。当工具栏由 QMainWindow管理时,不应使用此函数。//如果您想将已添加到主窗口的工具栏移动到另一个Qt:ToolBarArea,//可以使用QMainWindow::addToolBar()或QMainWindow::insertToolBar()。Q_PROPERTY(Qt::Orientation      orientation    //工具栏的方向.不应使用本属性。READ                orientation    WRITE   setOrientationNOTIFY              orientationChanged)Q_PROPERTY(QSize                iconSize       //工具栏里的按钮图片的最大大小READ                iconSize       WRITE   setIconSizeNOTIFY              iconSizeChanged) //小图片不会因此而扩展//size of icons in the toolbar.//The default size is determined by the application's style and is derived from the//  QStyle::PM_ToolBarIconSize pixel metric. It is the maximum size an icon can have.//Icons of smaller size will not be scaled up.//This property holds whether the user can move the toolbar within the toolbar area,//or between toolbar areas.   By default, this property is true.//This property only makes sense if the toolbar is in a QMainWindow.Q_PROPERTY(bool                 movable        //表本工具栏是否可在允许的上下左右区域间移动READ              isMovable        WRITE   setMovableNOTIFY              movableChanged)Q_PROPERTY(Qt::ToolBarAreas     allowedAreas   //上下左右READ                allowedAreas   WRITE   setAllowedAreasNOTIFY              allowedAreasChanged)//areas where the toolbar may be placed。The default is Qt::AllToolBarAreas.//This property only makes sense if the toolbar is in a QMainWindow.//定义  ToolBarAreas = QFlas<ToolBarArea>Q_PROPERTY(bool                 floatable      //表本工具栏是否可被拖拽到屏幕上的任意区域READ              isFloatable      WRITE   setFloatable)//This property holds whether the toolbar can be dragged and dropped as an//  independent window.   The default is true.//This property holds whether the toolbar is an independent window.//By default, this property is true.Q_PROPERTY(bool                floating        READ     isFloating)//This property holds the style of toolbar buttons。//This property defines the style of all tool buttons that are added as QActions.//Note that if you add a QToolButton with the addWidget() method,//  it will not get this button style.//To have the style of toolbuttons follow the system settings,//  set this property to Qt::ToolButtonFollowStyle.//  On Unix, the user settings from the desktop environment will be used.//  On other platforms, Qt::ToolButtonFollowStyle means icon only.Q_PROPERTY(Qt::ToolButtonStyle       toolButtonStyle    //设置工具栏的按钮类 QAction的样式READ                     toolButtonStyle    WRITE    setToolButtonStyleNOTIFY                   toolButtonStyleChanged)private:Q_DECLARE_PRIVATE(QToolBar)Q_DISABLE_COPY(QToolBar)Q_PRIVATE_SLOT(d_func(), void _q_toggleView(bool))Q_PRIVATE_SLOT(d_func(), void _q_updateIconSize(const QSize &))Q_PRIVATE_SLOT(d_func(), void _q_updateToolButtonStyle(Qt::ToolButtonStyle))friend class QMainWindow       ;friend class QMainWindowLayout ;friend class QToolBarLayout    ;friend class QToolBarAreaLayout;public://Constructs a QToolBar with the given parent.explicit QToolBar(QWidget * parent = nullptr); //toolBar = new QToolBar(MainWindow);explicit QToolBar(const QString & title, QWidget * parent = nullptr);//Constructs a QToolBar with the given parent. //title字段在上下文菜单里才有效。//The given window title identifies the toolbar and is shown in the//  context menu provided by QMainWindow.~QToolBar();//   Q_PROPERTY(Qt::Orientation      orientation    //工具栏的方向.不应使用本属性。
//               READ                orientation    WRITE   setOrientation
//               NOTIFY              orientationChanged)Qt::Orientation      orientation() const;void              setOrientation(Qt::Orientation orientation);
Q_SIGNALS:void                 orientationChanged(Qt::Orientation orientation);public :
//   Q_PROPERTY(QSize                iconSize       //工具栏里的按钮图片的最大大小
//               READ                iconSize       WRITE   setIconSize
//               NOTIFY              iconSizeChanged) //小图片不会因此而扩展QSize                iconSize() const;
public Q_SLOTS:void              setIconSize       (const QSize & iconSize);
Q_SIGNALS:void                 iconSizeChanged(const QSize & iconSize);public :
//   Q_PROPERTY(bool                 movable        //表本工具栏是否可在允许的上下左右区域间移动
//               READ              isMovable        WRITE   setMovable
//               NOTIFY              movableChanged)bool               isMovable() const;void              setMovable       (bool movable);
Q_SIGNALS:void                 movableChanged(bool movable);public :
//   Q_PROPERTY(Qt::ToolBarAreas     allowedAreas   //上下左右
//               READ                allowedAreas   WRITE   setAllowedAreas
//               NOTIFY              allowedAreasChanged)Qt::ToolBarAreas     allowedAreas() const;void              setAllowedAreas       (Qt::ToolBarAreas         areas);
Q_SIGNALS:void                 allowedAreasChanged(Qt::ToolBarAreas  allowedAreas);public :
//   Q_PROPERTY(bool                 floatable      //表本工具栏是否可被拖拽到屏幕上的任意区域
//               READ              isFloatable      WRITE   setFloatable)bool               isFloatable() const;void              setFloatable(bool floatable);//   Q_PROPERTY(bool                 floating       READ     isFloating)bool               isFloating() const;//   Q_PROPERTY(Qt::ToolButtonStyle       toolButtonStyle    //设置工具栏的按钮类 QAction的风格
//               READ                     toolButtonStyle    WRITE    setToolButtonStyle
//               NOTIFY                   toolButtonStyleChanged)Qt::ToolButtonStyle       toolButtonStyle() const;
public Q_SLOTS:void     setToolButtonStyle       (Qt::ToolButtonStyle   toolButtonStyle);
Q_SIGNALS:void        toolButtonStyleChanged(Qt::ToolButtonStyle   toolButtonStyle);public  ://Returns true if this toolbar is dockable可停靠的 in the given area;//otherwise returns false.inline bool isAreaAllowed(Qt::ToolBarArea area) const{ return (allowedAreas() & area) == area; }//Returns the widget associated with the specified action.QWidget * widgetForAction(QAction * action) const; //返回与形参按钮关联的控件。//Inserts the given widget in front of the toolbar item associated with the//  before action.                   在 before按钮的前面插入控件 widget。//Note: You should use QAction::setVisible() to change the visibility of the widget.//Using QWidget::setVisible(), QWidget::show() and QWidget::hide() does not work.//返回控件对应的 QAction对象的意义就在于用其控制 QWidget控件的可见性。QAction * insertWidget(QAction * before, QWidget * widget); //往工具栏里添加 QWidget控件QAction *    addWidget(QWidget * widget);//Adds the given widget to the toolbar as the toolbar's last item.//The toolbar takes ownership of widget.  //工具栏会容纳这个控件,完成对其将来的析构。//If you add a QToolButton with this method,//  the toolbar's Qt::ToolButtonStyle will not be respected不会被参考.QRect     actionGeometry(QAction * action) const;QAction * actionAt(const QPoint & p) const;inlineQAction * actionAt(int x,   int   y) const { return actionAt(QPoint(x, y)); }//Returns a checkable action that can be used to show or hide this toolbar.//The action's text is set to the toolbar's window title.QAction * toggleViewAction() const; //新建的 toolBar按钮,以工具栏为父容器,故而不会内存泄露。//QToolBar继承于 QWidget,故也有 windowTitle属性,默认取值为"toolBar"。//把该 toolBar按钮添加到窗体后,点击它就会隐藏或开启工具栏了。void clear(); //Removes all actions from the toolbar.清空按钮与控件,剩余空的工具栏。using   QWidget::addAction; //void  QWidget::addAction(QAction * action);添加按钮QAction * addAction(const QString & text);    //这些与菜单栏是重复的QAction * addAction(const QString & text,     //添加按钮并指定按钮的槽函数const QObject * receiver, const char    * member);QAction * addAction(const QIcon   & icon,     const QString & text);QAction * addAction(const QIcon   & icon,     const QString & text,const QObject * receiver, const char    * member);// addAction(QString)://Connect to a QObject slot / functor or function pointer (with context)template<class 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){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){QAction * result = addAction(text);connect(result, & QAction::triggered, slot);return result;}// addAction(QString)://Connect to a QObject slot / functor or function pointer (with context)template<class Obj, typename Func1>inline typename std::enable_if<!  std::is_same<const char*, Func1>::value&& QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value,QAction *>::typeaddAction(const QIcon   & actionIcon,const QString & text, const Obj * object, Func1 slot){QAction *result = addAction(actionIcon, text);connect(result, &QAction::triggered, object, std::move(slot));return result;}// addAction(QIcon, QString)://Connect to a functor or function pointer (without context)template <typename Func1>inline QAction * addAction(const QIcon & actionIcon, const QString & text, Func1 slot){QAction *result = addAction(actionIcon, text);connect(result, &QAction::triggered, slot);return result;}QAction * insertSeparator(QAction * before); //往工具栏里添加分隔符QAction *    addSeparator();public Q_SLOTS://void setIconSize       (const         QSize &  iconSize       );//void setToolButtonStyle(Qt::ToolButtonStyle    toolButtonStyle);Q_SIGNALS://This signal is emitted when an action in this toolbar is triggered.//This happens when the action's tool button is pressed,//or when the action is triggered in some other way outside the toolbar.//The parameter holds the triggered action. //工具栏里的按钮被触发,发射此信号void          actionTriggered(QAction    *          action);//This signal is emitted when the floating property changes.//The topLevel parameter is true if the toolbar is now floating;//otherwise it is false. 当工具栏真的浮动起来或撤销浮动时,均会触发本信号函数。void          topLevelChanged(bool                  topLevel       );//This signal is emitted when the toolbar becomes visible (or invisible).//This happens when the widget is hidden or shown.void        visibilityChanged(bool                  visible        );//  This signal is emitted when the collection of allowed areas for the//      toolbar is changed. The new areas in which the toolbar can be positioned are//  specified by allowedAreas.//void    allowedAreasChanged(Qt::ToolBarAreas      allowedAreas   ); //函数原型//void     orientationChanged(Qt::Orientation       orientation    ); //函数原型//  This signal is emitted when the orientation of the toolbar changes.//  The orientation parameter holds the toolbar's new orientation.//  This signal is emitted when the tool button style is changed.//  The toolButtonStyle parameter holds the toolbar's new tool button style.//void toolButtonStyleChanged(Qt::ToolButtonStyle   toolButtonStyle); //函数原型//void         movableChanged(bool                  movable        ); //函数原型//  This signal is emitted when the toolbar becomes movable or fixed.//  If the toolbar can be moved, movable is true; otherwise it is false.//  This signal is emitted when the icon size is changed.//  The iconSize parameter holds the toolbar's new icon size.//void        iconSizeChanged(const QSize         & iconSize       ); //函数原型protected:virtual void initStyleOption(QStyleOptionToolBar * option) const;bool        event(QEvent       * event) override;void  actionEvent(QActionEvent * event) override;void  changeEvent(QEvent       * event) override;void   paintEvent(QPaintEvent  * event) override;}; //完结 class QToolBar : public QWidgetQT_END_NAMESPACE#endif // QDYNAMICTOOLBAR_H

(10)

谢谢

相关文章:

  • C46-二维数组与指针的总结
  • 谁在用AI掘金?——近屿智能教你掌握AI时代的生存密码
  • 开源运维工具HHDESK源码开发
  • 【DCGMI专题2】---DCGMI 常用命令详解与生产实例分析
  • 《微机原理与接口技术》第 8 章 常用接口芯片
  • es学习小结
  • LeetCode-前缀和-和为K的子数组
  • 便捷的Office批量转PDF工具
  • 第32节:基于ImageNet预训练模型的迁移学习与微调
  • 鸿蒙路由参数传递
  • X 下载器 2.1.42 | 国外媒体下载工具 网页视频嗅探下载
  • 分析 redis 的 exists 命令有一个参数和多个参数的区别
  • Python的sys模块:系统交互的关键纽带
  • 【开源工具】文件夹结构映射工具 | PyQt5实现多模式目录复制详解
  • Python基础学习-Day30
  • Python训练营打卡——DAY31(2025.5.20)
  • Modbus通信协议详解
  • 基于FFT变换的雷达信号检测和参数估计matlab性能仿真
  • 【SPIN】PROMELA远程引用与控制流验证(SPIN学习系列--5)
  • AI练习:毛发旋转效果
  • 焦点访谈丨售假手段又翻新,警惕化肥“忽悠团”的坑农套路
  • 国际博物馆日|航海博物馆:穿梭于海洋神话与明代造船工艺间
  • 15年全免费,内蒙古准格尔旗实现幼儿园到高中0学费
  • 又一例!易方达基金张坤卸任副总职务,将专注于投资管理工作
  • 大陆非遗项目打铁花、英歌舞将在台演出
  • 大环线呼之欲出,“金三角”跑起来了