problems with PyQt4 and all that

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AngelOfDarkness
    New Member
    • Feb 2007
    • 2

    #1

    problems with PyQt4 and all that

    Hi guys,

    unfortunately I am pretty new to Python and Qt and all that. Well, I managed to successfully program a little stuff for my work (computational neuroscientist. .. no comment please), using a simple graphical interface and binding it to my long existing C library. There was no problem, everything was running fine...

    ... until I decided to upgrade everything to kubuntu's Edgy and PyQt4. Well, I understood that I need to port my original PyQt3 stuff, as there are many principle changes in PyQt4. Anyway, I created my interfaces with Qt4 builder from Trolltech, and connected the buttons in my main Python program (that's the normal way to do, I guess?) However, this most basic "connect" does not work anymore, and I am to new to all that to understand why. Here is an example code which explains the problem:

    First, the python code for the ui, generated with pyuic4:

    Code:
    import sys
    from PyQt4 import QtCore, QtGui
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Dialog.minimumSizeHint()))
    
            self.buttonBox = QtGui.QDialogButtonBox(Dialog)
            self.buttonBox.setGeometry(QtCore.QRect(30,240,341,32))
            self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
            self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.NoButton|QtGui.QDialogButtonBox.Ok)
            self.buttonBox.setObjectName("buttonBox")
    
            self.okButton = QtGui.QPushButton(Dialog)
            self.okButton.setGeometry(QtCore.QRect(70,70,80,27))
            self.okButton.setObjectName("okButton")
                    
            self.retranslateUi(Dialog)
            QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("accepted()"),Dialog.accept)
            QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("rejected()"),Dialog.reject)
            #QtCore.QObject.connect(self.okButton,QtCore.SIGNAL("clicked()"),self.bok0)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
            
        #def bok0(self):
            #print "OK !!!!!!!!"
    
        def retranslateUi(self, Dialog):
            Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
            self.okButton.setText(QtGui.QApplication.translate("Dialog", "New OK", None, QtGui.QApplication.UnicodeUTF8))
    Now the main python code, which evokes the ui and tries to connect the OK button:

    Code:
    #!/usr/bin/env python
    
    import sys
    from PyQt4 import QtCore, QtGui
    from untitled import Ui_Dialog
    
    class TestDialog(QtGui.QDialog, Ui_Dialog):
        def __init__(self):
            QtGui.QDialog.__init__(self)
    
            # Set up the user interface from Designer.
            self.setupUi(self)
            
            # Connect up the buttons.
            self.connect(self.okButton, QtCore.SIGNAL("clicked"), self.bok)
    
        @QtCore.pyqtSignature("")
        def bok(self):
            print "OK 2 !!!!!!!!"
                         
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = QtGui.QDialog()
        ui = TestDialog()
        ui.setupUi(window)
        
        window.show()
        sys.exit(app.exec_())
    As I said, I must make a principal mistake here, as there is no output "OK 2!!!!" on my shell when I press the button. There is also no error message or crash or whatever nasty thing, it just doesn't work. However, if I uncomment the same type of code in the ui (bok0 function), then it works. But that's not what I want.

    I would very much appreciate if there is somebody who could help me in this matter. Please feel free to tell me that I am stupid as I made a stupid mistake... no hard feelings ;-)

    Cheers,

    Michelle
    - a.k.a. Angel of Darkness -
  • AngelOfDarkness
    New Member
    • Feb 2007
    • 2

    #2
    Hi again,

    OK, found an example which worked. Hmmm... still don't really know what was the point... maybe the "clicked" -> "clicked()" thing... anyway, here is a slightly stripped code which works:

    Code:
    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'untitled.ui'
    #
    # Created: Thu Feb 22 15:51:35 2007
    #      by: PyQt4 UI code generator 4.1.1
    #
    # WARNING! All changes made in this file will be lost!
    
    import sys
    from PyQt4 import QtCore, QtGui
    
    class Ui_MainWindow(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("MainWindow")
            Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Dialog.minimumSizeHint()))
    
            self.buttonBox = QtGui.QDialogButtonBox(Dialog)
            self.okButton = QtGui.QPushButton(Dialog)
            self.okButton.setGeometry(QtCore.QRect(70,70,80,27))
            self.okButton.setObjectName("okButton")
                    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
            
        def retranslateUi(self, Dialog):
            Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
            self.okButton.setText(QtGui.QApplication.translate("Dialog", "New OK", None, QtGui.QApplication.UnicodeUTF8))
    And:

    Code:
    #!/usr/bin/env python
    
    import sys
    from PyQt4 import QtCore, QtGui
    from untitled import Ui_MainWindow
    
    class TestDialog(QtGui.QDialog):
        def __init__(self):
            QtGui.QDialog.__init__(self)
    
            # Set up the user interface from Designer.
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            
            # Connect up the buttons.
            self.connect(self.ui.okButton, QtCore.SIGNAL("clicked()"), self.bok)
    
        def bok(self):
            print "OK 2 !!!!!!!!"
                         
    if __name__ == '__main__':
        a = QtGui.QApplication(sys.argv)
        w = TestDialog()    
        w.show()
        sys.exit(a.exec_())
    Sorry for bothering. Guess I should read more carefully websites ;-)

    Cheers,

    Michelle
    - a.k.a. Angel of Darkness -

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Thanks for keeping us up-to-date. The input is really appreciated.

      Comment

      Working...