I wanted to port a PyQt4 app of mine to PyQt5 and came across a subtle problem.
At some point I check if a custom QThread object (worker) has still some specific signal connected, which I have done in PyQt4 like so (exemplary code):
if worker.receivers(PyQt4.QtCore.SIGNAL("signalFinished(QString,QString)")):
do_stuff()
Is there any way to do this in PyQt5? The PyQt5 reference is not very helpful, and always leeds me to the C++ reference, where it is still the same behaviour.
Of course there is a more 'pythonic' solution using a try-except-pass block instead of an if statement, but I am still wondering for the 'PyQt signal' way.
With the new-style syntax, the equivalent code would simply be:
if worker.receivers(worker.signalFinished[str, str])):
do_stuff()
Related
I have a basic PyQt5 application. I want to make it so that when a certain button is clicked, a function is called that will run until another button is clicked. How is it possible to implement it?
Something like this (of course it doesn't work):
...
self.ui.start_button.clicked.connect(infinity_before_cancel_method)
self.ui.stop_button.clicked.connect(infinity_before_cancel_method)
...
def infinity_before_cancel_method():
...
while True:
do_something()
I have absolutely no idea how to do this in PyQt5. I've seen that this can be done using a multiprocessing library or similar, but I don't quite understand how to apply it to a PyQt5 application. Any hints would be helpful!
While this question has already been answered at Embeding PySide/PyQt widget into Qt/C++ application . I could not reproduce the code because I did not have OPs functions and more importantly the post is 8+ years old and QT has developed a lot since then, especially when it comes to Python.
I need a method to have my PySide widget called by C++ QT main window/application and essentially work as a QColor, I need to call it and it will return a value just like how QColor works.
Simplified explanation:
I need to call my PySide widget in C++ QT like this
a = CustomPySideWidget()
I need to be able to recieve data to the variable a, this data can be returned as a simple string so the data type will be C++ compatible.
Is there any methods to do this via PySide or PyQT?
Thank you for any help.
I'm very new to pyside, qt and python.
I managed to setup a project with a basic window and a push button which closes the app.
My problem is, that somehow vscode won't show all properties available, even though the code runs with them fine.
Note how a bunch of other properties are suggested, except for the signal clicked. If I hover over clicked, it tells me clicked: Any
Only during debugging, vscode tells me what clicked is:
Current setup:
OS: Linux
Virtualenv with pyside2 installed
Using ms-python.python and ms-python.vscode-pylance
ui/MainWindow.ui file and corresponding generated ui/MainWindow_ui.ui file with pyside2-uic
You can generate the stubs manually by running
pyside6-genpyi all
PySide2 have the same tools, it's not just for PySide6.
I use PDM to handle my project, with the last one I execute:
pdm run pyside6-genpyi all
And PyLance detect all my stubs:
If you got error with the Signal, it's because Qt/PySide do some magic tricks and convert Signal to SignalInstance. My solution is to cast SignalInstance to get to right hint.
from typing import cast
from PySide6.QtCore import Signal, SignalInstance
class ConnectionPanel(QtWidgets.QWidget):
connected = cast(SignalInstance, Signal())
disconnected = cast(SignalInstance, Signal())
Pylance tries to use stub files(which you can traverse with Go to definition) of PySide6 library to offer intellisense features. According to Qt docs clicked signal should be defined in QAbstractButton class but in type definitions (stubs) we can't see this signal definitions. If you look closely PyCharm gathers it from QAbstractButton class in your linked video but Pylance in Vs Code searches it in QPushButton.
This was a known issue in Pylance since PySide2 but according to the Pylance repo this may originate from Qt for Python's incorrect type definitions:
Libraries that have typing that's either incorrect or doesn't work well with PyLance. Best and maybe most known example I have is PySide2 (but see issues with other libraries in earlier posts): tons(!) of errors for correct, working code, which makes spotting real errors difficult. The same code in PyQt5 (which is basically a twin sibling to PySide2) doesn't raise any complaints from PyLance, so I'm assuming the problem is with PySide2's typing. While I'm aware that neither PySide2's typing nor PyLance's analysis results are necessarily incorrect, fact is that most of the reported errors should not be present, so for sake of simplicity I'll just call it incorrect.
The reason it can access at runtime is; it's created instance over shiboken QObject type I guess. I don't have any workarounds but it doesn't bother me because I prefer following official Qt documentation when looking for which signals available in a class definition.
EDIT: There was a resolved bug report on this which is the reason PyCharm able to handle auto-completion. Opening another on VS Code should help.
Since I had this exact same problem, I found this very informative comment on a PySide bug report: https://bugreports.qt.io/browse/PYSIDE-1603
Our stubs are auto-generated by introspection, signals are not included. At the moment, we need to discuss how we should support signals.
So, not a bug, just a missing feature.
I am writing a small application in Qt (with PySide in Python), which makes extensive use of QGraphicsView and displays animations (sort of). There are a few buttons to start/stop/pause and a few other widgets.
At first, I thought I should use QThread to handle the graphics and send signals to the main thread to QGraphicsScene etc., so I began implementing it. It worked, but I was not very satisfied with it - I tried a few different ways, but I think I overcomplicated it too much and the signal/slot mechanism and locking QMutexes was not even that fast.
Then (I am new to PySide), I found the processEvents() method in the docs. I tried implementing it and it works just the way I want, it's fast and simple.
Right now I am using it and everything is done on the main thread, and the GUI is responsive.
My questions are: Is it wrong to use that method in that way? Should I just stick to QThreads?
These questions are related to these...
How to make Qt work when main thread is busy?
Should I use QCoreApplication::processEvents() or QApplication::processEvents()?
... where others say it should not be used.
I'm programming (in python) GDK without GTK, simply as a x11 abstraction. THIS POST IS MY LAST CHANCE.
My problem is that I don't know how capture the GDK window's signals/events or what are their names.
When I do:
window = gdk.Window(
gdk.get_default_root_window(),
width=400,
height=200,
window_type=gdk.WINDOW_CHILD,
wclass=gdk.INPUT_OUTPUT,
event_mask=gdk.KEY_PRESS_MASK | gdk.MOTION_NOTIFY | gdk.EXPOSURE_MASK)
window.connect("key_press_event", on_key)
I get:
unknown signal name: key_press_event
GTK and PYGTK references talk about classes, functions and constants but nothing about their interrelation so they don't help.
Is it about glib main loop?
I need some examples. Any good GDK tutorial or source code? Any glib.MainLoop example for capturing GDK signals?
Thank you.
You can use gdk_event_handler_set to set the event handler (which is also the function used by gtk). Don't know which python binding u r using but I think you can easily find the corresponding python function. (In gi, it's simply Gdk.Event.handler_set)
You can check here for an example (There is also a non-block example). Although they are in C.
You're trying to connect to key-press-event, which is a gtk.Widget signal. gdk.Window doesn't inherit from gtk.Widget, so it doesn't have that signal.
In the GTK documentation, the little sections near the top labeled "Object Hierarchy" tell you how the classes relate together. There isn't really such thing as a GDK tutorial since one hardly ever uses it without GTK. I don't think it's very useful to do so either. Perhaps you can elaborate on what you are trying to achieve?