467,906 Members | 1,597 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,906 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. from PyQt4 import QtCore, QtGui
  3.  
  4. class Ui_Dialog(object):
  5.     def setupUi(self, Dialog):
  6.         Dialog.setObjectName("Dialog")
  7.         Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Dialog.minimumSizeHint()))
  8.  
  9.         self.buttonBox = QtGui.QDialogButtonBox(Dialog)
  10.         self.buttonBox.setGeometry(QtCore.QRect(30,240,341,32))
  11.         self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
  12.         self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.NoButton|QtGui.QDialogButtonBox.Ok)
  13.         self.buttonBox.setObjectName("buttonBox")
  14.  
  15.         self.okButton = QtGui.QPushButton(Dialog)
  16.         self.okButton.setGeometry(QtCore.QRect(70,70,80,27))
  17.         self.okButton.setObjectName("okButton")
  18.  
  19.         self.retranslateUi(Dialog)
  20.         QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("accepted()"),Dialog.accept)
  21.         QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("rejected()"),Dialog.reject)
  22.         #QtCore.QObject.connect(self.okButton,QtCore.SIGNAL("clicked()"),self.bok0)
  23.         QtCore.QMetaObject.connectSlotsByName(Dialog)
  24.  
  25.     #def bok0(self):
  26.         #print "OK !!!!!!!!"
  27.  
  28.     def retranslateUi(self, Dialog):
  29.         Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
  30.         self.okButton.setText(QtGui.QApplication.translate("Dialog", "New OK", None, QtGui.QApplication.UnicodeUTF8))
  31.  
Now the main python code, which evokes the ui and tries to connect the OK button:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from PyQt4 import QtCore, QtGui
  5. from untitled import Ui_Dialog
  6.  
  7. class TestDialog(QtGui.QDialog, Ui_Dialog):
  8.     def __init__(self):
  9.         QtGui.QDialog.__init__(self)
  10.  
  11.         # Set up the user interface from Designer.
  12.         self.setupUi(self)
  13.  
  14.         # Connect up the buttons.
  15.         self.connect(self.okButton, QtCore.SIGNAL("clicked"), self.bok)
  16.  
  17.     @QtCore.pyqtSignature("")
  18.     def bok(self):
  19.         print "OK 2 !!!!!!!!"
  20.  
  21. if __name__ == '__main__':
  22.     app = QtGui.QApplication(sys.argv)
  23.     window = QtGui.QDialog()
  24.     ui = TestDialog()
  25.     ui.setupUi(window)
  26.  
  27.     window.show()
  28.     sys.exit(app.exec_())
  29.  
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 -
Feb 23 '07 #1
  • viewed: 6146
Share:
2 Replies
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:

Expand|Select|Wrap|Line Numbers
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'untitled.ui'
  4. #
  5. # Created: Thu Feb 22 15:51:35 2007
  6. #      by: PyQt4 UI code generator 4.1.1
  7. #
  8. # WARNING! All changes made in this file will be lost!
  9.  
  10. import sys
  11. from PyQt4 import QtCore, QtGui
  12.  
  13. class Ui_MainWindow(object):
  14.     def setupUi(self, Dialog):
  15.         Dialog.setObjectName("MainWindow")
  16.         Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Dialog.minimumSizeHint()))
  17.  
  18.         self.buttonBox = QtGui.QDialogButtonBox(Dialog)
  19.         self.okButton = QtGui.QPushButton(Dialog)
  20.         self.okButton.setGeometry(QtCore.QRect(70,70,80,27))
  21.         self.okButton.setObjectName("okButton")
  22.  
  23.         self.retranslateUi(Dialog)
  24.         QtCore.QMetaObject.connectSlotsByName(Dialog)
  25.  
  26.     def retranslateUi(self, Dialog):
  27.         Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
  28.         self.okButton.setText(QtGui.QApplication.translate("Dialog", "New OK", None, QtGui.QApplication.UnicodeUTF8))
  29.  
And:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from PyQt4 import QtCore, QtGui
  5. from untitled import Ui_MainWindow
  6.  
  7. class TestDialog(QtGui.QDialog):
  8.     def __init__(self):
  9.         QtGui.QDialog.__init__(self)
  10.  
  11.         # Set up the user interface from Designer.
  12.         self.ui = Ui_MainWindow()
  13.         self.ui.setupUi(self)
  14.  
  15.         # Connect up the buttons.
  16.         self.connect(self.ui.okButton, QtCore.SIGNAL("clicked()"), self.bok)
  17.  
  18.     def bok(self):
  19.         print "OK 2 !!!!!!!!"
  20.  
  21. if __name__ == '__main__':
  22.     a = QtGui.QApplication(sys.argv)
  23.     w = TestDialog()    
  24.     w.show()
  25.     sys.exit(a.exec_())
  26.  
Sorry for bothering. Guess I should read more carefully websites ;-)

Cheers,

Michelle
- a.k.a. Angel of Darkness -
Feb 23 '07 #2
bartonc
Expert 4TB
Thanks for keeping us up-to-date. The input is really appreciated.
Feb 24 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Skink | last post: by
3 posts views Thread by Tina I | last post: by
25 posts views Thread by Daniel Jonsson | last post: by
5 posts views Thread by Mel | last post: by
2 posts views Thread by Pradnyesh Sawant | last post: by
2 posts views Thread by Glen | last post: by
2 posts views Thread by Pradnyesh Sawant | last post: by
reply views Thread by Chris Bergstresser | last post: by
reply views Thread by kunalgalav | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.