Results 1 to 2 of 2

Thread: How to create a matrix of Qlabels

  1. #1
    Join Date
    Dec 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to create a matrix of Qlabels

    I want to create somekind of deteriorating shields for my college project, so I went with this kind of implementation. But for some reason when I start my game it completly freeze's. Is it because I didn't write my code in a correct way? Can someone try to correct it.


    I have a class, looking like this:

    from PyQt5.QtWidgets import QLabel

    col = 14
    row = 8


    class ShieldLabel:
    def __init__(self, pix, x, y, width, height):
    self.livingObjects = [row][col]

    for i in range(0, row):
    for j in range(0, col):
    self.shieldPart = QLabel()
    self.shieldPart.setPixmap(pix)
    self.shieldPart.setGeometry(x + (i * 8), y + (j * 8), width, height)
    self.setStyleSheet("background:transparent")

    self.livingObjects[i, j] = self.shieldPart

    And in my main I call it like this: (it's in a loop because I want 4 objects of 2d array from my class above)
    def show_shields(self):
    for i in range(0, 4):
    new_shield = ShieldLabel(QPixmap("Images/shieldPart.png"), 120 + (i * 300), 480, 80, 80)
    self.shieldArray.append(new_shield)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,692
    Thanks
    13
    Thanked 1,599 Times in 1,527 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create a matrix of Qlabels

    Quote Originally Posted by Lugi View Post
    I want to create somekind of deteriorating shields
    really do not know what this means
    for my college project, so I went with this kind of implementation. But for some reason when I start my game it completly freeze's.
    You will need to define freezes. The code you have posted is not complete enough to run and nothing there should "freeze."
    Is it because I didn't write my code in a correct way?
    Maybe. At a glance your ShieldLabel objects will each contain 14x8 label object with a notional 80x80 size but absolutely positioned on an 8x8 spacing, i.e. mostly overlapping. Not sure if that is what you intended. Nothing actually displays these widgets in the code we see. If you simply construct these structures but never arrange from them to be shown before calling app.exec() you'll get no GUI but the event loop will be running (perhaps that is your freezes).

    Your code in [code] [/code] block
    Qt Code:
    1. from PyQt5.QtWidgets import QLabel
    2.  
    3. col = 14
    4. row = 8
    5.  
    6.  
    7. class ShieldLabel:
    8. def __init__(self, pix, x, y, width, height):
    9. self.livingObjects = [row][col]
    10.  
    11. for i in range(0, row):
    12. for j in range(0, col):
    13. self.shieldPart = QLabel()
    14. self.shieldPart.setPixmap(pix)
    15. self.shieldPart.setGeometry(x + (i * 8), y + (j * 8), width, height)
    16. self.setStyleSheet("background:transparent")
    17.  
    18. self.livingObjects[i, j] = self.shieldPart
    To copy to clipboard, switch view to plain text mode 
    And in my main I call it like this: (it's in a loop because I want 4 objects of 2d array from my class above)
    Qt Code:
    1. def show_shields(self):
    2. for i in range(0, 4):
    3. new_shield = ShieldLabel(QPixmap("Images/shieldPart.png"), 120 + (i * 300), 480, 80, 80)
    4. self.shieldArray.append(new_shield)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Create interactive matrix using QGraphicsFramework
    By Scope in forum Qt Programming
    Replies: 3
    Last Post: 28th June 2016, 15:08
  2. can we create a dot matrix display in qt
    By navi.ragula in forum Qt Programming
    Replies: 0
    Last Post: 29th October 2012, 08:24
  3. dynamically create QLabels
    By jeffmetal in forum Newbie
    Replies: 5
    Last Post: 19th June 2011, 19:24
  4. How to show QLabels
    By Pavel Abrosimov in forum Qt Programming
    Replies: 24
    Last Post: 9th July 2009, 10:39
  5. Dynamic QLabels
    By rajeshs in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2008, 12:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.