PyQt user logo in center of MDI Area - python

I have been working on PyQt QMainWindow QMdiArea Class. I have been able to change the background colour as it is needed for my application.
However, I am unable to add a logo in the centre of the window.
I have tried QBrush but that just insert logo in full QMdiArea.
Furthermore, I have tried paintEvent overridden method but that seems to not work.
Please find enclosed my code and snapshot of the code output below:
# Import necessary libraries
import sys
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtGui import QColor, QBrush, QPainter
from PyQt5.QtWidgets import QStyleFactory, QWidget, QMainWindow, QMdiArea
class MDI_Window(QMainWindow, QWidget):
def __init__(self):
super().__init__()
self.centralWidget = QWidget(self)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
self.window_initialize()
self.show()
def window_initialize(self):
title = 'MDI'
self.setWindowTitle(title)
self.setWindowIcon(QtGui.QIcon("Some_Icon.png"))
self.setMinimumSize(800, 600)
self.mdi.setBackground(QBrush(QColor(169, 169, 169)))
self.showMaximized()
def paintEvent(self, event):
self.mdi.paintEvent(event)
self.painter = QPainter(self)
# For testing logo
self.painter.drawPixmap(500, 500, 500, 500, QtGui.QPixmap("KDS_Main-Window.png"))
if __name__ == "__main__":
# Create App with the design
LMS_App = QtWidgets.QApplication(sys.argv)
LMS_App.setStyle(QStyleFactory.create('Fusion'))
a = MDI_Window()
# Exit application when system is terminated
sys.exit(LMS_App.exec_())

You cannot implement the paintEvent like that, mostly because a paintEvent has to be called by Qt and for the specific widget that is being painted. The paint event must be implemented in the widget.
The easiest solution is to subclass the QMdiArea:
class MdiArea(QMdiArea):
def paintEvent(self, event):
# call the base implementation to draw the default colored background
super().paintEvent(event)
# create the painter *on the viewport*
painter = QPainter(self.viewport())
painter.drawPixmap(500, 500, 500, 500, QtGui.QPixmap("KDS_Main-Window.png"))
Note that:
you shoud remove the paintEvent for the main window, now;
as you can see, the painter is called on the viewport: this is mandatory for all QAbstractScrollArea subclasses;
QPainter instances used in a paintEvent should not be set as instance attributes (as you can see, I didn't use self.painter), as the painter must be destroyed at the end of the function, otherwise you'll face performance and drawing issues; you could theoretically avoid this issue by manually calling painter.end(), but, since a new QPainter instance is most likely going to be recreated very soon and very often, making it a persistent attribute each time has really no use.

Related

PyCharm giving cannot find reference and unexpected argument warnings when working with inherited classes from PyQt6 despite code working normally

I am trying to make a calculator app using PyQt6 and I wanted a QPushButton with fixed size to go into a grid layout. I was going to be using this button a lot so I created my own SquarePushButton class inheriting from QPushButton with an additional setMinimumSize and setMaximumSize already in the init function.
I simplified the code as much as possible while replicating the error which is why I am using a QGridLayout for just two widgets.
When I run it, the code works perfectly fine and as expected.
import sys
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import QWidget, QApplication, QPushButton, QLabel, QGridLayout
# class to have a button of fixed size
class SquarePushButton(QPushButton):
def __init__(self, text):
super().__init__()
self.setText(text)
self.setMinimumSize(QSize(50, 50))
self.setMaximumSize(QSize(50, 50))
# main window class
class MyWindow(QWidget):
def __init__(self):
super().__init__()
# create a text label
self.label = QLabel(self)
self.label.setText("Label")
# create a button
btn = SquarePushButton("Button")
btn.clicked.connect(self.buttonPressed)
# error: "Cannot find reference 'connect' in 'function'"
# add text label into layout
layout = QGridLayout()
layout.addWidget(self.label, 0, 0)
layout.addWidget(btn, 0, 1)
# sets the layout
self.setLayout(layout)
def buttonPressed(self):
self.label.setText("Pressed")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
Here it gives me an error saying "Cannot find reference 'connect' in 'function'"
However if I change how the QWidgets are imported I get a new error at a completely different location, this time the error occurs during layout.addWidget, where it says "unexpected argument" for the last two arguments.
Keep in mind everything still works, its just PyCharm IDE getting mad at me.
import sys
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import *
# class to have a button of fixed size
class SquarePushButton(QPushButton):
def __init__(self, text):
super().__init__()
self.setText(text)
self.setMinimumSize(QSize(50, 50))
self.setMaximumSize(QSize(50, 50))
# main window class
class MyWindow(QWidget):
def __init__(self):
super().__init__()
# create a text label
self.label = QLabel(self)
self.label.setText("Label")
# create a button
btn = SquarePushButton("Button")
btn.clicked.connect(self.buttonPressed)
# add text label into layout
layout = QGridLayout()
layout.addWidget(self.label, 0, 0)
layout.addWidget(btn, 0, 1)
# error: "unexpected argument"
# sets the layout
self.setLayout(layout)
def buttonPressed(self):
self.label.setText("Pressed")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
Note: It would be easiest to spot if you just copy and pasted the code into your PyCharm IDE because it highlights the supposed error.
I do not understand why there is an error there, everything seems to be working fine so is this a PyCharm bug, PyQt6 bug or am I just using inheritance incorrectly? I am quite new to Python still so sorry if this is really stupid.

How to make an image selectable using pixmap and Qlabel?

I am trying to show multiple images using Pyqt5. It would be nice to make the image selectable within the GUI so that the users can select and copy that image right away easily.
By "selectable", I meant the user can right click the image and then copy it and then potentially paste it to somewhere else outside of the GUI. Just like a normal image saved in a Word. User can select/copy an image in Word and then paste it to somewhere else.
I know for Text in Qlabel this can easily achieved by using self.my_label.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse). However, it seems for images there is no such method handling it. Is there any way I can work it out for images?
import sys
import PyQt5
from PyQt5.QtWidgets import (
QLabel,
QVBoxLayout,
QWidget
)
from PyQt5 import QtCore
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QSize
class Display_Window(QWidget):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(980,700))
self.layout = QVBoxLayout(self)
self.label1 = QLabel(self)
self.pixmap = QPixmap(path_to_my_image)
self.pixmap = self.pixmap.scaled(900, 900, QtCore.Qt.KeepAspectRatio)
self.label1.setPixmap(self.pixmap)
self.label1.resize(self.pixmap.width(), self.pixmap.height())
# Run if Script
if __name__ == "__main__":
app = PyQt5.QtWidgets.QApplication(sys.argv)
MainWindow = Display_Window() # Initialize GUI
MainWindow.show() # Show Window
app.exec_()
You can subclass the label and create a menu whenever it has a valid pixmap, then use the system clipboard to copy it.
class CopiableLabel(QLabel):
def contextMenuEvent(self, event):
pixmap = self.pixmap()
if not pixmap.isNull():
menu = QMenu()
copyAction = menu.addAction('Copy image to clipboard')
if menu.exec_(event.globalPos()) == copyAction:
QApplication.clipboard().setPixmap(pixmap)
return
super().contextMenuEvent(event)
class Display_Window(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout(self)
self.label1 = CopiableLabel(self)
self.layout.addWidget(self.label1)
self.pixmap = QPixmap(path_to_my_image)
self.pixmap = self.pixmap.scaled(900, 900, Qt.KeepAspectRatio)
self.label1.setPixmap(self.pixmap)
Note that setting a pixmap on a QLabel automatically resizes it (unless it has the scaledContents property set to True.
You also should add the label to the layout, as I did in the above modifications.

Widgets are not displayed

I'm currently doing a tutorial on how to create GUI apps from a book called "Modern PyQt" by Joshua Willman and I'm stuck right out of the gate:
I've copy pasted a basic code snippet from the book and tried to tweak it bit by bit instead of reading a bunch of text without any practical trial and error. I can display a window and adjust it's size and properties, but when I try to attach other widgets to the main window, they just don't show up.
Here's what I've got so far:
# First practical example from the book: Pomodoro time manager
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
class Pomodoro(QWidget):
def __init__(self): # Create default constructor
super().__init__()
self.initializeUI()
def initializeUI(self):
"""Initialize the window and display its contents to the screen."""
self.setGeometry(int((SCREEN_WIDTH-WINDOW_WIDTH)/2),int((SCREEN_HEIGHT-WINDOW_HEIGHT)/2),WINDOW_WIDTH,WINDOW_HEIGHT) # x,y,w,h
self.setWindowTitle('Bortism')
# self.setWindowIcon(QIcon("Borticon.png"))
self.button1 = QPushButton()
self.button1.setText('Button')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
WINDOW_WIDTH, WINDOW_HEIGHT = 1000,600
SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT = app.desktop().screenGeometry().getRect()
window = Pomodoro()
sys.exit(app.exec_())
For a widget to be shown as part of another then the requirement is that the first is the child of the second. This can be done by passing the parent or by using a layout (which is also passed to it by the parent). So in your case you must change to:
self.button1 = QPushButton(self)

Create pyside application in different shape than rectangle

I am building an application in Pyside2.
As we know when we use mainwindow for our application it comes with rectangle shape.
But I want user define shape of application.
For example check below image of Zoiper application.
The background is my editor with some text, You can easily feel the outer border of application.
Can we achieve the same thing using pyside2?
Thanks in advance.
Create a transparent widget. Two lines are important:
self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)
self.setAttribute(Qt.WA_TranslucentBackground)
Working example:
from PySide2.QtCore import Qt
from PySide2.QtGui import QColor, QPainterPath, QPainter
from PySide2.QtWidgets import QApplication, QWidget
class WCustomShape(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)
self.setFixedSize(400, 300)
self.setAttribute(Qt.WA_TranslucentBackground)
def paintEvent(self, e):
painter = QPainter(self)
path = QPainterPath()
path.addEllipse(200, 150, 100, 50)
painter.fillPath(path, QColor(Qt.blue))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
main_window = WCustomShape()
main_window.show()
main_window.raise_()
sys.exit(app.exec_())

Connecting to events of another widget

This is most likely a duplicate question, but I have to ask it because other answers aren't helping in my case, since I am new to pyqt (switched from tkinter few days ago).
I am wondering if is it possible to connect to an event of a widget like this:
self.lineEdit = QtGui.QLineEdit(self.frame)
self.lineEdit.keyReleaseEvent(lambda: someFunction(QtCore.Qt.Key_A ))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.horizontalLayout.addWidget(self.lineEdit)
and then...
def someFunction(event):
print(event)
...
My question is how to bind to a specific event from another widget, and connect that event with a function - like btn.clicked.connect(function_goes_here).
In tkinter it's something be like this:
self.Entry.bind("<KeyRelease-a>", lambda event: someFunction(event))
There are a number of different ways to achieve this. A generic way to listen to all events for a given widget, is to install an event-filter on it. All protected functions have a corresponding event type that can be accessed in this way:
class MainmWindow(QMainWindow):
def __init__(self):
...
self.lineEdit = QLineEdit(self.frame)
self.lineEdit.installEventFilter(self)
def eventFilter(self, source, event):
if source is self.lineEdit:
if event.type() == QEvent.KeyRelease:
print('key release:', event.key())
# the following line will eat the key event
# return True
return super(MainmWindow, self).eventFilter(source, event)
Alternatively, you can sub-class the widget, re-implement the relevant event handler, and emit a custom signal:
class LineEdit(QLineEdit):
keyReleased = pyqtSignal(int)
def keyReleaseEvent(self, event):
self.keyReleased.emit(event.key())
super(LineEdit, self).keyReleaseEvent(event)
class MainmWindow(QMainWindow):
def __init__(self):
...
self.lineEdit = LineEdit(self.frame)
self.lineEdit.keyReleased.connect(self.handleKeyRelease)
def handleKeyRelease(self, key):
print('key release:' key)
A more hackish variation on this is to overwrite the method directly:
class MainmWindow(QMainWindow):
def __init__(self):
...
self.lineEdit = QLineEdit(self.frame)
self.lineEdit.keyReleaseEvent = self.handleKeyRelease
def handleKeyRelease(self, event):
print('key release:', event.key())
QLineEdit.keyReleaseEvent(self.lineEdit, event)
Note that if you don't want to invoke the default event handling, you can omit the call to the base-class method.
I don't have enough reputation to reply to #ekhumoro, but wanted to add to their answer with an applied answer specific to MouseEvents for those who might benefit, based on a similar issue I was having.
Scenario
Display an image in the central widget, and connect a custom signal to capture mouse double click actions in the central widget.
Source for test image: https://en.wikipedia.org/wiki/Standard_test_image#/media/File:SIPI_Jelly_Beans_4.1.07.tiff
Consider the following:
A basic QMainWindow class which loads a supplied image_file and shows the image in a custom sub-class of the QLabel class, fit to the window:
import sys
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap, QImage, QPalette, QMouseEvent
from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QScrollArea)
class ImageView(QMainWindow):
def __init__(self):
super().__init__()
self.initializeUI()
self.image = QImage()
def initializeUI(self):
self.setMinimumSize(300, 200)
self.setWindowTitle("Image Viewer")
# self.showMaximized()
self.createMainQLabel()
self.show()
def createMainQLabel(self):
"""Create an instance of the imageQLabel class and set it
as the main window's central widget."""
self.image_qlabel = imageQLabel(self)
self.image_qlabel.resize(self.image_qlabel.pixmap().size())
self.scroll_area = QScrollArea()
self.scroll_area.setBackgroundRole(QPalette.Dark)
self.scroll_area.setAlignment(Qt.AlignCenter)
self.scroll_area.setWidget(self.image_qlabel)
self.setCentralWidget(self.scroll_area)
class imageQLabel(QLabel):
"""Subclass of QLabel for displaying image"""
def __init__(self, parent, image_file="images/SIPI_Jelly_Beans_4.1.07.tiff"):
super().__init__(parent)
self.openImage(image_file)
self.setScaledContents(True)
self.setAlignment(Qt.AlignCenter)
def openImage(self, image_file):
# Get image, create the pixmap and resize to fit window
self.image = QImage(image_file)
self.setPixmap(QPixmap().fromImage(self.image))
self.resize(self.pixmap().size())
"""
Other methods used to customize the class, for example:
- Resize
- Rotate
- Crop
- ...
...
"""
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_DontShowIconsInMenus, True)
window = ImageView()
sys.exit(app.exec_())
When executed, yield a simple window with the image:
SIPI Test Image shown in PyQT5 Window
What we would like to do is add a mouseDoubleClickEvent to the imageQLabel sub-class, but ensure that other widgets or classes in the program can access the event signals. To do this, you can modify #ekhumoro's answer #2 to accommodate a pyqtSignal.
First, add the signal to the attributes of the imageQLabel class. Here, we need the full QMouseEvent rather than just an int:
import sys
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap, QImage, QPalette, QMouseEvent
from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QScrollArea)
class imageQLabel(QLabel):
"""Subclass of QLabel for displaying image"""
# Class attributes
mouseDoubleClickSignal = pyqtSignal(QMouseEvent)
We wish to pass the event from that pyqtSignal to some other method in our ImageView main window class. As an example, let's make a method to print the cursor position when the user double clicks in the imageQLabel sub-class.
In ImageView:
class ImageView(QMainWindow):
"""
...
The rest of the class code
...
"""
def print_mouse(self, event):
print("print_mouse event from QMainWindow: {}".format((event.x(), event.y())))
Now we just need to connect print_mouse to the custom pyqtSignal we made. It makes the most sense to do this in the initializeUI method:
def initializeUI(self):
self.setMinimumSize(300, 200)
self.setWindowTitle("Image Viewer")
# self.showMaximized()
self.createMainQLabel()
# Connect the signal from custom imageQLabel class
# to the QMainWindow
self.image_qlabel.mouseDoubleClickSignal.connect(self.print_mouse)
self.show()
These modifications now print the cursor position when double clicking inside of the imageQLabel sub-class in the central widget. The call to print_mouse is outside of the sub-class. Moreover, double-clicking outside of the central widget (like outside of the frame of the image) does not call print_mouse because the mouseDoubleClickEvent is only active for the central widget.
%user profile%\AppData\Local\Programs\Python\Python39\python.exe
%script path%\stackoverflowsolution.py
print_mouse event from QMainWindow: (115, 140)
print_mouse event from QMainWindow: (54, 55)
override using python lambda magic
def on_key(line_edit : QLineEdit, key):
print(key)
...
line_edit = self._lineedit = QLineEdit()
line_edit.keyReleaseEvent = lambda ev, self=line_edit, super=line_edit.keyReleaseEvent: \
(super(ev), on_key(self, ev.key()), None)[-1]
We save current keyReleaseEvent function in lambda kwarg super.
Also we need to save line_edit in lambda kwarg self, because line_edit will be removed from locals during lambda execution.
Finally we need to return None, thus get it from last element from our Tuple.

Categories