How to change the content of a qLabel at runtime?

Asked

Viewed 64 times

0

Good afternoon, you guys! I need each line of qLinedit to be numbered in ascending order from 1 to 24, I created a qLabel for this, but I don’t know how to make it change the numbering according to each line, I was only able to print a lot of [i] as shown in the image[here]1. What can I do?

    self.ql_coords = {}
    self.num = {}
    #nós

    lo_nodes = qg.QGridLayout()
    #no = qg.Qlabel('Node')
    x = qg.QLabel('x:')
    y = qg.QLabel('y:')
    z = qg.QLabel('z:')

    for i in range(0,24):
        self.num[str(i+1)+str(0)] = qg.QLabel('[i]') #cria a qlabel
        lo_nodes.addWidget(self.num[str(i+1)+str(0)], i+1, 0) #adiciona a qlabel ao layout
        for j in range(1,4):
            self.ql_coords[str(i)+str(j)] = qg.QLineEdit() #cria as qlineedits
            lo_nodes.addWidget(self.ql_coords[str(i)+str(j)], i+1, j) #adiciona as qlineedits ao layout
            lo_nodes.addWidget(x, 0, 1) 
            lo_nodes.addWidget(y, 0 ,2) 
            lo_nodes.addWidget(z, 0, 3) 
  • Changing text of a Qlabel at runtime is completely different from creating Qlabel at runtime, it would be good to clarify. If you want to create, just pass the string in the creation (which seems to be your case, but you are passing a fixed string instead of using the variable). If you want to change (ie change an existing Qlabel), you have to call the . setText method

1 answer

0

The parameter passed in the QLabel is what will be displayed. So, if you want the number to be displayed, you need to pass this number as a parameter in the form of string. Your code can be changed in the following ways:

If you are using Python 2:

self.num[str(i+1)+str(0)] = qg.QLabel('[%d]' % i+1) #cria a qlabel

If you are using Python 3:

self.num[str(i+1)+str(0)] = qg.QLabel('[{}]'.format(i+1)) #cria a qlabel

If you are using Python 3.6+:

self.num[str(i+1)+str(0)] = qg.QLabel(f'[{i+1}]') #cria a qlabel

I don’t know if the [] are required in your display. But for the above functions they are not required.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.