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

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

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

        如何在 PyQt5 中通过拖放来绘制矩形并调整其形状

        时间:2023-08-05

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

            <tbody id='HEfzL'></tbody>
          <tfoot id='HEfzL'></tfoot>

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

              • <small id='HEfzL'></small><noframes id='HEfzL'>

                  <bdo id='HEfzL'></bdo><ul id='HEfzL'></ul>
                • 本文介绍了如何在 PyQt5 中通过拖放来绘制矩形并调整其形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试通过拖放在 PyQt5 创建的 GUI 上绘制一个矩形.我设法做到了,但是释放鼠标左键时会绘制矩形.

                  I'm trying to draw a rectangle on GUI created by PyQt5 by drag and drop. I managed to do that, but the rectangle is drawn when the mouse left key is released.

                  我想做的就是这样link:

                  • 按下鼠标左键时,开始绘制矩形.
                  • 拖动时,通过鼠标移动调整矩形形状.
                  • 松开鼠标左键后,确定矩形形状.

                  我该如何实现呢?提前致谢.

                  How can I implement this? Thanks in advance.

                  这是我的代码.

                  # -*- coding: utf-8 -*-
                  
                  import sys
                  from PyQt5 import QtWidgets, QtCore
                  from PyQt5.QtGui import QPainter
                  
                  class MyWidget(QtWidgets.QWidget):
                      def __init__(self):
                          super().__init__()
                          self.setGeometry(30,30,600,400)
                          self.pos1 = [0,0]
                          self.pos2 = [0,0]
                          self.show()
                  
                      def paintEvent(self, event):
                          width = self.pos2[0]-self.pos1[0]
                          height = self.pos2[1] - self.pos1[1]     
                  
                          qp = QPainter()
                          qp.begin(self)           
                          qp.drawRect(self.pos1[0], self.pos1[1], width, height)        
                          qp.end()
                  
                      def mousePressEvent(self, event):
                          self.pos1[0], self.pos1[1] = event.pos().x(), event.pos().y()
                          print("clicked")
                  
                      def mouseReleaseEvent(self, event):
                          self.pos2[0], self.pos2[1] = event.pos().x(), event.pos().y()
                          print("released")
                          self.update()
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      window = MyWidget()
                      window.show()
                      app.aboutToQuit.connect(app.deleteLater)
                      sys.exit(app.exec_())
                  

                  推荐答案

                  你不必使用mouseReleaseEvent函数,而是每次调用的mouseMoveEvent函数鼠标移动了,我修改了代码让它更简单.

                  You do not have to use the mouseReleaseEvent function, but the mouseMoveEvent function that is called each time the mouse is moved, and I have modified the code to make it simpler.

                  class MyWidget(QtWidgets.QWidget):
                      def __init__(self):
                          super().__init__()
                          self.setGeometry(30,30,600,400)
                          self.begin = QtCore.QPoint()
                          self.end = QtCore.QPoint()
                          self.show()
                  
                      def paintEvent(self, event):
                          qp = QtGui.QPainter(self)
                          br = QtGui.QBrush(QtGui.QColor(100, 10, 10, 40))  
                          qp.setBrush(br)   
                          qp.drawRect(QtCore.QRect(self.begin, self.end))       
                  
                      def mousePressEvent(self, event):
                          self.begin = event.pos()
                          self.end = event.pos()
                          self.update()
                  
                      def mouseMoveEvent(self, event):
                          self.end = event.pos()
                          self.update()
                  
                      def mouseReleaseEvent(self, event):
                          self.begin = event.pos()
                          self.end = event.pos()
                          self.update()
                  

                  这篇关于如何在 PyQt5 中通过拖放来绘制矩形并调整其形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 PyQt/PySide 中将项目添加到 QComboBox 下一篇:macOS Big Sur 11.0.1 上未弹出应用程序

                  相关文章

                  最新文章

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

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