python qwidget closeEvent doesn't call - python

I created form using designer.
So, I have 3 files:
1. Form.py: - autocreated from designer
..... bla-bla-bla ......
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(1920, 1000)
Form.setMaximumSize(QtCore.QSize(1920, 1000))
..... bla-bla-bla ......
2. FormProc.py: - manual created for processing
from Form import *
class FormProc(Ui_Form):
def setupUi(self, Form):
Ui_Form.setupUi(self, Form)
..... bla-bla-bla ......
3. Main.py: - main file
import sys
from FormProc import *
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QWidget()
mw=FormProc()
mw.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I need to catch closeEvent, but I can't. Defining of closeEvent function in FormProc.py does not work.

If you look at the class definition in Form.py, you'll see that it's just a simple namespace that inherits from object. The setupUi method creates all the widgets specified in Qt Deisgner and makes them attributes of this namespace. The only excetion to this is the top-level widget, which must be passed in to setupUi as the Form argument.
So the FormProc class you created in FormProc.py is currently completely redundant. To make it useful, you need to make it a subclass of the top-level widget, and then add the ui to that. There are several ways to do this. Here's one way:
FormProc.py:
from PyQt4 import QtGui
from Form import Ui_Form
class FormProc(QtGui.QWidget):
def __init__(self, parent=None):
super(FormProc, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
def closeEvent(self, event):
print('close event')
Main.py:
import sys
from PyQt4 import QtGui
from FormProc import FormProc
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainWindow = FormProc()
MainWindow.show()
sys.exit(app.exec_())
For some other examples, see Using Qt Designer in the PyQt4 docs.

Related

Accessing object attributes in pyqt window in separate module

I have a main window created in one python module. This module also contains a class definition used to create an object called my_settings.
I also have a separate module which creates a another window (which should be in a different module for various reasons). This window allows various inputs such as opening a file and storing some settings once buttons are clicked blaa blaa.
What I want to do is then amend the attributes of my_setttings. A simplified version of the code is:
MAIN MODULE
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
uic.loadUi(self.some_directory + "arc_custom_main.ui", self)
self.some_button.clicked.connect(self.open_some_widget)
def open_some_widget(self):
widget = widget_in_other_module(self)
widget_in_other_module.exec_()
app = QApplication(sys.argv)
my_settings=settings()
_mainWindow = MainWindow()
_mainWindow.show()
SEPERATE MODULE
class widget_in_other_module(QDialog):
def __init__(self, parent):
my_settings.temppath = my_settings.OutputDir
QDialog.__init__(self)
self.parent = parent
my_settings.some_attribute= foo
uic.loadUi("some.ui", self)
self.pushButtonOpenMain.clicked.connect(self.openMain)
def openMain(self):
my_settings.some_other_attibute=bar
The problem I have is that I can't find a way to be able to access my_settings in the openMain method of widget_in_other_module. I'm a bit of a newbe to qt and can't for the life of me work out where I should pass my_settings. For various reasons I need all the functionality of widget_in_other_module to be outside of the main module (mostly to do with readability and future planed changes to the main module). I've tried as much as I can think of e.g. including it as a parameter in the line self.pushButtonOpenMain.clicked.connect(self.openMain,my_settings) but this doesn't seem to be allowed. Am I missing something basic here?
Instantiate settings variable in separate module and import this varable where you need it. It could be object of a class or just dictionary.
storage.py
class Settings:
def __init__(self):
print("Settings")
my_settings = Settings()
MainWindow.py
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
from Widget import Widget
from storage import my_settings
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
self.open_some_widget()
def open_some_widget(self):
widget = Widget()
widget.show()
widget.test()
self._widget = widget
my_settings.foo = 10
app = QApplication(sys.argv)
_mainWindow = MainWindow()
_mainWindow.show()
app.exec_()
Widget.py
from PyQt5.QtWidgets import QWidget
from storage import my_settings
class Widget(QWidget):
def test(self):
print(my_settings.foo)

Class for Each QStackedWidget Page

I am using PyQt5 and Qt-Designer to design an application.
How do I instantiate a class for each page on QstackedWidget. I can do it in a single class, all widgets belong to the same QMainWindow. But, the issue is that the file will get too long and impracticale. How do I assign a class for each page. For example, class I handles all the widgets on Page I and class II handles all the widgets on Page II; in the QMainWindow file I can just assign an Object that represents each page.
How can I do it?
Just create multiple modules:
widget1.py
from PyQt5.QtWidgets import QWidget
class Widget1(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
widget2.py
from PyQt5.QtWidgets import QWidget
class Widget2(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
main.py
from widget1 import Widget1
from widget2 import Widget2
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setTitle("Stackked widget demo")
self.stacked = QStackedWidget(self)
self.setCentralWidget(self.stacked)
self.widget1 = Widget1()
self.stacked.addWidget(self.widget1)
self.widget2 = Widget2()
self.stacked.addWidget(self.widget2)
if __name__ == "__main__":
app = QApplication([])
mainwin = MainWindow()
mainwin.show()
app.exec_()

Updating child widget in Qt

I have a simple project with the following classes
class MainWindow(QMainWindow)
class Home(QWidget)
class Login(QWidget)
All I want is to be able to nest the QWidget classes (make them children of the QMainWindow) and display them INSIDE the MainWindow. I can't seam to figure out how to make the QWidgets "appear" after I've called them in the MainWindow.
Code is bellow:
import sys
from gui.MainWindow import Ui_MainWindow
from gui.home import Ui_Home
from gui.login import Ui_Login
from PyQt4.QtGui import QMainWindow, QApplication, QWidget
class Home(QWidget, Ui_Home):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
class Login(QWidget, Ui_Login):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
#INSERT pushButton.click to go to HOME here
#INSERT pushButton.click to go to LOGIN here
def setHome(self):
self.label_Screen.setText("HOME")
self.mainwidget = Home()
#NEEDS SOMETHING HERE
def setLogin(self):
self.label_Screen.setText("LOGIN")
self.mainwidget = Login()
#NEEDS SOMETHING HERE
if __name__ == '__main__':
app = QApplication(sys.argv)
Main = MainWindow()
Main.show()
sys.exit(app.exec_())
I think I just need something where I've tagged "#NEEDS SOMETHING HERE", but I'm not sure what!
Cheers!
RESOLVED: thanks to kh25
Just had to add a layout to the QMainWindow and change the setHome to this:
def setHome(self):
self.label_Screen.setText("HOME")
self.currentScreen = Home()
self.layout.addWidget(self.currentScreen)
self.setLayout(self.layout)
The equivalent should be done for the setLogin method also.
You need to create a layout and add the widgets to this layout first. There are various types of layout. Read here:
http://doc.qt.io/qt-4.8/layout.html
For a simple case like yours I'd suggest either using a QHBoxLayout or QVBoxLayout.
Declare this layout. Call addWidget() on the layout for each of the Login and Home widgets and then call setLayout() on the QMainWindow.

How to re-implement Ui_MainWindow generated by Qt

I've created a interface in Qt as .ui file and then converted it to a python file. Then, I wanted to add some functionality to the components such as radio button, etc. For doing so, I tried to re-implement the class from Qt and add my events. But it gives the following error:
self.radioButton_2.toggled.connect(self.radioButton2Clicked)
NameError: name 'self' is not defined
My first question is whether this is the correct/proper way to deal with classes generated by Qt? And second, why do I get the error?
My code is here:
import sys
from PySide import QtCore, QtGui
from InterfaceClass_Test01 import Ui_MainWindow
class MainInterface(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainInterface, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def setupUi(self, MainWindow):
super(MainInterface, self).setupUi(parent, MainWindow)
self.radioButton.toggled.connect(self.radioButtonClicked)
self.radioButton_2.toggled.connect(self.radioButton2Clicked)
self.radioButton_3.toggled.connect(self.radioButton3Clicked)
def radioButton3Clicked(self, enabled):
pass
def radioButton2Clicked(self, enabled):
pass
def radioButtonClicked(self, enabled):
pass
The generated files are a little unintuitive. The UI class is just a simple wrapper, and is not a sub-class of your top-level widget from Qt Designer (as you might expect).
Instead, the UI class has a setupUi method that takes an instance of your top-level class. This method will add all the widgets from Qt Designer and make them attributes of the passed in instance (which would normally be self). The attribute names are taken from the objectName property in Qt Designer. It is a good idea to reset the default names given by Qt to more readable ones so that they are easy to refer to later. (And don't forget to re-generate the UI module after you've made your changes!)
The module that imports the UI should end up looking like this:
import sys
from PySide import QtCore, QtGui
from InterfaceClass_Test01 import Ui_MainWindow
class MainInterface(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainInterface, self).__init__(parent)
# inherited from Ui_MainWindow
self.setupUi(self)
self.radioButton.toggled.connect(self.radioButtonClicked)
self.radioButton_2.toggled.connect(self.radioButton2Clicked)
self.radioButton_3.toggled.connect(self.radioButton3Clicked)
def radioButton3Clicked(self, enabled):
pass
def radioButton2Clicked(self, enabled):
pass
def radioButtonClicked(self, enabled):
pass

PyQT4 - communication between 2 windows

I've got problem with communication between 2 windows in PyQt.
main window = UI_Form (class MyForm)
additional window = UI_Employee (class Employee)
I'd like, when i clicked AddTextButton (Ui_Employee), to set text in LineTextEdit (UI_Form)
This is my code.
import sys
from PyQt4 import QtCore, QtGui
from Form import Ui_Form
from Window import Ui_Employee
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.AddButton,QtCore.SIGNAL("clicked()"), self.add)
def add(self):
self.Employee = Employee(self)
self.Employee.show()
class Employee(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_Employee()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.AddRowButton,QtCore.SIGNAL('clicked()'), self.addText)
def addText(self):
self.Form = MyForm()
self.Form.ui.textEdit.setText('someText')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
I've got problem with line in method addText. First and second lines are ignored. I dont know why.
In your method Employee.addText you create a new MyForm. This is probably not what you want. You can access your original myapp from inside Employee via self.parentWidget().
class Employee(QtGui.QMainWindow):
def addText(self):
self.parentWidget().ui.textEdit.setText('someText')

Categories

Resources