Pergunta

I have several Spinboxes and I want them to change the background color, if the value is changed.

With this connect command:

self.doubleSpinBox_1.valueChanged.connect(self.color)

My first try was:

def color(self):
   send = self.sender()
   emitter = send.text()

The Problem is, that "send.text()" returns the changed value, and not the spinbox witch emmited the signal. In the documentation about QDoubleSpinBox I searched for something like "setName", or something to identify my sender-spinbox, but I didnt find anything.

Foi útil?

Solução

self.sender() is the spinbox which emitted the signal. You are effectively calling doubleSpinBox_1.text() which of course gives you the text in the spinbox.

So just write mySpinBox = self.sender() and you will be right.

Outras dicas

You can just use the is operator to identify the sender:

def color(self):
    spinbox = self.sender()
    if spinbox is self.doubleSpinBox_1:
        # do something with doubleSpinBox_1
    elif spinbox is self.doubleSpinBox_2:
        # do something with doubleSpinBox_1
    ...

But if really do want to give your widgets a name, you can use setObjectName for that:

    self.doubleSpinBox_1.setObjectName('spinbox1')
    print(self.doubleSpinBox_1.objectName())

PS: if your widgets were created via Qt Designer, they will automatically have their objectName set (it will be the same as their attribute name).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top