乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       印度阿三17 2019-10-09

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

      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()
      

      解決方法:

      當您使用QtGui.QCursor.pos()獲取光標相對于屏幕的坐標時,但是當您想要繪制小部件時,您必須位于小部件的坐標中,因為小部件具有mapToGlobal()方法:

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

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

      cursor_event.pos()
      

      另一個問題是您創(chuàng)建的標簽位于小部件上方,因此您看不到這些點,最簡單的方法是使用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

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多