PySide(PyQt)在图像上画线

1、按鼠标左键任意画线

python 复制代码
import sys
from PySide6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PySide6.QtGui import QPainter, QPixmap, QMouseEvent, QColor, QPen
from PySide6.QtCore import Qt, QPoint


class PaintLabel(QLabel):
    def __init__(self, parent=None):
        super(PaintLabel, self).__init__(parent)
        self.setFixedSize(500, 500)
        self.pixmap = QPixmap(500, 500)  # 创建图像
        self.pixmap.fill(QColor('#000000'))  # 设为黑色
        self.setPixmap(self.pixmap)  # 设置图像
        self.drawing = False
        self.last_point = QPoint()

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.last_point = event.position()

    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() & Qt.LeftButton and self.drawing:
            painter = QPainter(self.pixmap)
            pen = QPen(QColor('red'))
            pen.setWidth(3)
            painter.setPen(pen)
            painter.drawLine(self.last_point, event.position())
            self.last_point = event.position()
            self.setPixmap(self.pixmap)

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = False


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.label = PaintLabel()

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        # layout.addWidget(save_button)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())

2、画直线

python 复制代码
import sys
from PySide6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PySide6.QtGui import QPainter, QPixmap, QMouseEvent, QColor, QPen
from PySide6.QtCore import Qt, QPoint, QLine


class PaintLabel(QLabel):

    def __init__(self, parent=None):
        super(PaintLabel, self).__init__(parent)

        self.line = QLine()
        self.setFixedSize(500, 500)
        self.pixmap = QPixmap(500, 500)  # 创建图像
        self.pixmap.fill(QColor('#000000'))  # 设为黑色
        self.setPixmap(self.pixmap)  # 设置图像
        self.drawing = False         # 是否绘画中
        self.start_pos = QPoint()    # 起点
        self.end_pos = QPoint()      # 终点

    def mousePressEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.start_pos = event.position().toPoint()

    def mouseMoveEvent(self, event: QMouseEvent):
        if event.buttons() & Qt.LeftButton and self.drawing:
            self.end_pos = event.position().toPoint()
            self.line = QLine(self.start_pos, self.end_pos)
            self.update()

    def mouseReleaseEvent(self, event: QMouseEvent):
        if event.button() == Qt.LeftButton:
            self.drawing = False
            self.update()

    def paintEvent(self, event):
        super().paintEvent(event)  # 继承绘画事件
        if self.drawing:  # 如果正在绘画中
            painter = QPainter(self)  # 就不必刷新和写入,只是在当前的label上展示一下新增的线条而已
            pen = QPen(QColor(0, 255, 0), 2, Qt.CustomDashLine)   # 虚线
            pen.setDashPattern([1, 4, 5, 4])  # 设置自定义虚线模式
            painter.setPen(pen)
            painter.drawLine(self.line)
            painter.end()
        else:
            painter = QPainter(self.pixmap)  # 如果是松开鼠标的最后结果,就在图像上画
            painter.setPen(QPen(QColor(255, 0, 0, 255), 2, Qt.SolidLine))
            painter.drawLine(self.line)
            self.line = QLine()   # 画完后舍弃

            self.setPixmap(self.pixmap)    # 刷新显示


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.label = PaintLabel()

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())
相关推荐
蹦蹦跳跳真可爱5894 天前
Python----PyQt开发(PyQt高级:手搓一个音乐播放器)
python·pyqt
mosquito_lover19 天前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt
薛尧笔记13 天前
关于FSGithubPNG生成外链时描述出现路径问题
笔记·python·github·pyqt·桌面应用
蹦蹦跳跳真可爱58913 天前
Python----PyQt开发(PyQt高级:手搓一个简单的记事本)
python·pyqt
深蓝海拓14 天前
PySide(PyQT)使用场景(QGraphicsScene)进行动态标注的一个demo
开发语言·python·pyqt
#岩王爷15 天前
cv2小练习
音视频·pyqt
不知名君15 天前
PyQt组态软件 拖拽设计界面测试
python·pyqt·qt5
XS3015 天前
PyQt学习记录03——批量设置水印
学习·pyqt
蹦蹦跳跳真可爱58915 天前
Python----PyQt开发(PyQt基础,环境搭建,Pycharm中PyQttools工具配置,第一个PyQt程序)
开发语言·python·pyqt
蹦蹦跳跳真可爱58916 天前
Python----PyQt开发(PyQt高级:组件大小,界面位置,按钮,文本显示,文本输入,字体大小)
python·pyqt