I'm confused with the signals connections with pyqt5. My signals are working I'm just trying to understand the inheritance.
My imports are:
from PyQt5 import QtCore, QtGui, QtWidgets
Then I have a button
self.button_send_mail = QtWidgets.QPushButton('Resend', self)
self.button_send_mail.clicked.connect(self.button_send_mail_pressed)
If I open QPushButton.py there's no defined method "clicked". From Qt Docs I read that QPushButton inherits from QAbstractButton. I open QAbstractButton.py and there's the method clicked, but why my ide (pycharm 2017 with the pyqt5 modules installed) doesn't list the "clicked" method after I write self.button_send_mail.c...??
And the same happens for .connect but in this case, I have the warning: "cannot find the reference "connect" in function.
So, it works but I'd like to understand better, is it maybe because pyqt is wrapping C functions? and pycharm cannot access to them?
Thanks
Related
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 am working on a module for 3D slicer. A chunk of the template code is pasted below. It is using qt for GUI. I need to add my own GUI here, But I am not able to find how to add toolbar here. I am not able to find any documentation regarding this. Whenever I google I get PyQt4, is that different from this ? So, my question is please explain the diference between qt and PyQt4 and how can I add toolbar here ?
def __init__(self, widgetClass=None):
self.parent = qt.QFrame()
self.parent.setLayout( qt.QVBoxLayout() )
# TODO: should have way to pop up python interactor
self.buttons = qt.QFrame()
self.buttons.setLayout( qt.QHBoxLayout() )
self.parent.layout().addWidget(self.buttons)
self.addDataButton = qt.QPushButton("Add Data")
self.buttons.layout().addWidget(self.addDataButton)
self.addDataButton.connect("clicked()",slicer.app.ioManager().openAddDataDialog)
self.loadSceneButton = qt.QPushButton("Load Scene")
self.buttons.layout().addWidget(self.loadSceneButton)
self.loadSceneButton.connect("clicked()",slicer.app.ioManager().openLoadSceneDialog)
import statement
import vtk, qt, ctk, slicer
I think the form/GUI is created by tool. You can have a look at this
You can add your PyQt4 code in this, that should work fine.
If the import statement is qt, 2 solution : it use an alias (it would be weird) or it use a pyqt wrapper which allow to use PySide2, PyQt5, PySide and PyQt4 the same base code.
Maybe this one : https://github.com/mottosso/Qt.py
maybe another ... If you have a requirement.txt you should be able to know
please explain the diference between qt and PyQt4
Qt is cross-platform application framework, using C++ extensions.
In order to use it in python you need a wrapper and this is what PyQt, PySide and the rest are.
Check your application documentation and probably you will find if it uses PyQt4,5, PySide or something else.
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()
I am using PyQt4 and would like to be able to use "Organize Imports", so I can just write something like:
QPixmap(":/filename.png")
and hit Ctrl+Shift+F (Organize Imports) and this is added:
from PyQt4.QtGui import QPixmap
But this does not work for me.
My question is: Is this feature available? How can I activate it?
Note: The default auto-complete works with PyQt.
This works up to a level... PyDev can do what you just described (although there's a typo there: the shortcut is actually Ctrl+Shift+O), but only for source modules (if you're going to the internal tokens level). In the case of PyQt4, it only goes to the module level.
So, you could do:
QtGui and it'd show PyQt4.QtGui, but it doesn't go on to analyze tokens to suggest inside QtGui (although it'll suggest tokens inside QtGui after you have the PyQt4.QtGui import already).
There's an issue reported at the tracker for that already: https://sw-brainwy.rhcloud.com/tracker/PyDev/176 (although it's not very high in the priority list as it still does not have any votes).
A note: code-completion on the QtGui would already suggest that option or you could just do a Ctrl+1 in that same line to be offered the option to add the import (without having to resort to doing Ctrl+Shift+O).
I made a gui using Pyqt earlier. I did all the coding on the terminal in Linux. Now I am going to make a big project in Pyqt.
Is there any sdk which will help me to overcome the coding part, so I can just drag and drop the items? I know about qt designer, but I don't know how I should write and integrate it with Python.
Do you have suggestions on what program to use for this?
Qt Designer when coupled with PyQt4 is usually used only for the layout process, as opposed to defining signals, buddies, etc. You perform the visual layout of your widgets, and save the .ui file.
Using pyuic4, you can then compile the .ui -> .py, and import that into your coded project.
Though there are probably 3 different approaches to using the UI at this point, what I typically do is multiple inheritance. If class MyMainWindow is meant to be a QMainWindow, then I inherit my class from QMainWindow, and the UI class.
Something like this...
pyuic4 myMainWindow.ui -o myMainWindowUI.py
main.py
from PyQt4 import QtGui
from myMainWindowUI import Ui_MainWindow
class MyMainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs)
super(MyMainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
The setupUi method applies your entire UI design to the class and you can now access all of the widgets you designed by their object names.
win = MyMainWindow()
print win.listWidget
print win.button1
win.show()
See Creating interfaces visually with designer
if using pyqt4 use pyuic4
if using pyside use pyside-uic
These compile the output from QTDesigner into a python file.
You can google you way to usage of these command line tools but Riverbank is usually a good reference