1

I have a main window designed in QT Designer. Among other things it has a scroll area with a label used to display image. I want to replace default mouse wheel handler which scrolls image with my own that zooms image instead. If I add wheelEvent event to my class that inherits the main window class from compiled qt designer file (Ui_MainWindow) it zooms image no matter where the mouse pointer is and which widget is active. Even more the scroll area keeps handling wheel event before processing my own handler that does zooming. I'm not quite sure how do I replace standard wheelevent event handler so that zooming works only when scrollarea is active? I know how to do that If I was coding GUI myself, but with QT Designer I'm not supposed to touch the compiled version of GUI. Below is the piece of code that handles zooming. It won't work without QT Designer compiled code though...

from MainWindow import Ui_MainWindow

if __name__ == "__main__":
    class CCDInspectorMainWindow(QMainWindow, Ui_MainWindow):
        ImageWidth=0
        ImageHeight=0            
        zoom=1.0

        def __init__(self, parent = None):
            super(CCDInspectorMainWindow, self).__init__(parent)
            self.setupUi(self)
            QObject.connect(self.actionOpen_File, SIGNAL("triggered()"), self.openDialog)
            self.imageMap=QImage(0,0,QImage.Format_RGB888)


        def openDialog(self):
            fileObj=QFileDialog.getOpenFileName(self,"Open image", filter="All Image Files (*.png *.xpm *.jpg *.tif *.fit *.fits *.cr*)")
            self.imageMap=QImage(fileObj)
            self.ImageWidth=self.imageMap.width()
            self.ImageHeight=self.imageMap.height()
            self.updateImages()

        def wheelEvent(self,event):
            self.zoom = self.zoom+event.delta() / 1200.0
            if self.zoom<0:
                self.zoom=0
            self.updateImages()

        def updateImages(self):
            self.setOriginalImage()
            self.setSplitImage()

        def setOriginalImage(self):
            self.OriginalImage.setPixmap(QPixmap.fromImage(self.imageMap.scaled(int(self.ImageWidth*self.zoom),int(self.ImageHeight*self.zoom),aspectRatioMode=Qt.IgnoreAspectRatio, transformMode=Qt.FastTransformation)))
Dennis Sakva
  • 1,447
  • 2
  • 13
  • 26
  • 1
    What exactly is the problem? Subclassing the compiled ui file and overriding the method is the correct way to do it. Does it not work or do you want to do it another way? – three_pineapples Jul 06 '14 at 00:07
  • This might be trivial, but how do I override method which is in another class inside parent class? The way I wrote it wheelEvent method overrides wheelEvent for the whole ui compiled file while I need to override it only for 1 scroll area. – Dennis Sakva Jul 06 '14 at 05:22
  • 1
    What you want is to promote a widget in Qt Designer to a widget class you have written yourself in Python. See this SO post for details: http://stackoverflow.com/questions/19622014/how-to-use-promote-to-in-qt-designer-in-pyqt4 – three_pineapples Jul 06 '14 at 05:34
  • Thanks @three_pineapples! You answer is very helpful. (How do I mark it as a solution BTW?) – Dennis Sakva Jul 06 '14 at 07:37
  • I didn't really feel like it warranted posting as an answer since I haven't provided any new information! – three_pineapples Jul 06 '14 at 11:53

0 Answers0