Python Forum
[PyQt] QT5 Designer Drawing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QT5 Designer Drawing
#1
Hi all. I'm having trouble implementing a drawing box in QT5 Designer. This code as an example, creates the window dynamically and uses QPainter and QImage, but there's no QImage widget to drag and drop in QT5 designer I am aware of. I read a few examples with Qlabel and pixmap but this code below looked much cleaner and I was having trouble with inheritance and methods to clear the image.

I have a label on my ui named "lbldrawing", and I would like to know how do I set the QImage to be inside (if that's the correct term) the label container widget?

I would rather use a static widget in the ui than positioning it in code. Any help would be great.

Here's the code (shortened vers) from https://www.geeksforgeeks.org/pyqt5-crea...plication/

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Paint with PyQt5 - nice example of painting with python")
        self.setGeometry(500, 100, 800, 600)
        self.image = QImage(self.size(), QImage.Format_RGB32)
        self.image.fill(Qt.white)
        self.drawing = False
        self.brushSize = 2
        self.brushColor = Qt.black
        self.lastPoint = QPoint()
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu("File")
        saveAction = QAction("Save", self)
        saveAction.setShortcut("Ctrl + S")
        fileMenu.addAction(saveAction)
        saveAction.triggered.connect(self.save)
        clearAction = QAction("Clear", self)
        fileMenu.addAction(clearAction)
        clearAction.triggered.connect(self.clear)
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.lastPoint = event.pos()
    def mouseMoveEvent(self, event):
        if (event.buttons() & Qt.LeftButton) & self.drawing:
            painter = QPainter(self.image)
            painter.setPen(QPen(self.brushColor, self.brushSize,
                            Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
            painter.drawLine(self.lastPoint, event.pos())
            self.lastPoint = event.pos()
            self.update()
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drawing = False
    def paintEvent(self, event):
        canvasPainter = QPainter(self)
        canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
    def save(self):
        filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
                          "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
 
        if filePath == "":
            return
        self.image.save(filePath)
    def clear(self):
        self.image.fill(Qt.white)
        self.update()
App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec())
Reply
#2
You can add widgets to QtDesigner.

https://doc.qt.io/qt-5/designer-creating...dgets.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QT Designer Krissstian 4 1,722 May-26-2022, 09:45 AM
Last Post: Krissstian
  Qt Designer : help document viewer error. davediamond 2 1,610 Apr-14-2022, 10:38 AM
Last Post: davediamond
  GUI programming PyQt 5 + Qt Designer flash77 9 2,753 Mar-19-2022, 10:31 AM
Last Post: flash77
  Installed Designer - Helpf Files for "assistant" are missing Valmont 0 2,011 Mar-22-2021, 11:09 AM
Last Post: Valmont
  Using a GUI Designer vs. hard coding 357mag 9 6,111 Feb-21-2021, 06:43 PM
Last Post: kkaur
  Attempting to use Qt Designer. thewolf 17 6,162 Feb-17-2021, 12:03 AM
Last Post: thewolf
  [PyGUI] help code python QT Designer yan_mhb 0 1,932 Aug-12-2020, 09:32 AM
Last Post: yan_mhb
  [Kivy] Kivy Designer Module Error SARAVANAN_M 0 3,876 Nov-20-2019, 09:57 AM
Last Post: SARAVANAN_M
  [PyQt] Send data between windows Pyqt5 and Qt Designer kkonrad002 8 10,219 Sep-05-2019, 02:25 PM
Last Post: Denni
  [PyQt] Qt Designer - Making a Font with a border jimmyvegas29 4 7,567 Feb-19-2019, 11:08 PM
Last Post: jimmyvegas29

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020