Simple text editor in PyQT4

We want to make a simple text editor (viewer at start). So in QTDesigner we create a GUI based on "Main Window". I used two PushButtons and one TextEdit:
The "Close" button I've connected with the window assigning "close()" slot which will close the GUI. For the "Open File" button I've changed it objectName to "button_open", and for the TextEdit window to "editor_window", and the window name from "MainWindow" to "notepad". You change them in the "Property Editor" when you select a widget.
I've saved the GUI and made a Python class out of it:

pyuic4 edytor.ui > edytor.py

And I got a file with a "Ui_notepad" class. To start an application using it we need a "special" code. Create a file start.py with:

Execute start.py to show the application. Click on "Close" to close it.
Now the main part - our own slots. In QT3 we could do that in QTDesigner, but here it looks different. We will edit start.py and add there our code not touching edytor.py code. Here is the modified start.py:

Where self.ui is our window object through which we can access the widgets, so self.ui.button_open is the "Open File" button. self.file_dialog is the method name which will be executed when the signal will be emitted. It is important to add our code after self.ui.setupUi(self). As for the custom slot:

deffile_dialog(self):self.ui.editor_window.setText('aaaaaaaaaa')

self.ui.editor_window this is our TextEdit, which has setText method, as described in the docs. That's how you add custom slots to a PyQT4 application :cool:
Now we have to use QFileDialog to select a file. Here is the code:

fd.getOpenFileName() will show a File Selection window, which will close after selecting a file (returns path to file), and allowing the rest of the code to execute. It works but not that if we cancel file selection fd.getOpenFileName() will not return a path to a existing file, we will get a bug like:

So we can view our files, but we can't save changes. I needed a "Save" button so I edited the ui file in QTDesigner and added a pushButton with "button_save" name. After editing the *ui file we have to recreate the gui class:

pyuic4 edytor.ui > edytor.py

And now our application looks like this:
We add a slot that saves the file and connect him with a "clicked()" signal of "Save" pushButton: