当前位置:   article > 正文

第五节 PyQt5之QAbstractButton对象(抽象按键基类)_qlayout' represents a c++ abstract class and canno

qlayout' represents a c++ abstract class and cannot be instantiated

简介

  • 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/904318
推荐阅读
相关标签
  

闽ICP备14008679号