[PyQt] QComboBox and QVariant in pyqt4 ?
Ingmar Steen
iksteen at gmail.com
Wed Nov 7 18:41:40 GMT 2007
On Nov 7, 2007 5:07 PM, alteo_gange <romanocaldoni at free.fr> wrote:
> Hi,
>
> I try to include an personal identify (or key) in ComboBox items. I've thought
> to use QVariant:
>
> ------------------------------------
>
> #!/usr/bin/python
> # -*- coding: Utf-8 -*-
>
> import sys
> from PyQt4.QtGui import *
> from PyQt4.QtCore import *
>
> class Widget(QWidget):
> def __init__(self):
> QWidget.__init__(self)
>
> hbox=QHBoxLayout()
> combo = QComboBox()
> lst=[("a",0),("b",1),("c",2),("d",3),("e",4)]
> for i in lst:
> combo.addItem(i[0],QVariant(i[1]))
> hbox.addWidget(combo)
> self.connect(combo,SIGNAL("activated(QString, QVariant"),self.changeItem)
> self.setLayout(hbox)
>
> def changeItem(self,i,j):
> print i,j
>
> app = QApplication(sys.argv)
> main = Widget()
> main.show()
> sys.exit(app.exec_())
>
> -------------------------------------
>
> But it doesn't work. changeItem() is never called because of the
> signal "activated(QString, QVariant)".
>
> QComboBox and QVariant in pyqt4: Is this possible?
>
> --
> alteo_gange
Hi
You can use the activated(int) signal instead and then look up the
userdata with QComboBox.itemData(int).
#!/usr/bin/python
# -*- coding: Utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Widget(QWidget):
def __init__(self):
QWidget.__init__(self)
hbox=QHBoxLayout()
self.combo = combo = QComboBox()
lst=[("a",0),("b",1),("c",2),("d",3),("e",4)]
for i in lst:
combo.addItem(i[0],QVariant(i[1]))
hbox.addWidget(combo)
self.connect(combo,SIGNAL("activated(int)"),self.changeItem)
self.setLayout(hbox)
def changeItem(self,i):
print self.combo.itemData(i).toInt()[0]
app = QApplication(sys.argv)
main = Widget()
main.show()
sys.exit(app.exec_())
Ingmar
More information about the PyQt
mailing list