Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Nested QGraphicsWidgets in QGraphicsScene



  • Hi all,

    I've been trying rack my brain around the QGraphics framework and i think i don't understand how it works.
    I've that Monopoly game and switched from using "normal" widgets to QGraphics and I tried replicating what i had before

    But I have that instead:
    Capture d’écran de 2019-10-28 15-43-32.png

    Here is my code:

    class Board(QGraphicsView):
       def __init__(self):
          super().__init__()
    
          self.scene = QGraphicsScene(parent = self)
          self.board = QGraphicsWidget()
          self.board_layout = QGraphicsGridLayout()
    
          self.properties = [
             "Free Parking", "Strand", "Fleet Street", "Chance", "Trafalgar Square",
             "Fenchurch Street station", "Leicester Square", "Coventry Street", "Water Works", "Piccadilly", "Go to Jail",
             "Vine Street", "", "", "", "", "", "", "", "", "", "Regent Street",
             "Marlborough Street", "", "", "", "", "", "", "", "", "", "Oxford Street",
             "Community Chest", "", "", "", "", "", "", "", "", "", "Community Chest",
             "Bow Street", "", "", "", "", "", "", "", "", "", "Bond Street",
             "Marylebine station", "", "", "", "", "", "", "", "", "", "Liverpool Street station",
             "Northumberland Avenue", "", "", "", "", "", "", "", "", "", "Chance",
             "Whitehall", "", "", "", "", "", "", "", "", "", "Park Lane",
             "Electric Company", "", "", "", "", "", "", "", "", "", "Super Tax",
             "Pall Mall", "", "", "", "", "", "", "", "", "", "Mayfair",
             "Visit Jail", "Pentonville Road", "Euston Road", "Chance", "The Angel Islington", "King's Cross station",
             "Income Tax", "Whitechapel Road", "Community Chest", "Old Kent Road", "Start"
          ]
    
          positions = [(i, j) for i in range(11) for j in range(11)]
    
          for position, name in zip(positions, self.properties):
             if name == "":
                continue
             
             tile = self.scene.addItem(Tile(name, self.scene))
             self.board_layout.addItem(tile, *position)
    
          self.board.setLayout(self.board_layout)
    
          self.scene.addItem(self.board)
          self.setScene(self.scene)
    
       def get_tile(self):
          for i in range(0, 40):
             prop = self.board_layout.itemAt(i)
             # true_position = self.board_positions[i]
             # print(f"Property: {property_name} {true_position}")
             print(prop.name)
    
    
    class Tile(QGraphicsWidget):
       def __init__(self, name, scene):
          super().__init__()
          self.name = name
          self.tokens = []
          self.layout = QGraphicsLinearLayout()
          self.layout.setOrientation(Qt.Vertical)
          text = scene.addItem(QGraphicsTextItem(self.name))
          self.layout.addItem(text)
    
       def has_tokens(self):
          return self.tokens
    
    
    class Token(QGraphicsWidget):
       def __init__(self):
          super().__init__()
    

    I created those 3 objects nested in some way to have a good grasp of what was going on on the board --> this tile is located here on the board, has those token, has this value, is owned by ...
    I think i'm not understanding QGraphics at all.
    Can anyone help me understand?


  • Lifetime Qt Champion

    Hi,

    Why not use a QGraphicsTextItem ?



  • Where? I'm already using a QGraphicsTextItem to try and display the name of the properties in the Tile object


  • Lifetime Qt Champion

    I meant only that class to create your board. Why are you using QGraphicsWidget at all ?


Log in to reply