Setting up and getting "data" from PyQt widget elements?

This is not so much a question as a request for explanation. I follow Mark Summerfield's “Quick GUI Programming with Python and Qt,” and I must have missed something because I can't figure out the following mechanism to link the real “instance-element” that I use, and is full of different types data and the "widget_item" that represents it in the QTreeWidget model for convenience.

Installation:

widget_item.setData(0, Qt.UserRole, QVariant(long(id(instance_item))))

how

widget_item.data(0, Qt.UserRole).toLongLong()[0]

Stuff like toLongLong()doesn't seem to be “Pythonic” at all, and why are we calling Qt.UserRole and QVariant? are part of the setData function and the "data" part of the Qt framework or is it a more general Python command?

+5
source share
2 answers

2 . :

1)

widget_item.setData(0, Qt.UserRole, QVariant(instance_item))
widget_item.data(0, Qt.UserRole).toPyObject()

2) API PyQt4, QVariant , QVariant . , PyQt4:

import sip
sip.setapi('QVariant', 2)

:

widget_item.setData(0, Qt.UserRole, instance_item)
widget_item.data(0, Qt.UserRole)  # original python object

, sip.setapi('QString', 2), QString , unicode .

+8

- setData(), data(), toLongLong() Qt ++, . , , , - , , :

## The setter:
widget_item.instance_item = instance_item

## The getter:
instance_item = widget_item.instance_item

Qt , , ++ . , , , QVariant (, QtSQL), .

+3

All Articles