(1)
#ifndef QCHECKBOX_H
#define QCHECKBOX_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qabstractbutton.h>
QT_REQUIRE_CONFIG(checkbox);
QT_BEGIN_NAMESPACE //说明本类定义在 QT的全局空间
class QCheckBoxPrivate;
class QStyleOptionButton;
/*
QCheckBox是一个开关按钮,可以打开(选中)或关闭(未选中)。
复选框通常用于表示应用程序中的功能,这些功能可以被启用或禁用而不会影响其他功能。
可以实施不同类型的行为。例如,可以使用OButtonGroup将复选按钮逻辑分组,允许独占复选框。
但是,OButtonGroup不提供任何视觉表示。
每当复选框被选中或清除时,它都会发出信号 stateChanged().
如果您想每次复选框状态改变时触发一个动作,请连接到这个信号。
您可以使用isChecked()来查询复选框是否被选中。
除了通常的已勾选和未勾选状态外,QCheckBox可选地提供第三种状态来表示“无变化”。
这在你需要为用户提供既不勾选也不取消勾选复选框的选项时非常有用。
如果你需要这种第三种状态,可以使用setTristate()来启用它,并使用checkState()来查询当前切换状态。
就像QPushButton一样,复选框显示文本,并可选地显示一个小图标。图标设置为setlcon()。
文本可以在构造函数中设置,也可以用setText()设置。快捷键可以通过在首选字符前加上&号来指定。例如:
QCheckBox * checkbox = new QCheckBox("C&ase sensitive", this);
*/
class Q_WIDGETS_EXPORT QCheckBox : public QAbstractButton
{
Q_OBJECT //又插入了此宏
Q_PROPERTY(bool tristate READ isTristate WRITE setTristate)
//This property holds whether the checkbox is a tri-state checkbox。
//The default is false, i.e., the checkbox has only two states.
private:
Q_DECLARE_PRIVATE(QCheckBox)
Q_DISABLE_COPY(QCheckBox)
friend class QAccessibleButton;
public:
//Constructs a checkbox with the given parent, but with no text.
//parent is passed on to the QAbstractButton constructor.
explicit QCheckBox(QWidget * parent = nullptr);
explicit QCheckBox(const QString & text, QWidget * parent = nullptr);
//Constructs a checkbox with the given parent and text.
~QCheckBox();
//Q_PROPERTY(bool tristate READ isTristate WRITE setTristate)
bool isTristate() const;
void setTristate(bool y = true);
//Returns the checkbox's check state. If you do not need tristate support,
//you can also use QAbstractButton::isChecked(), which returns a boolean.
Qt::CheckState checkState() const;
void setCheckState(Qt::CheckState state);
//Qt::enum CheckState { Unchecked, PartiallyChecked, Checked };
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
Q_SIGNALS: //为本类,复选框,增加了一个信号发射函数
void stateChanged(int state);
//This signal is emitted whenever the checkbox's state changes, i.e.,
//whenever the user checks or unchecks it.
//state contains the checkbox's new Qt::CheckState.
protected:
virtual void initStyleOption(QStyleOptionButton *option) const;
bool hitButton(const QPoint &pos) const override;
void checkStateSet() override;
void nextCheckState() override;
bool event(QEvent * e) override;
void paintEvent(QPaintEvent * ) override;
void mouseMoveEvent(QMouseEvent * ) override;
}; //完结 class QCheckBox : public QAbstractButton
QT_END_NAMESPACE
#endif // QCHECKBOX_H
(2)
谢谢