日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

python – QWidget上的QPixmap上的繪圖點(diǎn)(pyqt5)

 印度阿三17 2019-10-09

我有一個(gè)帶QLayout的QWidget,其上有一個(gè)QLabel.
我在標(biāo)簽上設(shè)置了一個(gè)QPixmap.無論用戶點(diǎn)擊圖像,我想繪制一個(gè)點(diǎn).我定義了mouseReleaseEvent(可以工作)和paintEvent(但是沒有繪制點(diǎn)).我已經(jīng)閱讀了所有類似的問題,但沒有一個(gè)解決方案適合我.有幫助嗎?我的相關(guān)代碼:

class ImageScroller(QtWidgets.QWidget):

    def __init__(self, img):
        QtWidgets.QWidget.__init__(self)
        main_layout = QtWidgets.QVBoxLayout()
        self._image_label = QtWidgets.QLabel()
        self._set_image(img)
        main_layout.addWidget(self._image_label)
        main_layout.addStretch()
        self.setLayout(main_layout)

    def _set_image(self, img):
        img = qimage2ndarray.array2qimage(img)
        qimg = QtGui.QPixmap.fromImage(img)
        self._img_pixmap = QtGui.QPixmap(qimg)
        self._image_label.show()

    def paintEvent(self, paint_event):
        painter = QtGui.QPainter(self)
        painter.begin(self)
        painter.setPen(QtGui.QPen(QtCore.Qt.red))
        pen = QtGui.QPen()
        pen.setWidth(20)
        painter.setPen(pen)
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        painter.drawPoint(300,300)
        painter.drawLine(100, 100, 400, 400)
        for pos in self.chosen_points:
            painter.drawPoint(pos)
        painter.end()

    def mouseReleaseEvent(self, cursor_event):
        self.chosen_points.append(QtGui.QCursor().pos())
        self.update()

解決方法:

當(dāng)您使用QtGui.QCursor.pos()獲取光標(biāo)相對(duì)于屏幕的坐標(biāo)時(shí),但是當(dāng)您想要繪制小部件時(shí),您必須位于小部件的坐標(biāo)中,因?yàn)樾〔考哂衜apToGlobal()方法:

self.mapFromGlobal(QtGui.QCursor.pos())

但在這種情況下還有另一種解決方案,您必須使用返回具有pos()方法中信息的mouseReleaseEvent的事件:

cursor_event.pos()

另一個(gè)問題是您創(chuàng)建的標(biāo)簽位于小部件上方,因此您看不到這些點(diǎn),最簡(jiǎn)單的方法是使用drawPixmap()方法直接繪制QPixmap.

完整代碼:

from PyQt5 import QtWidgets, QtGui, QtCore


class ImageScroller(QtWidgets.QWidget):
    def __init__(self):
        self.chosen_points = []
        QtWidgets.QWidget.__init__(self)
        self._image = QtGui.QPixmap("image.png")

    def paintEvent(self, paint_event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(self.rect(), self._image)
        pen = QtGui.QPen()
        pen.setWidth(20)
        painter.setPen(pen)
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        painter.drawPoint(300, 300)
        painter.drawLine(100, 100, 400, 400)
        for pos in self.chosen_points:
            painter.drawPoint(pos)

    def mouseReleaseEvent(self, cursor_event):
        self.chosen_points.append(cursor_event.pos())
        # self.chosen_points.append(self.mapFromGlobal(QtGui.QCursor.pos()))
        self.update()


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = ImageScroller()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

enter image description here

來源:https://www./content-1-495401.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多