Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过拖动角点以选择图像裁剪区域来调整QPaint drawRect的大小_Python_Pyqt_Pyqt5 - Fatal编程技术网

Python 通过拖动角点以选择图像裁剪区域来调整QPaint drawRect的大小

Python 通过拖动角点以选择图像裁剪区域来调整QPaint drawRect的大小,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我想要红色的矩形像这样调整大小 现在 我想保持图像的大小不变,但通过拖动红色矩形的角,同时显示图标来调整红色矩形的大小 我重新实现QLabel以重新绘制图像和矩形,但如何重新定位角落处的图标 import sys from PyQt5.QtCore import QPointF, QSize, QRectF, Qt from PyQt5.QtGui import QPixmap, QIcon, QPainter, QPen from PyQt5.QtWidgets import * cla

我想要红色的矩形像这样调整大小

现在

我想保持图像的大小不变,但通过拖动红色矩形的角,同时显示图标来调整红色矩形的大小

我重新实现QLabel以重新绘制图像和矩形,但如何重新定位角落处的图标

import sys
from PyQt5.QtCore import QPointF, QSize, QRectF, Qt
from PyQt5.QtGui import QPixmap, QIcon, QPainter, QPen
from PyQt5.QtWidgets import *

class CropLabel(QLabel):
    def __init__(self, parent=None):
        super(CropLabel, self).__init__(parent)
        self.img = None
        self.left_top = QPointF(0, 0)
        self.bot_right = QPointF(0, 0)
        self.left_top_icon = QPixmap('images/lefttop-botright.png').scaled(QSize(25, 25))

    def setPixmap(self, QPixmap):
        self.img = QPixmap
        h, w = QPixmap.height(), QPixmap.width()
        self.bot_right = QPointF(self.left_top.x() + w, self.left_top.y() + h)
        self.resize(QSize(w, h))


    def paintEvent(self, QPaintEvent):
        if self.img is not None:
            painter = QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing)
            painter.drawPixmap(self.left_top, self.img)
            painter.drawPixmap(self.left_top.x() - self.left_top_icon.width() / 2,
                               self.left_top.y() - self.left_top_icon.height() / 2,
                               self.left_top_icon)

            paintRect = QPen(Qt.red)
            paintRect.setWidth(3)
            painter.setPen(paintRect)
            painter.drawRect(QRectF(self.left_top, self.bot_right))

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.label = CropLabel(self)
        self.board_width = 100
        qPixmap = QPixmap('images/cat.jpg')
        self.resize(QSize(qPixmap.width() + self.board_width * 2, qPixmap.height() + self.board_width * 2))
        self.label.left_top = QPointF   (self.board_width, self.board_width)
        self.label.setPixmap(qPixmap)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

您需要更新光标位置,然后更新小部件以重新绘制它

QLabel与大多数小部件一样,通常仅在单击鼠标按钮时才会接收MouseMoveEvents,但如果要实现
鼠标追踪
(每次鼠标移动到小部件上时都会发出该事件),则可能需要对其进行检查


请注意,整个方法非常基本(例如,即使用户在“箭头”光标外单击,也会发生调整大小的操作),不建议使用这种交互来实现您可能要实现的目标。您可能想看看,这需要一些理解和研究,但对于这类事情更推荐。

只有当用户单击“图形视图框架”在“调整大小模式”下显示的“角”(特定位置)和“箭头”(任何图标)时,才能进行调整大小吗?Jiburiru它可以以任何方式发生,这取决于您如何重新实现它。如果您真的打算使用图形视图,我建议您仔细阅读文档,查找一些教程,并进行大量实验。
class CropLabel(QLabel):
    # ...
    def mouseMoveEvent(self, event):
        # update the mouse position and refresh the contents, only if the a
        # mouse button is pressed
        if event.buttons():
            self.left_top = event.pos()
            self.update()

    # do NOT use "QPaintEvent" as argument name, as it's an object class name
    def paintEvent(self, event):
        if self.img is not None:
            rect = QRectF(self.left_top, self.bot_right)
            painter = QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing)
            # save the painter state
            painter.save()
            # set a clip rectangle (the image will not be drawn outside)
            painter.setClipRect(rect)
            painter.drawPixmap(self.left_top, self.img)
            # restore the previous painter state
            painter.restore()
            painter.drawPixmap(self.left_top.x() - self.left_top_icon.width() / 2,
                               self.left_top.y() - self.left_top_icon.height() / 2,
                               self.left_top_icon)

            paintRect = QPen(Qt.red)
            paintRect.setWidth(3)
            painter.setPen(paintRect)
            painter.drawRect(rect)


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        # set a layout and add the label to it, otherwise it will have a
        # fixed size which will probably be smaller than the window
        layout = QVBoxLayout(self)
        self.label = CropLabel(self)
        layout.addWidget(self.label)
        # ...