QT6 源(68):阅读与注释按钮类 QAbstractButton,及各种属性验证,QT 为按钮中的图片编码的过程,

(1)首先给出按钮类的继承关系

在这里插入图片描述

(2) 为按钮设置快捷方式,就是字符前面带个 & 符号

在这里插入图片描述

++

在这里插入图片描述

(3) QT 为按钮中的图片编码的过程 :

在这里插入图片描述

++ 以下代码来自 UIC 编译器生成的界面生成头文件 ui_widget.h

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtGui/QIcon>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QVBoxLayout * verticalLayout_3; //基类 widget 的布局
    QGroupBox   * groupBox;
    QVBoxLayout * verticalLayout_2; // groupbox 的布局
    QPushButton * pushButton;
    QPushButton * pushButton_2;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(259, 227);
        
        QFont font;
        font.setPointSize(14);
        Widget->setFont(font);
        
        verticalLayout_3 = new QVBoxLayout(Widget); //设置窗体的布局
        verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
        
        groupBox = new QGroupBox(Widget); //为窗体添加容器,并将容器设置为垂直布局
        groupBox->setObjectName(QString::fromUtf8("groupBox"));
        groupBox->setFlat(false);
        verticalLayout_2 = new QVBoxLayout(groupBox);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        
        pushButton = new QPushButton(groupBox);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        QIcon icon;    //注意此处的为图片对象的赋值 QIcon::QIcon(const QString & fileName);
        icon.addFile(QString::fromUtf8(":/icon/images/322.bmp"), QSize(), 
        			 QIcon::Normal, QIcon::Off);
        pushButton->setIcon(icon);
        verticalLayout_2->addWidget(pushButton); //把按钮添加进容器

        pushButton_2 = new QPushButton(groupBox);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        QIcon icon1;
        icon1.addFile(QString::fromUtf8(":/icon/images/324.bmp"), QSize(),
                      QIcon::Normal, QIcon::Off);
        pushButton_2->setIcon(icon1);
        verticalLayout_2->addWidget(pushButton_2); //把按钮添加进容器

        verticalLayout_3->addWidget(groupBox);     //把容器添加进窗体
        
        retranslateUi(Widget); //调用下面的成员函数重新绘制窗体

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget      ->setWindowTitle(QCoreApplication::translate("Widget", "Widget2-1", nullptr));
        groupBox    ->setTitle(QCoreApplication::translate("Widget", "GroupBoxA"    , nullptr));
        pushButton  ->setText (QCoreApplication::translate("Widget", "&PushButton"  , nullptr));
        pushButton_2->setText (QCoreApplication::translate("Widget", "Push&Button_2", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

(4)基于本类的信号函数与槽函数的实验验证

在这里插入图片描述

(5)附上完整源代码

#ifndef QABSTRACTBUTTON_H
#define QABSTRACTBUTTON_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtGui/qicon.h>
#if QT_CONFIG(shortcut)
#  include <QtGui/qkeysequence.h>
#endif
#include <QtWidgets/qwidget.h>

QT_REQUIRE_CONFIG(abstractbutton);

QT_BEGIN_NAMESPACE //说明本类定义在  QT的全局空间


class QButtonGroup;
class QAbstractButtonPrivate;

/*
这个类实现了一个抽象的按钮。这个类的子类处理用户操作,并指定如何绘制按钮。
QAbstractButton provides support for both push buttons and checkable (toggle) buttons.
Checkable buttons are implemented in the QRadioButton and QCheckBox classes.
Push buttons are implemented in the QPushButton and QToolButton classes;
these also provide toggle behavior if required.
如果需要,这些类也提供切换行为。
任何按钮都可以显示包含文本和图标的标签。setText()设置文本;setlcon()设置图标。
如果按钮被禁用,则其标签将更改,以使按钮具有“disabled”外观。
如果按钮是一个文本按钮,其字符串包含一个与符号('&’),QAbstractButton会自动创建一个快捷键。例如:
    QPushButton *button = new QPushButton(tr("Ro&ck && Roll"), this);
Alt+C快捷键被分配给该按钮,即当用户按下Alt+C时,该按钮将调用animateClick()方法。
请参说OShortcut 文档以获取详细信息。要显示实际的 ampersand,,请使用'&&。
您还可以使用setShortcut)函数设置自定义快捷键。
这对于没有任何文字,因此不能有任何自动快捷方式的按钮来说非常有用。
    button->setIcon(QIcon(":/images/print.png"));
    button->setShortcut(tr("Alt+F7"));

All the buttons provided by Qt (QPushButton, QToolButton, QCheckBox, and QRadioButton)
can display both text and icons. //换言之 default与 autodefault属性只有在对话框里才有意义
A button can be made the default button in a dialog by means of
QPushButton::setDefault() and QPushButton::setAutoDefault().

QAbstractButton provides most of the states used for buttons:
    isDown()    indicates whether the button is pressed down.
    isChecked() indicates whether the button is checked.
                    Only checkable buttons can be checked and unchecked (see below).
    isEnabled() indicates whether the button can be pressed by the user.
                    Note: As opposed to other widgets,
                    buttons derived from QAbstractButton accept mouse and context
                    menu events when disabled.

setAutoRepeat() sets whether the button will auto-repeat if the user holds it down.
autoRepeatDelay and autoRepeatInterval define how auto-repetition is done.

setCheckable () sets whether the button is a toggle button or not.

The difference between isDown() and isChecked() is as follows.
When the user clicks a toggle button to check it,
the button is first pressed then released into the checked state.
When the user clicks it again (to uncheck it),
the button moves first to the pressed state,
then to the unchecked state (isChecked() and isDown() are both false).


QAbstractButton provides four signals:
    pressed () is emitted when the left mouse button is pressed while the
        mouse cursor is inside the button.
    released() is emitted when the left mouse button is released.
    clicked () is emitted when the button is first pressed and then released,
        when the shortcut key is typed,
        or when click() or animateClick() is called.
    toggled () is emitted when the state of a toggle button changes.

To subclass QAbstractButton, you must reimplement at least paintEvent() to draw the
button's outline and its text or pixmap. It is generally advisable to
reimplement sizeHint() as well, and sometimes hitButton()
(to determine whether a button press is within the button).
For buttons with more than two states (like tri-state buttons),
you will also have to reimplement checkStateSet() and nextCheckState().

*/

class Q_WIDGETS_EXPORT QAbstractButton : public QWidget
{
    Q_OBJECT

    //这个属性持有按钮上显示的文本。
    //如果按钮没有文本,则文本()函数将返回空字符串。
    //如果文本包含 ampersand字符(&'),则会自动为其创建快捷方式。“&”后面的字符将用作快捷键。
    //如果文本中没有定义任何快捷方式,则之前的任何快捷方式都将被覆盖或清除。
    //有关详细信息,请参阅OShortcut文档。要显示实际的 ampersand,请使用“&&”。
    //没有默认文本。
    Q_PROPERTY(QString text READ text WRITE setText)

    //class Q_GUI_EXPORT QIcon ; 可见此图片类并不继承于 QWidget 或 QObject
    //QIcon(const QString & fileName); // file or resource name 这是其重要的构造函数
    Q_PROPERTY(QIcon   icon READ icon WRITE setIcon)
    //This property holds the icon shown on the button。
    //The icon's default size is defined by the GUI style,
    //but can be adjusted by setting the iconSize property.

    Q_PROPERTY(QSize   iconSize READ iconSize WRITE setIconSize)
    //此属性保存用于此按钮的图标大小。class QSize { int wd; int ht; };
    //默认大小由 GUI样式定义。这是图标的最大大小。较小的图标不会被放大。

#ifndef QT_NO_SHORTCUT //此属性持有与按钮关联的助记符.
    Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
#endif  //This property holds the mnemonic associated with the button

    //This property holds whether the button is checkable.
    //By default, the button is not checkable.
    //所谓   checkable是指用按钮按下时会记忆与切换选中与否的状态,
    //未启用 checkable则按钮按下时不会记忆前一次的状态。
    Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable)

    Q_PROPERTY(bool checked   READ isChecked   WRITE setChecked NOTIFY toggled USER true)
    //This property holds whether the button is checked
    //Only checkable buttons can be checked. By default, the button is unchecked.

    //This property holds whether the button is pressed down。
    //If this property is true, the button is pressed down.
    //The signals pressed() and clicked() are not emitted if you set this property to true. 、
    //															The default is false.
    Q_PROPERTY(bool down    READ isDown    WRITE setDown DESIGNABLE false)

    //This property holds whether auto-exclusivity is enabled。
    //If auto-exclusivity is enabled, checkable buttons that belong to the same
    //parent widget behave as if they were part of the same exclusive button group.
    //In an exclusive button group, only one button can be checked at any time;
    //checking another button automatically unchecks the previously checked one.
    //The property has no effect on buttons that belong to a button group.
    //autoExclusive is off by default, except for radio buttons.
    //互斥属性仅对 checkable按钮有效, //对普通按钮无效
    Q_PROPERTY(bool autoExclusive READ autoExclusive WRITE setAutoExclusive) //构成单选按钮

    //This property holds whether autoRepeat is enabled。
    //If autoRepeat is enabled, then the pressed(), released(), and clicked() signals are
    //emitted at regular intervals when the button is down.
    //autoRepeat is off by default. The initial delay and the repetition interval are
    //defined in milliseconds by autoRepeatDelay and autoRepeatInterval.
    //Note: If a button is pressed down by a shortcut key,
    //then  auto-repeat is enabled and timed by the system and not by this class. 、
    //The pressed(), released(), and clicked() signals will be emitted like in the
    //normal case.
    Q_PROPERTY(bool autoRepeat         READ autoRepeat         WRITE setAutoRepeat)

    Q_PROPERTY(int  autoRepeatDelay    READ autoRepeatDelay    WRITE setAutoRepeatDelay)
    //This property holds the initial delay of auto-repetition。
    //If autoRepeat is enabled, then autoRepeatDelay defines the initial delay in
    //milliseconds before auto-repetition kicks in.

    Q_PROPERTY(int  autoRepeatInterval READ autoRepeatInterval WRITE setAutoRepeatInterval)
    //This property holds the interval of auto-repetition。
    //If autoRepeat is enabled, then autoRepeatInterval defines the length of the
    //auto-repetition interval in millisecons.

private:
    Q_DECLARE_PRIVATE(QAbstractButton)
    Q_DISABLE_COPY(QAbstractButton)
    friend class QButtonGroup;    //还有这么哥友元类

protected:
    QAbstractButton(QAbstractButtonPrivate & dd, QWidget * parent = nullptr);

public:
    //Constructs an abstract button with a parent.  有参构造函数
    explicit QAbstractButton(QWidget * parent = nullptr);

    ~QAbstractButton();

//Q_PROPERTY(QString text READ text WRITE setText)
                    QString    text() const;
                    void    setText(const QString &text);

//Q_PROPERTY(QIcon  icon READ icon WRITE setIcon)
                    QIcon      icon() const;
                    void    setIcon(const QIcon & icon);

//Q_PROPERTY(QSize  iconSize READ iconSize WRITE setIconSize)
                    QSize         iconSize() const;
public Q_SLOTS:
                    void       setIconSize(const QSize & size);

public :

#ifndef QT_NO_SHORTCUT
//Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
                        QKeySequence    shortcut() const;
                        void         setShortcut(const QKeySequence &key);
#endif

//Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable)
                        bool     isCheckable() const;
                        void    setCheckable(bool);

//Q_PROPERTY(bool checked   READ isChecked   WRITE setChecked NOTIFY toggled USER true)
                        bool     isChecked() const;
public Q_SLOTS:
                        void    setChecked(bool);
Q_SIGNALS:
                        void    toggled(bool checked);

public :
//Q_PROPERTY(bool down READ isDown WRITE setDown DESIGNABLE false)
                    bool    isDown() const;
                    void   setDown(bool);

//Q_PROPERTY(bool autoExclusive READ autoExclusive WRITE setAutoExclusive) //构成单选按钮
                            bool     autoExclusive() const;
                            void  setAutoExclusive(bool);

//Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat)
                        bool      autoRepeat() const;
                        void   setAutoRepeat(bool);

//Q_PROPERTY(int  autoRepeatDelay  READ  autoRepeatDelay  WRITE setAutoRepeatDelay)
                                int      autoRepeatDelay() const;
                                void  setAutoRepeatDelay(int);


//Q_PROPERTY(int  autoRepeatInterval READ autoRepeatInterval WRITE setAutoRepeatInterval)
                                int       autoRepeatInterval() const;
                                void   setAutoRepeatInterval(int);


#if QT_CONFIG(buttongroup)
    //class QButtonGroup : public QObject //此类貌似是处理按钮组
    QButtonGroup * group() const;
    //Returns the group that this button belongs to.
    //If the button is not a member of any QButtonGroup, this function returns nullptr.
#endif

public Q_SLOTS: //本按钮类的定义好的槽函数,直接供别的信号函数使用
  //void setIconSize(const QSize &size);
  //void setChecked(bool);

    void animateClick();
    //Performs an animated click:  执行动画点击:
    //the button is pressed immediately, and released 100ms later.
    //Calling this function again before the button is released resets the release timer.
    //All signals associated with a click are emitted as appropriate.
    //This function does nothing if the button is disabled.

    void toggle(); //Toggles the state of a checkable button.

    void click ();
    //Performs a click.
    //All the usual signals associated with a click are emitted as appropriate.
    //If the button is checkable, the state of the button is toggled.
    //This function does nothing if the button is disabled.

Q_SIGNALS:
    void clicked(bool checked = false);
  //void toggled(bool checked);
    void pressed (); //This signal is emitted when the button is pressed down.
    void released(); //This signal is emitted when the button is released.

protected:
    //Returns true if pos is inside the clickable button rectangle;
    //otherwise returns false.
    //By default, the clickable area is the entire widget.
    //Subclasses may reimplement this function to provide support for clickable
    //areas of different shapes and sizes.
    virtual bool hitButton(const QPoint & pos) const; //判断形参坐标在按钮的区域内

    //This virtual handler is called when setChecked() is used,
    //unless it is called from within nextCheckState().
    //It allows subclasses to reset their intermediate button states.
    virtual void checkStateSet ();

    virtual void nextCheckState();
    //This virtual handler is called when a button is clicked.
    //The default implementation calls setChecked(!isChecked()) //切换与改变复选状态
    //                                                  if the button isCheckable().
    //It allows subclasses to implement intermediate button states.

    void        paintEvent(QPaintEvent * e) override = 0;
    bool             event(QEvent      * e) override;
    void     keyPressEvent(QKeyEvent   * e) override;
    void   keyReleaseEvent(QKeyEvent   * e) override;
    void   mousePressEvent(QMouseEvent * e) override;
    void mouseReleaseEvent(QMouseEvent * e) override;
    void    mouseMoveEvent(QMouseEvent * e) override;
    void      focusInEvent(QFocusEvent * e) override;
    void     focusOutEvent(QFocusEvent * e) override;
    void       changeEvent(QEvent      * e) override;
    void        timerEvent(QTimerEvent * e) override;

}; //完结 class Q_WIDGETS_EXPORT : public QWidget

QT_END_NAMESPACE

#endif // QABSTRACTBUTTON_H

(6)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值