0

I am using PySide in a not-so-MVC fashion, meaning, I try as much as possible not to edit the generated .ui to .py file, I put my application logic in packages (models) and I have one module (.pyw file) more like a controller for them all to initialize and perform management. Not too best of practice but I'm doing fine, all I want is I dont want to add code to the generated ui .py file (more like my view)

Now here is the problem

I Noticed that the generated PySide file doesn't inherit from the QDialog or QMainWindow as you have to create it when you are instantiating the class, as a result, events like closeEvent(self, event) doesn't work inside the class even when you put it there. I know hoe to write functions for QActions and widget connections, but I DONT KNOW HOW TO ADD A CLASS BASED FUNCTION TO A GENERATED PYSIDE CLASS OUTSIDE THE CLASS.

If I have to edit the generated view class, I can perfectly tweak it to what I want BUT i dont want to because I can make amendment in QtDesigner and compile at any time

This is my question, since I dont want to how do i attach say a closeEvent to the object created from the class in my controller class without touching the generated view class.

Thanks

2 Answers 2

3

There is never any need to edit the ui module generated by pyside-uic.

There are three main methods to add class-level functionality to widgets from Qt Designer. Firstly, for the top-level widget, you can simply create a subclass, like this:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow().setupUi(self)

    def closeEvent(self, event):
        print('Goodbye world!')
        QtGui.QMainWindow.closeEvent(self, event)

Secondly, for widgets that are not top-level, you can use an event-filter, like this:

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        ...
        self.lineEdit.installEventFilter(self)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.MouseMove and
            source is self.lineEdit):
            print('mouse-move:', event.globalPos())
        return QtGui.QMainWindow.eventFilter(self, source, event)

So instead of reimplementing mouseMoveEvent in a QLineEdit subclass, you can listen for the same events via the event-filter. All protected functions have a corresponding event type that can be accessed in this way.

The final method is widget promotion, which is more complicated to set up, but perhaps gives the most flexibility. This allows you to completely replace the widgets from Qt Designer with your own custom subclasses.

To do this, in Qt Designer you would right-click the widget you want to replace and select "Promote to...". In the dialog, set "Promoted class name" to your custom subclass (e.g. "MyLineEdit"), and set "Header file" to the python import path for the module containing the subclass (e.g. "myapp", or "myapp.gui"). Next you would click "Add", and then "Promote", and you will see the class change from "QLineEdit" to "MyLineEdit" in the Object Inspector pane.

With that in place, all you need to do is ensure that the myapp module contains a MyLineEdit class that can be imported by the .ui module generated by pyside-uic.

Sign up to request clarification or add additional context in comments.

Comments

0

Monkey Patching did the job, I dont know why I didn't taught of that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.