PyQt 5 : QRect 在窗口上的显示错误

问题描述 投票:0回答:0

大家。 我正在开发一个 GUI 应用程序,我在通过图像内的鼠标跟踪构建 QRect 时遇到问题。 The issue at hand

实际上,我想使用 mouseMoveEvent 在窗口上绘制一个矩形。

我让我的代码包含以下功能:

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Pyobs Interface')
        self.zone_plot,self.sub_title = Window_funcs.create_my_windows(self), Window_funcs.interface_title(self, 'Orbital Choice')

        self.widget , self.widget2 = Window_funcs.create_checkbox(self, 'LEO', 120, 75, self.get_choose) , Window_funcs.create_checkbox(self, 'MEO', 120, 75, self.get_choose)

        self.widget3, self.widget4 = Window_funcs.create_checkbox(self, 'GEO', 120, 75, self.get_choose), Window_funcs.create_checkbox(self, 'SURVEY', 120, 75, self.get_choose)

        self.reg_box = Window_funcs.region_box(self, [self.widget, self.widget2, self.widget3, self.widget4], 'Region Choices')

        self.zone_plot = Window_funcs.add_init_box(self, self.zone_plot, self.sub_title, self.reg_box)

        self.zone_plot.setRowStretch(self.zone_plot.rowCount(), 1)
        self.zone_plot.setColumnStretch(self.zone_plot.columnCount(), 1)

        self.im_w,self.im_h = 950,620

        self.begin,self.dest = QPoint(),QPoint()



        self.setLayout(self.zone_plot)

        self.flag = False

        self.show()


    def paintEvent(self,event):
        super().paintEvent(event)
        if not self.flag:
            return None

        qpaint = QPainter(self.image.pixmap())

        qpaint.setPen(QtGui.QColor(200,0,0))

        qpaint.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform,True)
        if not self.begin.isNull() and not self.dest.isNull():
            rect = QRect(self.begin,self.dest).normalized()
            qpaint.drawRect(rect)
            qpaint.setCompositionMode(QPainter.CompositionMode_Clear)

        qpaint.end()
        del qpaint

        self.image.setPixmap(self.image.pixmap())

        self.image.show()

    def press_zone(self,event):
        """
        Parameters
        -------
        event:
                QMouseEvent
        """
        self.flag = True
        if event.buttons() & Qt.LeftButton:
            self.start_x,self.start_y = event.x(),event.y()
            self.begin = event.pos()
            self.dest = self.begin

        self.update()
        super().mousePressEvent(event)

    def track_zone(self, event):
        if event.buttons() & Qt.LeftButton and self.flag :
            self.dest =  event.pos()
        self.update()
        super().mouseMoveEvent(event)


    def release_zone(self,event):
        """
        Parameters
        -------
        event:
                QMouseEvent
        """
        if event.buttons() & Qt.LeftButton:
            rect = QRect(self.begin,self.dest).normalized()
            qpaint = QPainter(self.image.pixmap())
            qpaint.drawRect(rect)
            self.begin, self.dest = QPoint(),QPoint()



        self.end_x,self.end_y = event.x(),event.y()
        k_x,k_y = self.I.shape[1]/self.image.width() , self.I.shape[0]/self.image.height() # Facteur d'échelle

        self.I,self.zoom_coord = im_process.zoom_proc(self.I, k_x, k_y,self.start_x,self.end_x,self.start_y,self.end_y)


        if self.I.size < 2 :
            self.z_error = Window_funcs.msg_error(self.zoom, 'Not possible to zoom')
            self.reset()
        else:
            self.set(self.I)

        self.update()
        self.flag = False
        super().mouseReleaseEvent(event)


在此先感谢您的帮助

我开始在窗口本身上绘图,但它不起作用。 我也尝试使用真实坐标而不是 QPoint 对象,但我检索到相同的效果

变量

self.image
是之前并行定义并添加到 QVBoxLayout 并进一步添加到 QGridLayout 的 QLabel,如:

示例创建

self.image = QPixmap('D: lpha_stars.png')

self.center_layout = QVBoxLayout()

self.center_layout.addWidget(self.image)

self.zone_plot = QGridLayout() self.zone_plot.addLayout(self.center_layout,1,1,alignment=Qt.AlignAbsolute)

python user-interface pyqt pyqt5
© www.soinside.com 2019 - 2024. All rights reserved.