pyqt4 signal/slot using PyObject* and shortcut

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pradnyesh Sawant

    #1

    pyqt4 signal/slot using PyObject* and shortcut

    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(QtCor e.SIGNAL("showC ntr1(PyObject*) "), (cntr,
    "a")) # line 1
    self.emit(QtCor e.SIGNAL("showC ntr2"), (cntr, "a"))
    # line 2
    time.sleep(0.2)
    class Gui(QtGui.QDial og):
    def __init__(self, parent = None):
    QtGui.QDialog._ _init__(self, parent)
    frameStyle = QtGui.QFrame.Su nken | QtGui.QFrame.Pa nel

    self.lCntr = QtGui.QLabel()
    self.lCntr.setF rameStyle(frame Style)
    loGrd = QtGui.QGridLayo ut()
    loGrd.addWidget (self.lCntr, 0, 0)
    self.setLayout( loGrd)
    self.setWindowT itle(self.tr("C ounter"))
    def showCntr1(self, val):
    print val, str(val)
    self.lCntr.setT ext(str(val))
    def showCntr2(self, val):
    print val, str(val)
    self.lCntr.setT ext(str(val))
    if __name__ == "__main__":
    app = QtGui.QApplicat ion(sys.argv)
    dialog = Gui()
    cntr = Counter()
    cntr.start()
    QtCore.QObject. connect(cntr, QtCore.SIGNAL(" showCntr1(PyObj ect*)"),
    dialog.showCntr 1, QtCore.Qt.Queue dConnection)
    QtCore.QObject. connect(cntr, QtCore.SIGNAL(" showCntr2"),
    dialog.showCntr 1, QtCore.Qt.Queue dConnection)
    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
Working...