Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Multiple backgrounds colors one for each line of a Qlabel (pyQt5)
Qt 6.11 is out! See what's new in the release blog

Multiple backgrounds colors one for each line of a Qlabel (pyQt5)

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 1 Posters 771 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.
  • O Offline
    O Offline
    otaolafr
    wrote on last edited by
    #1

    hello,
    I am doing an interface that presents different groups of objects (outside of the interface) where each of them has a different color. the idea is to create an easy way to see which line corresponds to which object. for this reason, in the pyQt interface, I am creating an small qLabel that i leave 'empty' of text, and i change the background of it.
    this worked nicely as intended see groups 0, 2, 4, 6. my 'issue' or what I am having trouble with, is that now some objects are parents with their own children (so some groups are dependent of others), and I can not get around to make the 'square' of color, of all the different colors for the parent and children groups. the part of the code that modify this, is cited below, I am not 100% if this is feasible at all, just for info, it remaining color for the block

            numberOfGroupsName=" "
            colorOfTheGroup="background:rgb("+str(Colors[i][0])+","+str(Colors[i][1])+","+str(Colors[i][2])+")"
            if i in listOfAxisParents:
                for j in relationsOfAxis:
                    if i==j[0]:
                        for k in j[1]:
                            numberOfGroupsName=numberOfGroupsName+"<br> "
                            colorOfTheGroup=colorOfTheGroup+",rgb("+str(Colors[k][0])+","+str(Colors[k][1])+","+str(Colors[k][2])+")"
            colorOfTheGroup=colorOfTheGroup+";"
            w[i] = QLabel(numberOfGroupsName)
            w[i].setStyleSheet(colorOfTheGroup)
            w[i].setFixedWidth(20)
            layout_1.addWidget(w[i],i+1,0,1,1)
    

    (the one that should be divided into 3 for this example, it is colored as the color of the last group in position k, so group 5)

    ![alt text](93ee037a-97e7-40a0-adc7-05825e4d3aaa-image.png image url)
    I am not sure if the issue commes from the way I created the variable colorOfTheGroup, meaning I might be misswriting this, or if it is even possible to give, 'different backgrounds to different lines of a qlabel'
    thanks

    O 2 Replies Last reply
    0
    • O otaolafr

      hello,
      I am doing an interface that presents different groups of objects (outside of the interface) where each of them has a different color. the idea is to create an easy way to see which line corresponds to which object. for this reason, in the pyQt interface, I am creating an small qLabel that i leave 'empty' of text, and i change the background of it.
      this worked nicely as intended see groups 0, 2, 4, 6. my 'issue' or what I am having trouble with, is that now some objects are parents with their own children (so some groups are dependent of others), and I can not get around to make the 'square' of color, of all the different colors for the parent and children groups. the part of the code that modify this, is cited below, I am not 100% if this is feasible at all, just for info, it remaining color for the block

              numberOfGroupsName=" "
              colorOfTheGroup="background:rgb("+str(Colors[i][0])+","+str(Colors[i][1])+","+str(Colors[i][2])+")"
              if i in listOfAxisParents:
                  for j in relationsOfAxis:
                      if i==j[0]:
                          for k in j[1]:
                              numberOfGroupsName=numberOfGroupsName+"<br> "
                              colorOfTheGroup=colorOfTheGroup+",rgb("+str(Colors[k][0])+","+str(Colors[k][1])+","+str(Colors[k][2])+")"
              colorOfTheGroup=colorOfTheGroup+";"
              w[i] = QLabel(numberOfGroupsName)
              w[i].setStyleSheet(colorOfTheGroup)
              w[i].setFixedWidth(20)
              layout_1.addWidget(w[i],i+1,0,1,1)
      

      (the one that should be divided into 3 for this example, it is colored as the color of the last group in position k, so group 5)

      ![alt text](93ee037a-97e7-40a0-adc7-05825e4d3aaa-image.png image url)
      I am not sure if the issue commes from the way I created the variable colorOfTheGroup, meaning I might be misswriting this, or if it is even possible to give, 'different backgrounds to different lines of a qlabel'
      thanks

      O Offline
      O Offline
      otaolafr
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • O otaolafr

        hello,
        I am doing an interface that presents different groups of objects (outside of the interface) where each of them has a different color. the idea is to create an easy way to see which line corresponds to which object. for this reason, in the pyQt interface, I am creating an small qLabel that i leave 'empty' of text, and i change the background of it.
        this worked nicely as intended see groups 0, 2, 4, 6. my 'issue' or what I am having trouble with, is that now some objects are parents with their own children (so some groups are dependent of others), and I can not get around to make the 'square' of color, of all the different colors for the parent and children groups. the part of the code that modify this, is cited below, I am not 100% if this is feasible at all, just for info, it remaining color for the block

                numberOfGroupsName=" "
                colorOfTheGroup="background:rgb("+str(Colors[i][0])+","+str(Colors[i][1])+","+str(Colors[i][2])+")"
                if i in listOfAxisParents:
                    for j in relationsOfAxis:
                        if i==j[0]:
                            for k in j[1]:
                                numberOfGroupsName=numberOfGroupsName+"<br> "
                                colorOfTheGroup=colorOfTheGroup+",rgb("+str(Colors[k][0])+","+str(Colors[k][1])+","+str(Colors[k][2])+")"
                colorOfTheGroup=colorOfTheGroup+";"
                w[i] = QLabel(numberOfGroupsName)
                w[i].setStyleSheet(colorOfTheGroup)
                w[i].setFixedWidth(20)
                layout_1.addWidget(w[i],i+1,0,1,1)
        

        (the one that should be divided into 3 for this example, it is colored as the color of the last group in position k, so group 5)

        ![alt text](93ee037a-97e7-40a0-adc7-05825e4d3aaa-image.png image url)
        I am not sure if the issue commes from the way I created the variable colorOfTheGroup, meaning I might be misswriting this, or if it is even possible to give, 'different backgrounds to different lines of a qlabel'
        thanks

        O Offline
        O Offline
        otaolafr
        wrote on last edited by
        #3

        to reproduce it the easiest possible:

        from PyQt5.QtWidgets import QWidget, QMessageBox
        from PyQt5 import QtCore, QtGui
        import PyQt5.QtCore as QtCore
        from PyQt5.QtWidgets import *
        from PyQt5.QtCore import Qt
        
        dialog = QDialog()
        dialog.setWindowTitle("Input size")
        layout_0 = QGridLayout(dialog)
        
        numberOfGroupsName="X<br>X<br>X<br>X"
        colorOfTheGroup="background:rgb(0,255,0),rgb(213,1,242),rgb(255,127,0),rgb(9,138,204);"
        w= QLabel(numberOfGroupsName)
        layout_0.addWidget(w,0,0,1,1)
        w.setStyleSheet(colorOfTheGroup)
        w.setFixedWidth(20)
        
        dialog.show()
        

        here we can see that the different lines of w, (that is X,X,X,X) show all the same last color (ie., rgb(9,138,204))

        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