PyQt application architecture

Posted by L. De Leo on Programmers See other posts from Programmers or by L. De Leo
Published on 2012-10-21T13:09:53Z Indexed on 2012/10/21 17:18 UTC
Read the original article Hit count: 217

Filed under:
|
|
|

I'm trying to give a sound structure to a PyQt application that implements a card game. So far I have the following classes:

  • Ui_Game: this describes the ui of course and is responsible of reacting to the events emitted by my CardWidget instances

  • MainController: this is responsible for managing the whole application: setup and all the subsequent states of the application (like starting a new hand, displaying the notification of state changes on the ui or ending the game)

  • GameEngine: this is a set of classes that implement the whole game logic

Now, the way I concretely coded this in Python is the following:

class CardWidget(QtGui.QLabel):
    def __init__(self, filename, *args, **kwargs):
        QtGui.QLabel.__init__(self, *args, **kwargs)
        self.setPixmap(QtGui.QPixmap(':/res/res/' + filename))

    def mouseReleaseEvent(self, ev):
        self.emit(QtCore.SIGNAL('card_clicked'), self)

class Ui_Game(QtGui.QWidget):
    def __init__(self, window, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self.setupUi(window)
        self.controller = None

    def place_card(self, card):
        cards_on_table = self.played_cards.count() + 1
        print cards_on_table

        if cards_on_table <= 2:
            self.played_cards.addWidget(card)
            if cards_on_table == 2:
                self.controller.play_hand()

class MainController(object):
    def __init__(self):
        self.app = QtGui.QApplication(sys.argv)
        self.window = QtGui.QMainWindow()
        self.ui = Ui_Game(self.window)
        self.ui.controller = self
        self.game_setup()

Is there a better way other than injecting the controller into the Ui_Game class in the Ui_Game.controller? Or am I totally off-road?

© Programmers or respective owner

Related posts about architecture

Related posts about mvc