Pergunta

I am currently practicing GUI-programming using Python and (Py)Qt, and I ran into a bit of a problem.

I have a simple GUI which contains but two widgets: A QTableWidget and a QLabel. The table is populated with a list of names, which are actually attributes from objects. What I want to do is simple: - Click on a name (QTableWidgetItem) - Print out the information in the object (by a method defined in that class, all irrelevant for this)

So this is what I have:

    self.connect(self.scroll,
                 SIGNAL("itemClicked(QTableWidgetItem *)"),
                 self.updateMore 
                 )

The SIGNAL-argument seems to be okay, as far as I know. The self.updateMore is a method that will switch the current text displayed in the QLabel-widget with the information of the name I just clicked. But I cannot pass explicit variables to that method, because you're passing a method and not a method-call (so there are no brackets nor arguments). How do I tell self.updateMore which values need to be changed? Because the method itself refers to some variables which would normally be available as actual parameters, which it "cannot" have in the way I have it.

Thank you in advance!

Foi útil?

Solução

The QTableWidget.itemClicked signal sends the QTableWidgetItem that was clicked. So you just need to ensure that the handler you connect to the signal has an argument available to receive the sent item:

    self.scroll.itemClicked.connect(self.updateMore)
    ...

    def updateMore(self, item):
        self.label.setText(item.text())

Note that I have used the new-style signal and slot syntax to connect the signal, as it is much less error-prone, and more pythonic.

UPDATE:

To send another value with the signal, you could use a lambda:

    self.scroll.itemClicked.connect(
        lambda item: self.updateMore(item, info))
    ...

    def updateMore(self, item, info):
        text = item.text()
        ...

Outras dicas

In your updateMore function, you will need to look up/run your method to gather information to get the currently selected item.

def updateMore(self):
    select = self.scroll.currentItem().text()
    # Do something with this value; ie. Use it to run the object method 
    # and then set your QLabel to the results of this call

select will contain the text value of the selected object.

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