I recently decided to write my first app with Python and PySide. But I have a problem and hope you guys can help.
Python keeps raising exceptions that the "Internal C++ Object" is deleted. From my limited experience with Python I figure that my object is going out of scope and being deleted by Python's Garbage Collector.
So how would I go about designing a multi-page application in Python with PySide. And being able to keep my QWidgets so I can show the page again.
Thanks for your time.
Update (Code)
instancing = None
def instance():
global instancing
if instancing == None:
instancing = WPZKernel()
return instancing
class WPZKernel:
win = None
mainscreen = None
def mainwindow(self):
if self.win == None:
self.win = GMKMainWindow(self)
return self.win
def main_panel(self):
if self.mainscreen == None:
self.mainscreen = GMKMainScreen(self.mainwindow())
return self.mainscreen
I would then normally access the mainpanel by calling:
import kernel
kernel.instance().main_panel()
So am I going about this the wrong way?
After some searching and hair pulling, I found the solution. I was showing all the pages by setting them as the central widget, and when reading the QMainWindow documentation I found that my widget basically gets deleted by qt as stated:
Note: QMainWindow takes ownership of
the widget pointer and deletes it at
the appropriate time.
So to develop a Multi-Page application rather take a look at QStackedWidget.
See here: PySide Pitfalls.
If a QObject falls out of scope in
Python, it will get deleted. You have
to take care of keeping a reference to
the object:
Store it as an attribute of an object you keep around, e.g.
self.window = QMainWindow()
Pass a parent QObject to the object’s constructor, so it gets owned
by the parent
Related
I am writing an application that uses a Qt GUI and am now trying to multithread it (first time learner). A longer task will be contained within the worker thread eventually but output logs will be required to be written as it goes. The GUI class has a method to output these logs to a plain text widget. Up until now, everything has been running within the GUI class.
I currently have the following code (included the important bits for brevity):
class Worker(QRunnable):
#pyqtSlot()
def run(self):
Ui.logentry(self, "Test")
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi(resourcepath('./pycdra.ui'), self)
self.outputlog = self.findChild(QtWidgets.QPlainTextEdit, 'outputlog')
self.button = self.findChild(QtWidgets.QPushButton, 'button')
self.button.clicked.connect(self.Button)
self.threadpool = QThreadPool()
self.logentry("Available threads: %d" % self.threadpool.maxThreadCount())
def Button(self):
worker = Worker()
self.threadpool.start(worker)
def logentry(self, returntext):
self.outputlog.appendPlainText(self.timestamp() + " " + str(returntext))
self.outputlog.repaint()
def timestamp(self):
import datetime
ts = datetime.datetime.now(tz=None).strftime("%Y-%m-%d %H:%M:%S"))
return ts
def applic():
app = QApplication(sys.argv)
window = Ui()
window.show()
sys.exit(app.exec_())
applic()
When I try running this, the GUI loads perfectly, but upon pushing button the Ui.logentry part of Worker returns the error: AttributeError: 'Worker' object has no attribute 'outputlog'
I attempted making logentry() and timestamp() global so they can access and be accessed by both classes, but the closest I got with the line Ui.self.outputlog.appendPlainText(self.timestamp() + " " + str(returntext)) was that 'Ui' object has no attribute 'self'.
What am I missing? Am I right in making them global or are there other means to do this?
There are various problems with your code. I'll try to address them in order of importance.
logentry is an instance method of Ui, which means that it requires an instance of Ui in order to be run; what you're trying to achieve would never work since that self refers to an instance of Worker, which clearly doesn't have any outputlog attribute (which is what logentry expects instead). Remember: the self argument is not there just for fun, it always refers to the object (the instance, for instance methods) the function is called from; self.logentry means "call the function logentry using self as first argument. Since, in your case, self refers to an instance of Ui, that explains your main issue, since that instance doesn't have that attribute.
None is exactly what its name says: nothing. The timestamp function will throw another AttributeError, since there's no strftime attribute in None. That is part of a datetime object, so you have to get a now object, then call its strftime function against it. In any case, the tz argument requires a tzinfo subclass.
pyqtSlot only works for Qt classes that inherit from QObject (such as QWidget). QRunnable isn't one of those classes. You can check the whole inheritance tree in the header of the documentation in each class: if there's a "Inherits:" field, go up until there's none. If you get a QObject inheritance at some point, then you can use slots and signals for that object, otherwise not. Consider that: QWidget also inherits from QObject; all Qt widgets inherit from QWidget; there are Qt classes that inherit from QObject but are not widgets.
Even ignoring all the above, access to UI elements is always forbidden from external threads (including QRunnable objects); while you can theoretically (but unreliably) get their properties, trying to set them will most likely cause a crash; in order to change something in a UI element, using signals is mandatory. Note that "access" also includes creation, and always results in a crash.
Calling repaint is a common (and wrong) attempt to solve the above issue; that's unnecessary, as properly setting widget properties (like using appendPlainText()) already results in a scheduled repaint on its own; there are very few cases for which repaint is actually necessary, and the rule of thumb is that if you're calling it you probably don't know what your doing or why your doing it. In any case, calling update() is always preferred, and it must always be called from the main UI thread anyway.
Using imports in a function is rarely required, as import statements should always be in the very beginning of the script; while there are cases for which imports can (or should) be done later or in a specific function, doing them in a function that is likely to be called often, makes using them there completely pointless. Also, datetime is part of the standard library, so importing it on demand will hardly affect performance (especially considering its "performance weight" against what a big library like Qt is compared to it).
When the ui is loaded from a .ui file (or a pyuic generated file), PyQt alread creates all widgets as instance attributes, so there's no need for findChild. Unfortunately there are a lot of tutorials that suggest that approach, and they are just completely and plain wrong. You can already access those widgets as self.outputlog and self.button right after uic.loadUi.
Function names (like variables and attributes) should always begin with a lower case letter, as only classes and constants should begin with upper cases (see the official Style Guide for Python Code). Also, object names should always explain what those object do (see "self-documenting code"): a function that does an "action" should have a verb; if it's named "Button" it doesn't tell me that it's going to do some processing, and that's not a very good thing.
A "main" function (like your applic) usually makes sense within the common if __name__ == '__main__': block, which ensures that that function doesn't get called in case the file gets imported instead of being directly run. (See this answer and the related question).
Since QRunnable doesn't inherit from QObject, we can create a QObject subclass that acts as a signal "proxy", and make it a member of the QRunnable instance. Then we must connect to that signal everytime we create a new Worker object.
Here is a revised version of your code, based on the above points.
import datetime
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
class WorkerSignals(QObject):
mySignal = pyqtSignal(str)
class Worker(QRunnable):
def __init__(self):
super().__init__()
self.signalProxy = WorkerSignals()
self.mySignal = self.signalProxy.mySignal
def run(self):
self.mySignal.emit("Test")
class Ui(QMainWindow):
def __init__(self):
super(Ui, self).__init__()
loadUi('./pycdra.ui', self)
self.button.clicked.connect(self.startWorker)
self.threadpool = QThreadPool()
self.logentry("Available threads: %d" % self.threadpool.maxThreadCount())
def startWorker(self):
worker = Worker()
worker.mySignal.connect(self.logentry)
self.threadpool.start(worker)
def logentry(self, returntext):
self.outputlog.appendPlainText(self.timestamp() + " " + str(returntext))
def timestamp(self):
ts = datetime.datetime.now()
return ts.strftime("%Y-%m-%d %H:%M:%S")
def applic():
import sys
app = QApplication(sys.argv)
window = Ui()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
applic()
I suggest you to carefully study the provided code and the differences with yours, and then do some careful, patient research both on the links given above and the following topics:
classes and instances, and the meaning of self;
python types (including None);
what is event driven programming and how it relates with graphical interfaces;
the most important Qt classes, QObject and QWidget, and all their properties and functions (yes, they are a lot);
general PyQt related topics
(most importantly, signals/slots and properties);
code styling and good practices;
Use the class' initialised form
class foo:pass
selfuotsideclass=foo()
Use variable selfoutsideclass as self
I'm learning to use PyQt5 and have run across a problem. My code is attempting to just draw a simple black box in the QMainWindow object by writing a second class PaintWidget which inherits from QWidget. I've posted my code first, and the correct one below it.
class PaintWidget(QWidget):
def __init__(self):
super().__init__()
self.qp = QPainter()
self.initUI()
def initUI(self):
self.qp.fillRect(1,1,100,100, Qt.black)
Correct:
class PaintWidget(QWidget):
def paintEvent(self, event):
qp = QPainter(self)
qp.fillRect(1, 1, 100, 100, Qt.black)
This is what confuses me. In order to create this class, we need to inherint from the super class QWidget, inorder to do so we use the function super().__init__() under __init__(self). We then set up the QPaint object which we will use in our method initUI() which actually does the work. Now this doesn't work when I run it.
The second, correct class, doesn't even seem to inherent, since it has no super().__init__(), even worse, it is setting up a method that is never even called (paintevent(self, event)), which takes an argument that seemingly comes from nowhere. Can someone point out why I'm wrong?
There is absolutely no difference to inheritance between the two cases. In both cases you say class PaintWidget(QWidget), so you are inheriting the QWidget.
The difference is in where you draw. In constructor (__init__), the widget is not yet mapped to the screen, so if you try to draw there, it won't have effect.
When the widget is actually displayed on screen, the system will invoke the paintEvent, which is a virtual method of the QWidget, and that is where you must draw the content. You only define that method in the second example.
Note that you need fresh QPainter in each invocation of the paintEvent. Creating one in the constructor and then using it in paintEvent would not work.
Also, most windowing systems don't remember the content of the widget when it is not actually visible on screen and rely on being able to call the paintEvent whenever the widget becomes visible again. So the method will likely be called many times. In contrast, the constructor, __init__, is only called once when creating the object.
I have a strange issue when adding a widget to a PyQt5 application.
The following is the actual code, stripped off of everything that doesn't seem related (like translateUI):
class OllRoot(preferences.Group):
"""Basic openLilyLib installation"""
def __init__(self, page):
super(OllRoot, self).__init__(page)
self.setParent(page)
self.changedRoot()
layout = QGridLayout()
self.setLayout(layout)
self.directory = widgets.urlrequester.UrlRequester()
self.directory.changed.connect(self.changedRoot)
layout.addWidget(self.directory, 0, 1)
def changedRoot(self):
print("Self:", self)
print("Parent:", self.parent())
self.parent().changed.emit()
# TODO: Check for proper openLilyLib installation
When the constructor is called, parent() is correctly set to the object that has been passed in as page, so the two proper objects are printed.
Self: <preferences.openlilylib.OllRoot object at 0x7f855a1de288>
Parent: <preferences.openlilylib.OpenLilyLibPrefs object at 0x7f855a1bcb88>
However, when I make a change in the self.directory widget changedRoot is called again (as I've connected it), but now the parent seems to have disappeared:
Self: <preferences.openlilylib.OllRoot object at 0x7f855a1de288>
Parent: <PyQt5.QtWidgets.QWidget object at 0x7f855a1dbc18>
Question:
Am I doing anything wrong with the setParent?
Am I doing anything wrong with the connect?
Does the object somehow "forget" its parent?
PS: A comparable file which served as a model can be found here: https://github.com/wbsoft/frescobaldi/blob/master/frescobaldi_app/preferences/general.py#L56.
Whenever a widget is added to a layout, Qt will automatically re-parent it so that it becomes a child of the widget the layout is set on. Calling setParent (with a different widget) in __init__ will have no lasting effect.
See: Tips for Using Layouts in the Layout Management Overview.
I am trying to get drag and drop (with images or files) working for my listwidget in pyqt5. I can find a lot of examples with pyqt4, but there is one part that does not work in the newer version:
In the "dropevent":
self.emit(QtCore.SIGNAL("dropped"), links)
and in the MainForm:
self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
I read a lot of posts but can't seem to find an answer how this should be written down with pyqt5. Could anyone please help me with this?
link to the entire example:
PyQT4: Drag and drop files into QListWidget
Simple.
The signal must come first
The slot come as parameter.
You must connect only after the signal was created
You must emit only after the connection was made.
Here we go with a small example:
self.signalOwner.mySignal.connect(self.slotFunction)
In your example let's say and consider that the view owns the signal and that pictureDropped is your slot function, so:
self.view.dropped.connect(self.pictureDropped)
Remember, your signal must emit certain type(s) or nothing at all andddd your #pyqtSlot function must receive same type(s) with the function receiving the same amount of parameter that your signal emits.
I have made a post some short time ago about drag and drop with images, had many difficulties to figure it out how to accept events and how the classes behave, it's a bit deeper than only signals so, if you need here is the POST :D
Your linked example uses the old-style signal and slot syntax, whereas you need to use the new-style syntax. In PyQt4 (versions 4.5 or later), you could use either style, but PyQt5 only supports the new style.
The old-style syntax allowed custom signals to be emitted on-the-fly, but the new-style syntax demands that the signal is declared beforehand on the class:
class ListWidget(QtWidgets.QListWidget):
dropped = QtCore.pyqtSignal(list)
def __init__(self, parent=None):
...
def dropEvent(self, event):
...
self.dropped.emit(list_of_files)
The signal connection is then made like this:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
...
self.listWidget = ListWidget()
self.listWidget.dropped.connect(self.handleDropped)
def handleDropped(self, list_of_files):
print('dropped:', list_of_files)
I'm working on a game using Kivy where the player can press a key to shoot a bomb. Here's a simplified function:
def shoot(self, world):
# self is the player widget
bomb = Bomb(pos=self.pos)
world.add_entity(bomb)
The funny thing, when the player shoots, the bomb keeps moving with him wherever he goes.
After debugging I realized this is because both the player and the bomb share the velocity property.
The Player class and Bomb class both inherit Entity, which defined velocity = ObjectProperty(Vector(0, 0)) at class level. Debugging at runtime shows the two objects reference the exact same object.
The thing is, Kivy properties should actually create separate attributes on each instance. So why is this unwanted shared state, and how can I fix this?
The solution
Thanks to a comment by #Tshirtman to the answer on this question, I have found the problem and the solution.
Turns out Kivy properties have a gotcha, where the initial value set when instantiating them will be shared by all instances of the class.
To clarify:
# this implementation results in the property shared by all instances
class MyWidget(Widget):
my_field = ObjectProperty(MyObject())
w1 = MyWidget()
w2 = MyWidget()
w1.my_field is w2.my_field # True
As you can see, my_field is actually shared between the two instances.
To avoid this behavior, make sure to set the attribute on the particular object, in addition to setting the class-level property.
For example:
class MyWidget(Widget):
my_field = ObjectProperty()
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.my_field = MyObject() # explicitly set the value on the object
w1 = MyWidget()
w2 = MyWidget()
w1.my_field is w2.my_field # False
When explicitly setting the value for the field, the field is not shared between instances. Hope this helps someone.
Kivy's widgets would not do this automatically, probably you've written code that inadvertently causes it. Post a runnable example demonstrating the problem if you want help to track it down.