Is it possible to inherit signals from a base class and in the derived class connect methods to them? If yes, how?
Working testcase with composition
Instantiates a MyObject in a MyWidget, and in the widget reacts to a signal emitted by the object.
from PySide.QtGui import QApplication, QMainWindow
from PySide.QtCore import QObject, QTimer, Signal
from PySide.QtGui import QLabel
class MyObject(QObject):
sig = Signal()
def __init__(self, parent=None):
super().__init__(parent)
QTimer.singleShot(3000, self.alert)
QTimer.singleShot(5000, self.exit)
def alert(self):
self.sig.emit()
def exit(self):
print('All done')
exit(0)
class MyWidget(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.monitor = MyObject(self)
self.monitor.sig.connect(self.update)
def update(self):
print(2)
app = QApplication([])
w = MyWidget()
w.show()
app.exec_()
It is a small but working example that opens a minimal, empty window, the self.monitor object instantiated by the widget emits a timer signal after 3 and 5 seconds. The first prompts the widget to just print a number to the console, the second signal causes the application to quit.
Failing testcase with inheritance
For inheritance only the widget class is changed to:
class MyWidget(MyObject, QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.sig.connect(self.update)
def update(self):
print(2)
If this is run in the console nothing is printed but a Segmentation Fault happens.
Why? And can this be salvaged?
Salvaged by replacing super()
Interestingly, if both classes are changed to not use super(), the example works again:
class MyObject(QObject):
sig = Signal()
def __init__(self, parent=None):
QObject.__init__(self, parent)
QTimer.singleShot(3000, self.alert)
QTimer.singleShot(5000, self.exit)
def alert(self):
self.sig.emit()
def exit(self):
print('All done')
exit(0)
class MyWidget(MyObject, QLabel):
def __init__(self, parent=None):
MyObject.__init__(self, parent)
QLabel.__init__(self, parent)
self.sig.connect(self.update)
def update(self):
print(2)
Generally I prefer to use super(), but maybe I need to reconsider? I've purposefully linked to two controversial articles about the usage of super() in Python. And now would be interested in how to use super() properly with pyside, or in explanations why it doesn't work in pyside at all.
Minor update:
When using inheritance and super() but removing all signal related code the example works in that it does open up the window and does not segfault. So there seems to be some indication that the combination of super() initialization and signals causes a problem.
Minor update2:
..and when commenting out the self.sig.connect the window starts up and only segfaults when the 5-second signal fires to exit the application.
(This is Qt 4.8.4, Pyside 1.1.2 on an Ubuntu 13.04 system with a CPython 3.3.1 interpreter)
Qt does not support multiple inheritance from QObjects, and the same limitation applies to PySide and PyQt. Although there are sometimes ways to work around this limitation, it is generally a bad idea to attempt to create subclasses with two or more QObject base classes.
For the inheritance of signals, using a simple non-QObject mixin is probably the best way to go - although I think this solution will only work with PySide; for PyQt4, signals can only be defined on QObject subclasses.
UPDATE:
This latter restriction was removed in PyQt5: it is now possible to define properties, signals and slots in classes that do not inherit from QObject.
The only solution - or rather workaround - I could come up with so far that satisfies the criteria (a) inheritance and (b) use of super() is to prevent the diamond relationship as inspired by a comment by user tcaswell.
For my use case it is essential to retain the inheritance of any consumer classes (for custom widgets). On the other hand it is guaranteed - currently - that all consuming classes will be derived (indirectly) from QObject. Therefore there's no need to derive MyObject from QObject, though this in fact creates a true abstract MixIn class: It is not usable standalone and has interface requirements to consuming classes. I'm not sure if I like it.
Here's the working code:
from PySide.QtGui import QApplication, QMainWindow
from PySide.QtCore import QObject, QTimer, Signal
from PySide.QtGui import QLabel
class MyObject:
sig = Signal()
def __init__(self, parent=None):
super().__init__(parent)
QTimer.singleShot(3000, self.alert)
QTimer.singleShot(5000, self.exit)
def alert(self):
self.sig.emit()
def exit(self):
print('All done')
exit(0)
class MyWidget(MyObject, QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.sig.connect(self.update)
def update(self):
print(2)
app = QApplication([])
w = MyWidget()
w.show()
app.exec_()
Related
I have this app where I have several settings windows that open when buttons from the main window are clicked. The windows are application modal, so only one is open at a time. I have two ideas as to how to manage them, but I'm not sure which one would be the proper way to do it. I don't particularly care how the values are stored, as long as I can pass them to other windows in the app and do stuff with them.
MainWindow class Option 1:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
central = QWidget()
layout = QVBoxLayout()
button = QPushButton('Show window')
layout.addWidget(button)
window = OtherWindow()
button.clicked.connect(window.show)
# I can pull the settings and pass them on to other windows if needed.
self.setCentralWidget(central)
MainWindow class Option 2:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.other_settings = {}
button = QPushButton('Show window')
button.clicked.connect(self.show_other)
def show_other(self):
other_window = OtherWindow()
if other_window.exec():
self.other_settings.update(other_window.settings)
OtherWindow class:
class OtherWindow(QDialog):
def __init__(self):
super().__init__()
self.settings = {}
# widgets
box = QSpinBox(objectName='spinbox')
box.valueChanged.connect(self.save_settings)
# and so on ...
def save_settings(self):
sender = self.sender()
self.settings[sender.objectName()] = sender.value()
If i've understood your question correctly, look at this link which might be useful.
Small addition from me is, look here.
from PyQt5.QtWidgets import QDialog
from ui_imagedialog import Ui_ImageDialog
class ImageDialog(QDialog):
def __init__(self):
super(ImageDialog, self).__init__()
# Set up the user interface from Designer.
self.ui = Ui_ImageDialog()
self.ui.setupUi(self)
# Make some local modifications.
self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
# Connect up the buttons.
self.ui.okButton.clicked.connect(self.accept)
self.ui.cancelButton.clicked.connect(self.reject)
Once you've got your .ui files it is a good practice to use (second link, second example - mentoned above) pyside6-uic or pyuic5 - depending of your needs.
Then you're creating class which role is to setup desired QWidget ui converted python module.
So in QtDesigner you're creating a QWidget:
screen_qt_designer
then do all your stuff, that it would like to do - place over QLineEdit, QPushButtons, etc., then save that file in your project's folder. After that all you have to do is use pyuic or pyside6-uic with proper settings (look at --help to set the output filename).
Then all you have is a .ui file (which is used in case you would like to add some widget, or change the widget all all) and your generated python class that inherits QObject, eg:
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(800, 600)
self.verticalLayout = QVBoxLayout(Form)
self.verticalLayout.setObjectName(u"verticalLayout")
self.label = QLabel(Form)
self.label.setObjectName(u"label")
Then create a another Python file, that inherits this generated class, eg.:
from PySide6.QtWidgets import QWidget
from CreateNewDatabase_ui import Ui_Form
class NewDatabaseWidget(QWidget):
def __init__(self):
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
That's all. You have your prepared QWidget for adding it into a QStackedWidget. I use it in my own program and it fit me very well.
Edit:
I've forgot about 2 important notes:
Never ever edit your file generated by pyuic or pyside6-uic program. Whenever you wish to change the .ui file, all changes made to autogenerated code will be lost.
It's better to keep all signals/slots, logic mechanism in your QMainWindow class. It's better approach in case of code readability.
I'm trying to change QLabel text by clicking QPushbutton.
I installed PySide2==5.15.2.1
Python 3.7.6 32bit in Windows 10 environment.
First, I made UI class (well, I didn't used Qt designer so I don't have *.ui file)
import PySide2.QtWidgets as qtw
...
class UI_MainWindow(object):
def setupUI(self, MainWindow):
window = qtw.QWidget()
someButton = qtw.QPushButton("button")
someLabel = qtw.QLabel("---")
...
MainWindow.setCentralWidget(window)
And there is some other class who has functions and connects as below
import PySide2.QtWidgets as qtw
from uiSomething import UI_MainWindow #uiSomething is a name of the file of the code above, who has UI_MainWindow class.
...
class something(qtw.QMainWindow, UI_MainWindow):
def __init__(self):
super(something, self).__init__()
self.ui = UI_MainWindow()
self.ui.setupUI(self)
self.someButton.clicked.connect(self.function) # not working. ui.someButton... also not working.
#if I put this connect in UI_MainWindow, at least connect works.
def function(self):
self.ui.someLabel.setText("change text") # not working. ui.someLabel...also not working.
and the other file, I put main function
import Something
import PySide2.QtWidgets as qtw
...
if __name__ == "__main__":
app = qtw.QApplication()
MainWindow = Something.something()
MainWindow.show()
app.exec__()
However, it just give me an error message as below when I click the button.
AttributeEror: 'UI_MainWindow' object has no attribute 'someLabel'
I thought it's okay to define every widgets in setupUI function but probably not..?
Please let me know if there is any idea.
Thank you in advance!
Best wishes,
JESuh
Because someButton and someLabel are not declared as class attributes. These variables are only accessible within the function(setupUI). Objects created from UI_MainWidow cannot call or modify them. Not even if you create a subclass of UI_MainWindow.
Change
import PySide2.QtWidgets as qtw
...
class UI_MainWindow(object):
def setupUI(self, MainWindow):
window = qtw.QWidget()
someButton = qtw.QPushButton("button")
someLabel = qtw.QLabel("---")
...
MainWindow.setCentralWidget(window)
To
import PySide2.QtWidgets as qtw
...
class UI_MainWindow(object):
def setupUI(self, MainWindow):
window = qtw.QWidget()
self.someButton = qtw.QPushButton("button")
self.someLabel = qtw.QLabel("---")
...
MainWindow.setCentralWidget(window)
#blaqICE's answer is a good start but I'm noticing a handful of other things that are contributing to your issues.
The main things I'm seeing are your use of class inheritance and instance attributes. Overall organization seems to be working against you as well. The way you're doing it is workable but it gets confusing pretty quickly. I'm not sure how familiar you are with creating classes but I would spend some time in the python classes docs and looking at PySide example code.
If you want to fix your code the way it is all you need to do is fix the lines I commented on:
uiSomething.py
import PySide2.QtWidgets as qtw
class UI_MainWindow(object):
def setupUI(self, MainWindow): # consider using __init__ instead
self.window = qtw.QWidget() # make an instance attribute for later access with your class instance; not needed with __init__ and proper inheritance
self.main_layout = qtw.QVBoxLayout() # create a layout to hold everything
self.window.setLayout(self.main_layout) # set 'self.window' layout to the new 'self.main_layout'
self.someButton = qtw.QPushButton("button") # make an instance attribute for later access with your class instance
self.main_layout.addWidget(self.someButton) # add 'self.someButton' to 'self.main_layout'
self.someLabel = qtw.QLabel("---") # make an instance attribute for later access with your class instance
self.main_layout.addWidget(self.someLabel) # add 'self.someLabel' to 'self.main_layout'
MainWindow.setCentralWidget(self.window)
Something.py
from uiSomething import UI_MainWindow
class something(qtw.QMainWindow, UI_MainWindow):
def __init__(self):
super(something, self).__init__()
self.ui = UI_MainWindow()
self.ui.setupUI(self) # consider using __init__ in class definition instead; this line would not be needed
self.ui.someButton.clicked.connect(self.function) # 'self.ui' before 'someButton' to access instance's someButton
def function(self):
self.ui.someLabel.setText("change text") # this is fine
# this was not working because of the issue with someButton's signal connection
main.py
import Something
import PySide2.QtWidgets as qtw
# import sys
if __name__ == "__main__":
app = qtw.QApplication()
MainWindow = Something.something()
MainWindow.show()
app.exec_() # only one underscore; typically done as 'sys.exit(app.exec_())' to formally exit python interpreter
However, a better implementation would be:
uiSomething.py
import PySide2.QtWidgets as qtw
class UI_MainWidget(qtw.QWidget): # UI_MainWindow -> UI_MainWidget for clarity; inherit QWidget
def __init__(self, button_text="button", lable_click_text="change text", parent=None): # auto initialize object
super(UI_MainWidget, self).__init__(parent) # pass parent to inherited class __init__ function
# 'self.window' no longer needed due to __init__ and can be accessed simply through 'self'
self.parent = parent # set parent as instance variable for later use if needed
self.button_text = button_text # set custom arguments as instance variable for later use if needed
self.lable_click_text = lable_click_text # set custom arguments as instance variable for later use if needed
self.main_layout = qtw.QVBoxLayout() # create a layout to hold everything
self.setLayout(self.main_layout) # set the widget's layout to the new self.main_layout
self.someButton = qtw.QPushButton(self.button_text) # class argument for easy customization of other instances
self.someButton.clicked.connect(self.function) # moved to instantiation class for clarity and ease of access
self.main_layout.addWidget(self.someButton)
self.someLabel = qtw.QLabel("---") # placeholder text
self.main_layout.addWidget(self.someLabel)
# moved '.setCentralWidget(self)' to main window class for organization and ease of access
def function(self): # move to instantiation class for clarity, organization, and ease of access
# 'self.ui' no longer needed in instantiation class, use 'self' instead
self.someLabel.setText(self.lable_click_text) # class argument for easy customization of other instances
Something.py
import PySide2.QtWidgets as qtw # need to import module here as well due to inheritance
from uiSomething import UI_MainWidget
# for this class, double inheritance doesn't really have any effect. Stick with the main one it represents.
class UI_MainWindow(qtw.QMainWindow): # something -> UI_MainWindow for clarity; inherit QMainWindow only; PEP 8 naming
def __init__(self, parent=None): # added parent kwarg for parenting to other widgets if needed
super(UI_MainWindow, self).__init__(parent) # pass parent to inherited class __init__ function
self.ui = UI_MainWidget(parent=self)
# self.ui.setupUI(self) # no longer needed due to __init__; can delete
# using custom class keyword arguments
# self.ui = UI_MainWidget(button_text="new button name", lable_click_text="new button clicked", parent=self)
# self.ui = UI_MainWidget(button_text="another button name", lable_click_text="another button clicked", parent=self)
self.setCentralWidget(self.ui)
main.py
import Something
import PySide2.QtWidgets as qtw
import sys
if __name__ == "__main__":
app = qtw.QApplication()
MainWindow = Something.UI_MainWindow()
MainWindow.show()
sys.exit(app.exec_())
This question already has an answer here:
how to accept/ignore QKeyEvent
(1 answer)
Closed 3 years ago.
I have two PyQt5 widgets and both need keyboard input. Initially, one widget has the setFocusPolicy set to QtCore.Qt.StrongFocus, but when both widgets has this property activated, both of them get the input. I would like to initially set the input in one of them and if the user clicks in the other widget, the focus would be changed to the clicked widget or vice versa.
MRE:
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtWidgets import QOpenGLWidget, QWidget
import sys
class Renderizador(QOpenGLWidget):
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_W:
print("OpenGL")
super().keyPressEvent(event)
class Diedrico(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintEvent(self, event):
qp = QPainter(self)
qp.setPen(QPen(Qt.black))
qp.drawRect(0, 0, 1000, 1000) # Marco
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_W:
print("Widget")
super().keyPressEvent(event)
class UiVentana(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(UiVentana, self).__init__(parent)
self.resize(1500, 1015)
self.setFixedSize(1500, 1015)
self.statusBar().setSizeGripEnabled(False)
self.widget_central = QtWidgets.QWidget(self)
self.Renderizador = Renderizador(self.widget_central)
self.Renderizador.setGeometry(QtCore.QRect(0, 0, 1000, 1000))
self.Renderizador.setFocusPolicy(QtCore.Qt.StrongFocus)
visor = QtWidgets.QWidget(self.widget_central)
visor.setGeometry(1010, 510, 470, 460)
self.scene = QtWidgets.QGraphicsScene(visor)
self.view = QtWidgets.QGraphicsView(self.scene)
self.diedrico = Diedrico(visor)
self.diedrico.setFixedSize(470, 460)
self.view.setFocusPolicy(QtCore.Qt.StrongFocus)
self.scene.addWidget(self.diedrico)
self.setCentralWidget(self.widget_central)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_W:
print("Ui")
super().keyPressEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
ui = UiVentana()
ui.show()
sys.exit(app.exec_())
Based on your code, I'm not getting key events from the Diedrico widget, but only from the Renderizador and UiVentana instancies; with your implementation it seems very unlikely that you get key events from both the Renderizador and Diedrico widgets, since their parent hierarchy is completely different and they actually are members of different "physical" windows.
If you really get the Widget and OpenGL outputs from a single key event, it might be a bug, but, frankly, I doubt that, and that's mostly because there are some issues in your implementation, mostly due to confusing parent/child relationship.
If that's not the case (meaning that you're getting the key events from Renderizador and UiVentana), the explanation is simple: a QOpenGLWidget, as any basic QWidget class, doesn't process nor "consume" a key event; as soon as you call the base implementation with super() the event is propagated to its parents (UiVentana, in your case). If you want to stop the event propagation, just return without calling the base implementation of keyPressEvent.
Finally, some notes about your example.
When you want to add a widget to a scene, it must have a parent that is already embedded in the scene or it shouldn't have a parent at all (as in a top level widget). In your code you created the Diedrico widget setting its parent to a child of the "widget_central", which at that point is a widget without no parent (meaning that it would be a top level widget, as in a "window"): no matter what you do afterwards (setting it as the central widget), the topmost parent has not been embedded, and you can't add any of its child to a scene.
Qt itself warns about this when executing your code:
StdErr: QGraphicsProxyWidget::setWidget: cannot embed widget 0x911ae78 which is not a toplevel widget, and is not a child of an embedded widget
Then, you created the view, but you never actually add it to the main window or any of its children. You can see the Diedrico instance only because of the aforementioned problem: the widget is added to the main widget because of the parent set in the class initialization, but it's never added to the scene.
Be very careful when initializing widgets and setting parents, expecially when you're going to embed them into a QGraphicsScene: QWidgets added to a scene are actually QGraphicsProxyWidgets, not "actual" widgets, and some special care is required when dealing with them.
I was looking everywhere but I was unable to figure out answer to my problem. I need your help regarding QThread threading in PyQt5. For the purpose of learning I've created two classes in my main.py file. One of those classes is a GUI class and the other one is a thread created using QThread object.
from PyQt5 import QtCore, QtGui, QtWidgets
import threading_gui # contains GUI generated by pyuic5
class Change_values(QtCore.QThread):
def __init__(self, parent = None, str_variable, int_variable):
self.var1 = str_variable
self.var2 = int_variable
def run(self):
self.var1 += ' testing thread communication'
self.var2 += 4
class MainWindow(QtWidgets.QMainWindow, threading_gui.UI_MainWindow):
change_signal = QtCore.pyqtSignal(str, int, name='change_signal')
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_Run.clicked.connect(self.process_data)
self.thread1 = Change_values('I am', 3)
def process_data(self):
self.thread1.start()
This is as far as I managed to do. Now I need someone to explain me how can I emit signal from thread Change_values and display the content of variables var1 and var2 in my GUI thread.
I was able to find examples for PyQt4 but nothing useful when it comes to PyQt5. Thank you all in advance.
I'm developing an application with PySide 1.1.2, which is before the new style signals and slots were integrated. I haven't had any issue with most of my custom signals, except those which accept unicode or str types. Those with no parameters, or other types work just fine, but with unicode or str parameters, I get the error: "TypeError: Value types used on meta functions (including signals) need to be registered on meta type: str" on the emit statement.
Example of statements (these are of course in different classes):
self.emit(QtCore.SIGNAL('setCountType(str)'), self.countType)
self.connect(self.parent, QtCore.SIGNAL('setCountType(str)'), self.setCountType)
# part of a class that inherits from QWidget
def setCountType(self, value):
self.countType = value
The emit statement is the one that throws the error.
PySide 1.1.2 supports the new style. In my case, signals using "strings" works flawlessly.
In case you need some help, check this: http://qt-project.org/wiki/Signals_and_Slots_in_PySide
An example:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class Window(QMainWindow):
signal = Signal(str)
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.button = QPushButton()
self.button.setText("Test")
self.setCentralWidget(self.button)
self.button.clicked.connect(self.button_clicked)
self.signal.connect(self.print_text)
#Slot()
def button_clicked(self):
print('button clicked')
self.signal.emit("It works!")
#Slot(str)
def print_text(self, text: str):
print(text)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_()
sys.exit(0)