PyDev organize imports PyQt4 - python

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).

Related

How to properly setup vscode with pyside? Missing suggestions

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.

PyQt5 connect signals method

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

how to use qt in python

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.

PyQt5 receivers

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()

Setting window style in PyQT/PySide?

I've been looking for how to do this and I've found places where the subject comes up, but none of the suggestions actually work for me, even though they seem to work out okay for the questioner (they don't even list what to import). I ran across self.setWindowFlags(Qt.FramelessWindowHint) but it doesn't seem to work regardless of the import I try (QtGui.FramelessWindowHint, QtCore.FramelessWindowHint, etc.).
Any ideas?
u need to import QtCore
so the code will look like this :
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
whenever you see Qt.something put in mind that they are talking about the Qt class inside QtCore module .
hope this helps

Categories