Can't use "setGraphicsEffect"?
-
I've been trying to implement methods to add some shadow effects to some parts of the UI (i.e. the header) and was told to import/install PySide, PyQt and QT-PyQt-PySide-Custom-Widgets (note I'm using PyQt5 and PySide6) However, setGraphicsEffects doesn't seem to work. In other words, it acts like there is no said function available. Here's what the code looks like:
shadow_elements = { "left_menu","frame_3", "frame_5", "header", "frame_8"} class MainWindow(QMainWindow): def __init__(self,parent=None): QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.setMinimumSize(850,600) for x in shadow_elements: effect = QtWidgets.QGraphicsDropShadowEffect(self) effect.setBlurRadius(18) effect.setXOffset(0) effect.setYOffset(0) effect.setColor(QColor(0,0,0,255)) getattr(self.ui,x).setGraphicsEffect(effect) #isn't highlighted like the rest of teh functions here self.show()
Am I missing something?
-
Hi,
Please provide a complete minimal script that shows the issue.
-
Shadows can't be drawn outside of the parent widget, only inside.
See https://forum.qt.io/post/740663 -
@SGaist Here's the error it gives me when I run the file:
Traceback (most recent call last): File "C:\Users\UserPC\OneDrive\Documentos\ProjectSICI\propiedadesmain.py", line 12, in <module> from Custom_Widgets.Widgets import * File "C:\Users\UserPC\AppData\Local\Programs\Python\Python311\Lib\site-packages\Custom_Widgets\__init__.py", line 12, in <module> from Custom_Widgets.Qss.SvgToPngIcons import NewIconsGenerator File "C:\Users\UserPC\AppData\Local\Programs\Python\Python311\Lib\site-packages\Custom_Widgets\Qss\SvgToPngIcons.py", line 22, in <module> import cairosvg File "C:\Users\UserPC\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairosvg\__init__.py", line 26, in <module> from . import surface # noqa isort:skip ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\UserPC\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairosvg\surface.py", line 9, in <module> import cairocffi as cairo File "C:\Users\UserPC\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairocffi\__init__.py", line 47, in <module> cairo = dlopen( ^^^^^^^ File "C:\Users\UserPC\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairocffi\__init__.py", line 44, in dlopen raise OSError(error_message) # pragma: no cover ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OSError: no library called "cairo-2" was found no library called "cairo" was found no library called "libcairo-2" was found cannot load library 'libcairo.so.2': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.so.2' cannot load library 'libcairo.2.dylib': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.2.dylib' cannot load library 'libcairo-2.dll': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo-2.dll'
As for the full script of the file, here's what I wrote with the imports:
import os import sys import csv from propiedades import * from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import * from PyQt5.QtCore import * from Custom_Widgets.Widgets import * # from PySide6.QtGui import * # from PySide6.QtCore import * # from PySide6.QtWidgets import * shadow_elements = { "left_menu","frame_3", "frame_5", "header", "frame_8"} from functools import partial class MainWindow(QMainWindow): def __init__(self,parent=None): QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.setMinimumSize(850,600) loadJsonStyle(self, self.ui) for x in shadow_elements: effect = QtWidgets.QGraphicsDropShadowEffect(self) effect.setBlurRadius(18) effect.setXOffset(0) effect.setYOffset(0) effect.setColor(QColor(0,0,0,255)) getattr(self.ui,x).setGraphicsEffect(effect) self.show() if __name__ == "__main__": import sys app = QApplication(sys.argv) window = QMainWindow() window.show() sys.exit(app.exec_())
-
@Rangerguy128
In your original post you just said "setGraphicsEffects doesn't seem to work", you did not mention anything about the error you show now? TheCustom_Widgets
module seems to need this cairo stuff. You seem to be using Windows: do you have alibcairo-2.dll
file anywhere at all? Googlinglibcairo-2.dll
shows plenty of results about it being "missing", e.g. maybe Python: OSError: cannot load library libcairo.so.2. -
@JonB I see, maybe that's also why setGraphicsEffect isn't highlighted like the rest? Is there anyway to import the cairo library? I'm using Python 3.11.9 and I'm not sure if it's because of the version or not (cant use 3.12 as that one doesn't support PyQt6-tools yet)
-
@Rangerguy128
This is not my area. I suggested you discover whetherlibcairo-2.dll
exists anywhere for you. Searching for e.g.python cairo
indicates to me you have to install it, perhaps viapip
orpyPI
? You may have done that, but I suspect that is only the bindings for Python? Do you have to install cairo itself as a (binary) package, which includes e.g.libcairo-2.dll
, so that the Python bindings can use it? -
The first thing you should do is avoid star imports. You don't know what you are pulling there and it makes your code less readable and understandable.
Next, remove everything that is not related to your current Qt issue. Deal with each problem one after the other.