本系列文章长期更新修改.
QCheckBox,就是一个打勾的控件.
属性:
Methods
- __init__ (self, QWidget parent = None)
- __init__ (self, QString text, QWidget parent = None)
- Qt.CheckState checkState (self)
- checkStateSet (self)
- bool event (self, QEvent e)
- bool hitButton (self, QPoint pos)
- initStyleOption (self, QStyleOptionButton option)
- bool isTristate (self)
- mouseMoveEvent (self, QMouseEvent)
- nextCheckState (self)
- paintEvent (self, QPaintEvent)
- setCheckState (self, Qt.CheckState state)
- setTristate (self, bool y = True)
- QSize sizeHint (self)
Qt Signals
- void stateChanged (int)
详细分析:
1.主要设置
QCheckBox的主要设置是文本和icon,用法和QPushButton一样,参考QPushButton篇.
2.快捷键
QCheckBox的快捷键用法参考QPushButton篇.
3.打勾状态
QCheckBox默认有两种状态,打勾和空白,可以通过下面函数获取和设置:
- bool isChecked (self)
- setChecked (self, bool)
4.三状态
QCheckBox允许设置成三种状态,打勾,正方形,空白.文档对正方形的解释是"No change".
把属性tristate设置为true,则QCheckBox会变成3状态,相关函数如下:
- bool isTristate (self)
- setTristate (self, bool y = True)
这样的话,作为bool值的checked属性并不能表达三种状态,我们需要状态值checkState,它是一个枚举量.
- Qt.CheckState checkState (self)
- setCheckState (self, Qt.CheckState state)
枚举量 | 值 | 描述 | isChecked() |
---|---|---|---|
Qt.Unchecked | 0 | 空白 | false |
Qt.PartiallyChecked | 1 | 正方形 | true |
Qt.Checked | 2 | 打勾 | true |
注意,二状态的QCheckBox也能使用checkState属性,它会在0和2之间变化.
5.信号
QCheckBox就一个特有信号,并且含义很清晰,就是当状态改变时激活,参数是checkState的值.
- void stateChanged (int)
6.改变状态函数
QCheckBox提供了一个动作函数来改变状态.
2状态时,它会在空白和打勾之间改变,3状态时,则会在空白,正方形,打勾不断循环.
- nextCheckState (self)
7.待续
bool hitButton (self, QPoint pos)
checkStateSet (self)
bool event (self, QEvent e)
initStyleOption (self, QStyleOptionButton option)
mouseMoveEvent (self, QMouseEvent)
paintEvent (self, QPaintEvent)
QSize sizeHint (self)