[PyKDE] weird connectSlotsByName behavour with listWidget
Martin Kiebacher
makieb at googlemail.com
Wed Nov 22 13:40:20 GMT 2006
Hello list,
i have done a small sample form with a lineEdit, listWidget and some
pushButtons. One of the pushButtons is used to delete the current
selected listWidget-row. In the version which uses connectSlotsByName
2 rows of the listWidget are deleted, behaves like the clicked-signal
is emittet twice. In the version where i manually assign the signals
to my python-slot everything works fine.
Hope someone can give some hints, what i've done wrong, best regards, martin.
Here is the sample-code with connectSlotsByName, look at
on_pushButtonDelete_clicked:
#
# Uses connectSlotsByName feature
#
import sys
from PyQt4 import QtCore, QtGui
from ui_testform import Ui_TestForm
class AddDialog (QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_TestForm()
self.ui.setupUi(self)
def on_pushButtonAdd_clicked(self):
text = self.ui.lineEdit.text()
if text.length() > 0:
self.ui.listWidget.addItem(text)
self.ui.lineEdit.clear()
def on_pushButtonDelete_clicked(self):
self.ui.listWidget.takeItem(self.ui.listWidget.currentRow())
def on_pushButtonCancel_clicked(self):
self.close()
app = QtGui.QApplication(sys.argv)
ui = AddDialog()
ui.show()
sys.exit(app.exec_())
Here is the code where i manually assign signal and slots:
#
# manually assign signals to slots
#
import sys
from PyQt4 import QtCore, QtGui
from ui_testform import Ui_TestForm
class AddDialog (QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_TestForm()
self.ui.setupUi(self)
# Connect up the buttons.
self.connect(self.ui.pushButtonCancel,QtCore.SIGNAL("clicked()"),
self, QtCore.SLOT("close()"))
self.connect(self.ui.pushButtonAdd,
QtCore.SIGNAL("clicked()"),self.pushButtonAddSlot)
self.connect(self.ui.pushButtonDelete,
QtCore.SIGNAL("clicked()"), self.pushButtonDeleteSlot)
def pushButtonAddSlot(self):
text = self.ui.lineEdit.text()
if text.length() > 0:
self.ui.listWidget.addItem(text)
self.ui.lineEdit.clear()
def pushButtonDeleteSlot(self):
self.ui.listWidget.takeItem(self.ui.listWidget.currentRow())
app = QtGui.QApplication(sys.argv)
ui = AddDialog()
ui.show()
sys.exit(app.exec_())
To comlete the code, here is the generated ui:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'TestForm.ui'
#
# Created: Wed Nov 22 10:21:56 2006
# by: PyQt4 UI code generator 4.1
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt4 import QtCore, QtGui
class Ui_TestForm(object):
def setupUi(self, TestForm):
TestForm.setObjectName("TestForm")
TestForm.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(TestForm.minimumSizeHint()))
self.vboxlayout = QtGui.QVBoxLayout(TestForm)
self.vboxlayout.setMargin(9)
self.vboxlayout.setSpacing(6)
self.vboxlayout.setObjectName("vboxlayout")
self.gridlayout = QtGui.QGridLayout()
self.gridlayout.setMargin(0)
self.gridlayout.setSpacing(6)
self.gridlayout.setObjectName("gridlayout")
self.pushButtonAdd = QtGui.QPushButton(TestForm)
self.pushButtonAdd.setObjectName("pushButtonAdd")
self.gridlayout.addWidget(self.pushButtonAdd,0,1,1,1)
self.listWidget = QtGui.QListWidget(TestForm)
self.listWidget.setObjectName("listWidget")
self.gridlayout.addWidget(self.listWidget,1,0,1,1)
self.lineEdit = QtGui.QLineEdit(TestForm)
self.lineEdit.setObjectName("lineEdit")
self.gridlayout.addWidget(self.lineEdit,0,0,1,1)
self.vboxlayout.addLayout(self.gridlayout)
spacerItem =
QtGui.QSpacerItem(20,40,QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
self.vboxlayout.addItem(spacerItem)
self.hboxlayout = QtGui.QHBoxLayout()
self.hboxlayout.setMargin(0)
self.hboxlayout.setSpacing(6)
self.hboxlayout.setObjectName("hboxlayout")
spacerItem1 =
QtGui.QSpacerItem(40,20,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Minimum)
self.hboxlayout.addItem(spacerItem1)
self.pushButtonDelete = QtGui.QPushButton(TestForm)
self.pushButtonDelete.setFocusPolicy(QtCore.Qt.StrongFocus)
self.pushButtonDelete.setObjectName("pushButtonDelete")
self.hboxlayout.addWidget(self.pushButtonDelete)
self.pushButtonCancel = QtGui.QPushButton(TestForm)
self.pushButtonCancel.setObjectName("pushButtonCancel")
self.hboxlayout.addWidget(self.pushButtonCancel)
self.vboxlayout.addLayout(self.hboxlayout)
self.retranslateUi(TestForm)
QtCore.QMetaObject.connectSlotsByName(TestForm)
TestForm.setTabOrder(self.pushButtonAdd,self.lineEdit)
TestForm.setTabOrder(self.lineEdit,self.listWidget)
TestForm.setTabOrder(self.listWidget,self.pushButtonDelete)
TestForm.setTabOrder(self.pushButtonDelete,self.pushButtonCancel)
def retranslateUi(self, TestForm):
self.pushButtonAdd.setText(QtGui.QApplication.translate("TestForm",
"Add", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonDelete.setText(QtGui.QApplication.translate("TestForm",
"Delete", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonCancel.setText(QtGui.QApplication.translate("TestForm",
"Cancel", None, QtGui.QApplication.UnicodeUTF8))
More information about the PyQt
mailing list