(1) 本类来自于头文件 qpushbutton . h , 本普通按钮只是从其父类扩充了极少量的内容,如增加了对弹出菜单的支持 :
#ifndef QPUSHBUTTON_H
#define QPUSHBUTTON_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qabstractbutton.h>
QT_REQUIRE_CONFIG(pushbutton);
QT_BEGIN_NAMESPACE //说明本类定义在 QT 的全局命名空间
class QPushButtonPrivate;
class QMenu;
class QStyleOptionButton;
/*
按钮,或命令按钮,可能是任何图形用户界面中最常用的控件。
按下(点击)一个按钮以命令“取消”、计算机执行某些操作,或回答问题。
典型的按钮有“确定”“应用”、“关闭”“丕”和“帮助”。“是”.
命令按钮是矩形的,通常显示描述其操作的文本标签。
可以通过在文本中首选字符的前面加上&号来指定快捷键。例如:
QPushButton * button = new QPushButton("&Download", this);
In this example the shortcut is Alt+D. See the QShortcut documentation for details
(to display an actual ampersand, use '&&').
按钮显示文本标签,也可以选择显示一个小图标。
这些可以使用构造函数进行设置,稍后使用setText()和 setlcon()进行更改。
如果禁用了该按钮,则文本和图标的外观将根据 GU1 样式进行作,以使按钮看起来“已禁用”。
当一个按钮被鼠标、空格键或键盘快捷键激活时,它会发出clicked()信号。
连接到这个信号来执行按钮的动作。按钮还提供不太常用的信号,例如pressed()和released()。
对话框中的命令按钮默认为自动默认按钮,即当它们接收到键盘输入焦点时,它们会自动成为默认按钮。
默认按钮是指用户在对话框中按下 Enter或 Return 键时激活的按钮。
您可以通过setAutoDefault ()更改此设置。
请注意,自动默认按钮预留了一些额外的空间,这是绘制默认按钮指示器所必需的。
如果您不想在按钮周围留出空间,请调用setAutoDefault(false)。
由于其核心地位,在过去的十年中,按钮小部件已经发展到可以容纳许多变化。
微软风格指南现在显示了大约10种不同的Windows按钮状态,文本暗示,当考虑到所有功能组合时,还有几十种。
最重要的模式或状态是:
可用或不可用(灰色,禁用)。
标准按钮、切换按钮或菜单按钮。
开或关(仅用于切换按钮)。
默认或正常。对话框中的默认按钮通常可以使用 Enter 或 Return 键“单击”
自动重复或不重复。
按下或未按下。
一般来说,当应用程序或对话框窗口在用户单击时执行操作(如“应用”“取消”关闭”和“帮助”)
并且当小部件应该具有宽矩形形状和文本标签时,应使用按钮。
改变窗口状态而不是执行操作(如QFileDialog右上角的按钮)的小的、通常为方形的按钮不是命令按钮,
而是工具按钮。Qt为这些按钮提供了一个特殊类(QToolButton)。
如果你需要切换行为(见setcheckable ())或者
像滚动条中的箭头那样自动重复激活信号(见setAutoRepeat()),命令按钮可能不是你想要的。
有疑问时,使用工具按钮。
注:在 macOS上,当按钮的宽度小于50 或高度小于 30 时,按钮的角会从圆形变为方形。
使用 setMinimumSize()函数可以防止这种行为。
命令按钮的一种变体是菜单按钮。这些按钮不仅提供一种命令,还提供几种命令.
因为当它们被点击时,它们会弹出一个选项菜单。使用setMenu()方法将弹出式菜单与按钮关联。
其他类型的按钮是选项按钮(见 QRadioButton)和复选框(见 QCheckBox)。
在Qt中,QAbstractButton基类提供了大多数模式和其他APl,
而QPushButton提供了GUI逻辑。有关API的更多信息,请参见QAbstractButton。
*/
class Q_WIDGETS_EXPORT QPushButton : public QAbstractButton
{
Q_OBJECT //又插入了这个宏,以使用元对象系统
//这个属性表示按钮边框是否凸起。 //本属性为 true则没有按钮的灰色背景了。
//这个属性的默认值是false。如果设置了这个属性,大多数样式将不会绘制按钮的背景,除非按钮被按下。
//可以使用setAutoFillBackground()来确保使用OPalette::Button画笔填充背景。
Q_PROPERTY(bool flat READ isFlat WRITE setFlat)
Q_PROPERTY(bool default READ isDefault WRITE setDefault) //无注释
Q_PROPERTY(bool autoDefault READ autoDefault WRITE setAutoDefault)
//This property holds whether the push button is an auto default button。
//If this property is set to true then the push button is an auto default button.
//In some GUI styles a default button is drawn with an extra frame around it,
//up to 3 pixels or more. Qt automatically keeps this space free around
//auto-default buttons, //总之,开启了此属性后,在对话框里,按钮会具有颜色更深的边框
//i.e., auto-default buttons may have a slightly larger size hint.
//This property's default is true for buttons that have a QDialog parent;
//otherwise it defaults to false.
//See the default property for details of how default and auto-default interact.
private:
Q_DISABLE_COPY(QPushButton)
Q_DECLARE_PRIVATE(QPushButton)
#if QT_CONFIG(menu)
Q_PRIVATE_SLOT(d_func(), void _q_popupPressed())
#endif
public:
//Constructs a push button with no text and a parent.
explicit QPushButton(QWidget * parent = nullptr);
//Constructs a push button with the parent parent and the text text.
explicit QPushButton(const QString & text, QWidget * parent = nullptr);
QPushButton(const QIcon & icon,
const QString & text, QWidget * parent = nullptr);
//Constructs a push button with an icon and a text, and a parent.
//Note that you can also pass a QPixmap object as an icon
//(thanks to the implicit type conversion provided by C++).
~QPushButton();
//Q_PROPERTY(bool flat READ isFlat WRITE setFlat)
bool isFlat() const;
void setFlat(bool);
//Q_PROPERTY(bool default READ isDefault WRITE setDefault) //无注释
bool isDefault() const;
void setDefault(bool);
//Q_PROPERTY(bool autoDefault READ autoDefault WRITE setAutoDefault)
bool autoDefault() const;
void setAutoDefault(bool);
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
#if QT_CONFIG(menu) //经测试是存在这个宏定义的
//Returns the button's associated popup menu or nullptr
// if no popup menu has been set.
QMenu * menu() const; //本属性适合制作菜单栏里的按钮,都是带弹出菜单的。
//Associates the popup menu menu with this push button.
//This turns the button into a menu button,
//which in some styles will produce a small triangle to the right of the
//button's text.
//Ownership of the menu is not transferred to the push button.
void setMenu(QMenu * menu);
#endif
public Q_SLOTS:
#if QT_CONFIG(menu) //本子控件新增的槽函数,增加了对菜单的弹出框的支持
void showMenu(); //无注释
#endif
protected:
QPushButton(QPushButtonPrivate & dd, QWidget * parent = nullptr);
virtual void initStyleOption(QStyleOptionButton * option) const;
//判断形参坐标在按钮的区域内
bool hitButton(const QPoint & pos) const override; //继承自父类
bool event(QEvent * e) override;
void paintEvent(QPaintEvent * ) override;
void keyPressEvent(QKeyEvent * ) override;
void focusInEvent(QFocusEvent * ) override;
void focusOutEvent(QFocusEvent * ) override;
void mouseMoveEvent(QMouseEvent * ) override; //应该是重新实现了这些函数
public:
}; //完结 class QPushButton : public QAbstractButton
QT_END_NAMESPACE
#endif // QPUSHBUTTON_H
(2)
谢谢