[PyQt] pyqt4 signal/slot using PyObject* and shortcut
Pradnyesh Sawant
spradml at gmail.com
Fri Apr 27 07:00:04 BST 2007
Hello, i have the following code:
#################################################################
import time
import sys
from PyQt4 import QtGui, QtCore
class Counter(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
cntr = 0
while cntr < 10:
cntr += 1
self.emit(QtCore.SIGNAL("showCntr1(PyObject*)"), (cntr,
"a")) # line 1
self.emit(QtCore.SIGNAL("showCntr2"), (cntr, "a"))
# line 2
time.sleep(0.2)
class Gui(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel
self.lCntr = QtGui.QLabel()
self.lCntr.setFrameStyle(frameStyle)
loGrd = QtGui.QGridLayout()
loGrd.addWidget(self.lCntr, 0, 0)
self.setLayout(loGrd)
self.setWindowTitle(self.tr("Counter"))
def showCntr1(self, val):
print val, str(val)
self.lCntr.setText(str(val))
def showCntr2(self, val):
print val, str(val)
self.lCntr.setText(str(val))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dialog = Gui()
cntr = Counter()
cntr.start()
QtCore.QObject.connect(cntr, QtCore.SIGNAL("showCntr1(PyObject*)"),
dialog.showCntr1, QtCore.Qt.QueuedConnection)
QtCore.QObject.connect(cntr, QtCore.SIGNAL("showCntr2"),
dialog.showCntr1, QtCore.Qt.QueuedConnection)
sys.exit(dialog.exec_())
#################################################################
If i comment out "line 1", then i get the following output:
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
0.2 0.2
Notice that 0.2 is the time value of the sleep instruction. Why is
this happening?
On the other hand, if i comment out "line 2", then i get the following output:
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
(<refcnt 0 at 0xb7dcd28c>, 'a') (<NULL>, <NULL>)
What i get from the above is that a reference to "cntr" is being
passed, but by the time the gui thread is actually run, both the
values (cntr and "a") have been destroyed, hence the NULL values.
***How do i circumvent this problem?***
Lastly, if i don't comment out any of line 1 or 2, then i get the foll output:
(<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui object at
0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui
object at 0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>,
(<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui object at
0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui
object at 0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>,
(<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui object at
0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui
object at 0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>,
(<__main__.Gui object at 0xb6a8f12c>, (<__main__.Gui object at
0xb6a8f12c>, (<__main__.Gui object at 0xb6a8f12c>, ..........
i don't know what this means??? Can anyone kindly explain what's happening...
I'm using:
python: 2.4.4~c1-0ubuntu1
qt4-dev-tools: not installed
python-qt4: 4.0.1-1ubuntu1
sip4: (4.4.5-2ubuntu1
os: ubuntu edgy
--
warm regards,
Pradnyesh Sawant
--
Be yourself, everyone else is taken. --Anon
More information about the PyQt
mailing list