Setting window style in PyQT/PySide? - python

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

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.

pyqtgraph QGraphicsWidget import error

I'm trying to make a program using PyQt5 and pyqtgraph, however, I keep running into this error:
ImportError: cannot import name 'QGraphWidget'
I used QtDesigner to make a form and promoted a QGraphicsWidget. I know I did it correctly (I've done it at least 10 times to try and resolve the issue), but the error persists.
I'm using Windows 7, Anaconda, and PyCharm, but I tried running the code in other environments and still got the error.
In the documentation it implies that the promoted name can be anything, however, this seems to be untrue (at least at the moment).
Under “Promoted class name”, enter the class name you wish to use (“PlotWidget”, “GraphicsLayoutWidget”, etc).
After re-doing my QGraphicsWidget for the 6th time, I decided to name it one of the example names in the tutorial, which seems to have solved the problem.
In other words name your widget and promoted widget "PlotWidget", "ImageView", "GraphicsLayoutView", or "GraphicsView". (Please keep in mind I have only tested "PlotWidget".)

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

PyDev organize imports PyQt4

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

how to disable the window maximize icon using PyQt4?

I would like to know how to disable the window Maximise button in pyqt4. I am at present using QWidget.setFixedSize (self, QSize) to prevent user window resizing, however the maximise button is still enabled and when pressed causes the application to move to the top left corner of the screen. I am basically wanting to replicate the behaviour of the Windows calculator application, where the maximise icon is greyed out. Does anyone know how to achieve this with PyQt4?
Haven't worked with it but research seems to point to messing with the window flags.
QWidget has a method called setWindowFlags.
Here is the doc for the Qt.WindowFlags class.
Here is a reference for all of the flags. Look for Qt.WindowMaximizeButtonHint
In general it seems like you need to find a way to enable the Qt.CustomizeWindowHint flag and disable the Qt.WindowMaximizeButtonHint flag. Either way, you probably want this in addition to setFixedSize so that's a good start.
Edit:
Something like
win.setWindowFlags(win.windowFlags() | QtCore.Qt.CustomizeWindowHint)
win.setWindowFlags(win.windowFlags() & ~QtCore.Qt.WindowMaximizeButtonHint)
Assuming your import is something like this
from PyQt4 import QtCore
This would turn on the CustomizeWindowHint flag and turn off the WindowMaximizeButtonHint flag, I hope. Let me know if this works at all.
Edit:
As discovered by OP, the only call necessary for his desired outcome:
win.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)
but beware, since this will also remove the close button and potentially mess with other window flags.
This works perfectly:
MainWindow.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint)
you could set the maximumSize and minimumSize with the same values, it'll get to dissapear maximise button
This helped me:
Form.setMaximumSize(QtCore.QSize(width, height))
It would go here in your class:
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(328, 212)
Form.setMaximumSize(QtCore.QSize(352, 189))
This works in PyQT6:
MainWindow.setWindowFlags(QtCore.Qt.WindowType.CustomizeWindowHint | QtCore.Qt.WindowType.WindowCloseButtonHint | QtCore.Qt.WindowType.WindowMinimizeButtonHint)
Hope it's useful!

Categories