简介
- QAbstractButton是所有按钮控件的基类,提供按钮的通用功能。
- 继承自QWidget;
- 由于这是一个抽象类,所以在使用是需要将其子类化;
window = QWidget()
window.resize(500, 500)
# 直接使用下列方法创建会报错,抽象类无法直接使用
bnt = QAbstractButton(window)
bnt.setText('按钮')
# TypeError: PyQt5.QtWidgets.QAbstractButton represents a C++ abstract class and cannot be instantiated
# 要是用抽象类,必须将抽象类子类化,下面介绍子类化的方法
class Bnt(QAbstractButton)
pass
bnt = QAbstractButton(window)
bnt.setText('按钮')
# 运行以上代码程序仍然会报错,但是报错的类型改变了
# NotImplementedError: QAbstractButton.paintEvent() is abstract and must be overridden
# 错误提示,有一个方法是必须重写的“paintEvent()”
# 正确的方法如下,但是并不能看到该控件
class Bnt(QAbstractButton)
def paintEvent(self, evt