<small id='jmyKN'></small><noframes id='jmyKN'>

    1. <i id='jmyKN'><tr id='jmyKN'><dt id='jmyKN'><q id='jmyKN'><span id='jmyKN'><b id='jmyKN'><form id='jmyKN'><ins id='jmyKN'></ins><ul id='jmyKN'></ul><sub id='jmyKN'></sub></form><legend id='jmyKN'></legend><bdo id='jmyKN'><pre id='jmyKN'><center id='jmyKN'></center></pre></bdo></b><th id='jmyKN'></th></span></q></dt></tr></i><div id='jmyKN'><tfoot id='jmyKN'></tfoot><dl id='jmyKN'><fieldset id='jmyKN'></fieldset></dl></div>

      <legend id='jmyKN'><style id='jmyKN'><dir id='jmyKN'><q id='jmyKN'></q></dir></style></legend>

        <bdo id='jmyKN'></bdo><ul id='jmyKN'></ul>
        <tfoot id='jmyKN'></tfoot>

      1. 在 QWidget 上的 QPixmap 上绘制点(pyqt5)

        时间:2023-08-05
            <legend id='Dz7mP'><style id='Dz7mP'><dir id='Dz7mP'><q id='Dz7mP'></q></dir></style></legend>
            • <i id='Dz7mP'><tr id='Dz7mP'><dt id='Dz7mP'><q id='Dz7mP'><span id='Dz7mP'><b id='Dz7mP'><form id='Dz7mP'><ins id='Dz7mP'></ins><ul id='Dz7mP'></ul><sub id='Dz7mP'></sub></form><legend id='Dz7mP'></legend><bdo id='Dz7mP'><pre id='Dz7mP'><center id='Dz7mP'></center></pre></bdo></b><th id='Dz7mP'></th></span></q></dt></tr></i><div id='Dz7mP'><tfoot id='Dz7mP'></tfoot><dl id='Dz7mP'><fieldset id='Dz7mP'></fieldset></dl></div>
                <tbody id='Dz7mP'></tbody>

              • <bdo id='Dz7mP'></bdo><ul id='Dz7mP'></ul>

                  <tfoot id='Dz7mP'></tfoot>

                1. <small id='Dz7mP'></small><noframes id='Dz7mP'>

                  本文介绍了在 QWidget 上的 QPixmap 上绘制点(pyqt5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个带有 QLayout 的 QWidget,上面有一个 QLabel.我在标签上设置了一个 QPixmap.无论用户单击图像的何处,我都想画一个点.我定义了 mouseReleaseEvent(有效)和paintEvent(但没有绘制点).我已经阅读了所有类似的问题,但没有一个解决方案对我有用.有什么帮助吗?我的相关代码:

                  I have a QWidget with a QLayout on which there is a QLabel. I set a QPixmap on the label. Wherever the user clicks on the image, I want to draw a point. I defined mouseReleaseEvent (which works) and paintEvent (but no points are drawn). I've read all similar questions and none of the solutions worked for me. Any help? My relevant code:

                  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() 方法:

                  When you use QtGui.QCursor.pos() is getting the coordinates of the cursor with respect to the screen, but when you want to paint a widget you must be in the coordinates of the widget, for it the widget has the mapToGlobal() method:

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

                  但是在这种情况下还有另一种解决方案,您必须使用返回mouseReleaseEvent的事件,该事件具有pos()方法中的信息:

                  But in this case there is another solution, you must use the event that returns mouseReleaseEvent that has the information in the pos() method:

                  cursor_event.pos()
                  

                  另一个问题是你创建的标签在小部件上方所以你看不到点,最简单的方法是直接用drawPixmap()QPixmap> 方法.

                  Another problem is that the label you created is above the widget so you do not see the points, the easiest thing is to draw the QPixmap directly with the drawPixmap() method.

                  完整代码:

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

                  这篇关于在 QWidget 上的 QPixmap 上绘制点(pyqt5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PyQt5 QObject:无法为不同线程中的父级创建子级 下一篇:有什么方法可以在浏览器中获取 Pyqt5 中页面的

                  相关文章

                  最新文章

                  <small id='sk5kK'></small><noframes id='sk5kK'>

                    <bdo id='sk5kK'></bdo><ul id='sk5kK'></ul>

                    1. <i id='sk5kK'><tr id='sk5kK'><dt id='sk5kK'><q id='sk5kK'><span id='sk5kK'><b id='sk5kK'><form id='sk5kK'><ins id='sk5kK'></ins><ul id='sk5kK'></ul><sub id='sk5kK'></sub></form><legend id='sk5kK'></legend><bdo id='sk5kK'><pre id='sk5kK'><center id='sk5kK'></center></pre></bdo></b><th id='sk5kK'></th></span></q></dt></tr></i><div id='sk5kK'><tfoot id='sk5kK'></tfoot><dl id='sk5kK'><fieldset id='sk5kK'></fieldset></dl></div>
                    2. <tfoot id='sk5kK'></tfoot>
                    3. <legend id='sk5kK'><style id='sk5kK'><dir id='sk5kK'><q id='sk5kK'></q></dir></style></legend>