Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Nested QGraphicsWidgets in QGraphicsScene

Nested QGraphicsWidgets in QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 376 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Padgaus
    wrote on last edited by Padgaus
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not use a QGraphicsTextItem ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Padgaus
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved