[PyQt] how to subclass an instance from a generated ui-class?

Mads Kristiansen madsornomads at gmail.com
Sat Mar 14 13:57:34 GMT 2009


Hi all,

I have designed a simple gui (a MainWindow containing an
QGraphicsView) in qt-designer. How do I subclass QGraphicsView so the
generated 'Ui_MainWindow' use the subclass?

It seems to be a very basic thing, but I cannot get it right.

Code example below to illustrate.

Thanks in advance,
Mads


***** The generated class with comments added **********
*# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'imageviewer.ui'
#
# Created: Sat Mar 14 11:08:43 2009
#      by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
                MainWindow.setObjectName("MainWindow")

MainWindow.resize(QtCore.QSize(QtCore.QRect(0,0,710,419).size()).expandedTo(MainWindow.minimumSizeHint()))

                self.centralwidget = QtGui.QWidget(MainWindow)
                self.centralwidget.setObjectName("centralwidget")

                self.hboxlayout = QtGui.QHBoxLayout(self.centralwidget)
                self.hboxlayout.setObjectName("hboxlayout")

                self.graphicsView = QtGui.QGraphicsView(self.centralwidget)
                # This is what I really want...
                #self.graphicsView = ImageView(self.centralwidget)
                self.graphicsView.setObjectName("graphicsView")
                #  But how do I subclass self.graphicsViews so
                # 'self.hboxlayout.addWidget(self.graphicsView)' get
the correct reference?
                self.hboxlayout.addWidget(self.graphicsView)
                MainWindow.setCentralWidget(self.centralwidget)



                self.menubar = QtGui.QMenuBar(MainWindow)
                self.menubar.setGeometry(QtCore.QRect(0,0,710,30))
                self.menubar.setObjectName("menubar")
                MainWindow.setMenuBar(self.menubar)

                self.statusbar = QtGui.QStatusBar(MainWindow)
                self.statusbar.setObjectName("statusbar")
                MainWindow.setStatusBar(self.statusbar)

                self.retranslateUi(MainWindow)
                QtCore.QMetaObject.connectSlotsByName(MainWindow)

        def retranslateUi(self, MainWindow):

MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow",
"MainWindow", None, QtGui.QApplication.UnicodeUTF8))

***** The class contains my subclassed QGraphicsView and main  **********
#! /usr/bin/env python
from imageviewer import Ui_MainWindow
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ImageView(QGraphicsView):

        def __init__(self, parent=None):
                super(ImageView, self).__init__(parent)

                self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

                imgPath = '../Kings_Of_Leon_Wallpaper.jpg'
                self.origPixmap = QPixmap(imgPath)
                self.scene = QGraphicsScene()
                self.scene.addPixmap(self.origPixmap)
                self.setScene(self.scene)


        def resizeEvent(self, event):
                size = event.size()
                item =  self.items()[0]

                pixmap = self.origPixmap
                pixmap = pixmap.scaled(size, Qt.KeepAspectRatio,
Qt.SmoothTransformation)

                self.centerOn(1.0,1.0)

                item.setPixmap(pixmap)

                centerPos = (size.width()-pixmap.width())/2
                item.setPos(centerPos,0)


class StartGui(QMainWindow):
        def __init__(self, parent=None):
                QWidget.__init__(self, parent)
                self.ui = Ui_MainWindow()
                self.ui.setupUi(self)


if __name__ == "__main__":
        app = QApplication(sys.argv)
        gui = StartGui()
        gui.show()
        sys.exit(app.exec_())


Best regards,
Mads
-------------- next part --------------
A non-text attachment was scrubbed...
Name: imageviewer.py
Type: text/x-python
Size: 1670 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090314/fc3d9d8e/imageviewer.py
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.py
Type: text/x-python
Size: 1110 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090314/fc3d9d8e/main.py


More information about the PyQt mailing list