0

我正在制作一个纸牌游戏来练习 OOP 和 PyQt5,但我无法将继承 的卡片添加QAbstractButton到布局(QGridLayoutQHBoxLayoutQVBoxLayout)中。这是Card对象的一部分:

class Card(QAbstractButton):    

    def __init__(self, rank=None, suit=None, parent=None):
        super().__init__(parent)
        self.rank = rank
        self.suit = suit
        self.visibility = False

    def paintEvent(self, e):
        painter = QPainter()
        painter.begin(self)
        if self.visibility == True:
            self.draw_card_front(painter)
        else:
            self.draw_card_back(painter)
        painter.end()

    def draw_card_back(self, painter):
        painter.setPen(COLOR_OUTLINE)
        painter.setBrush(COLOR_BACK)
        painter.drawRoundedRect(0, 0, CARD_WIDTH-1, CARD_HEIGHT-1, 10, 10)

    def draw_card_front(self, painter):
        painter.setPen(COLOR_OUTLINE)
        painter.setBrush(COLOR_FRONT)
        painter.drawRoundedRect(0, 0, CARD_WIDTH-1, CARD_HEIGHT-1, 10, 10)

        self.draw_rank(painter)
        self.draw_suit(painter)

    ...

这是游戏的类:

class Solitaire(QWidget):

    def __init__(self):
        super().__init__()
        self.score = 0

        self.initUI()


    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)

        self.card1 = Card(rank=1, suit=2, parent=self)
        self.card2 = Card(rank=1, suit=2, parent=self)

        grid.addWidget(self.card1, 0, 0)
        grid.addWidget(self.card2, 1, 0)

        self.setWindowTitle('Yay')
        self.setGeometry(300, 300, 400, 400)
        self.show()

    ...

if __name__ == '__main__':
    app = QApplication(sys.argv)
    game = Solitaire()
    app.exec_()

当我运行程序时,Card没有出现。但如果我不使用布局,则Card正常显示。如果我尝试将 a 添加QPushButton到布局中,它也可以正常工作。我觉得我缺少该parent属性的某些内容,或者我没有从类QAbstractButton中重载函数Card。任何人都可以建议吗?

4

1 回答 1

1

根据文档

要继承 QAbstractButton,您必须至少重新实现 paintEvent()以绘制按钮的轮廓及其文本或像素图。通常建议重新实现sizeHint()以及有时 hitButton() (以确定按钮按下是否在按钮内)。对于具有两个以上状态的按钮(如三态按钮),您还必须重新实现 checkStateSet() 和 nextCheckState()。

从上面我们得出结论,您必须实现paintEvent()负责绘制按钮的方法,这取决于您要绘制的内容,以及sizeHint()布局使用的大小的方法。

例如:

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Card(QAbstractButton):    

    def __init__(self, rank=None, suit=None, parent=None):
        super().__init__(parent)
        self.rank = rank
        self.suit = suit
        self.visibility = False

    def sizeHint(self):
        return QSize(100, 100)

    def paintEvent(self, e):
        painter = QPainter(self)
        if self.visibility:
            self.draw_card_front(painter)
        else:
            self.draw_card_back(painter)

    ...

class Solitaire(QWidget):

    def __init__(self):
        super().__init__()
        self.score = 0

        self.initUI()


    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)

        self.card1 = Card(rank=1, suit=2, parent=self)
        self.card2 = Card(rank=1, suit=2, parent=self)

        grid.addWidget(self.card1, 0, 0)
        grid.addWidget(self.card2, 1, 0)

        self.setWindowTitle('Yay')
        self.setGeometry(300, 300, 400, 400)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    game = Solitaire()
    app.exec_()
于 2018-05-24T03:53:14.873 回答