Reputation: 11
First of all let me tell you that I am new to Qt and also to Python.
I am using Qt(Taurusdesigner) to create my GUIs. After starting Qt(Taurusdesigner), I generate my python code for that particular GUI using:
taurusuic4 -x -o file.py file.ui
or
pyuic4 -x -o file.py file.ui
After executing this command on command line I am able to generate python file, but the auto generated classes looks like:
class MainWindow(object):
def setupUi(self, MainWindow):
Where as when I am searching for any help on Google I find class written like:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
What would I do to generate 2nd type of class file using Qt(Taurusdesigner)??
Why there is a syntax difference in my class and class which are written for help on internet. Please Help regarding this. Thanks in advance.
Upvotes: 1
Views: 362
Reputation: 120578
The ui module generated by taurusuic4/pyuic4
should be imported into your main application. You do not need to use the -x
option, and obviously you should choose a better module name than "file":
taurusuic4 -o mainwindow.py file.ui
Your main application module should look something like this:
from PyQt4.QtGui import QMainWindow
from mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.handleButton)
def handleButton(self):
print('Hello World!')
This approach means that all the widgets from Qt(Taurus) Designer end up as attributes of the MainWindow
class. Another approach is to have the ui elements within a separate namespace:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.handleButton)
Upvotes: 1
Reputation: 6065
setupUI
and __init__
are two methods of the class MainWindow
. For one class, there can be any number of method and you can put them in whatever order you like.
There is always an __init__
method, it's called a constructor. This method is called when you create an object (for example when you do myWindow=MainWindow()
). It's usually put at the beginning because it'll be call first. Specifically for QT, you have to call the parent's constructor with super
.
setupUI
is the method created by your designer, to take care of layout and such. It should be called in the constructor.
Your code should look like:
class MainWindow(object):
def setupUi(self, MainWindow):
#code made by the designer
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#some code
def another_method(self):
#some more code
Upvotes: 0