如何在单击按钮时显示文本标签以及绘制圆圈 "ADD"

how to display a textlabel as well as draw a circle on clicking a pushbutton "ADD"

我已经阅读了所有关于一个信号的多个插槽的相关文章,但是在绘制圆圈时无法显示,这两个按钮都是由一个按钮触发的"ADD"。我可以在单击按钮之前在圆圈附近显示文本标签,但我希望它仅在单击按钮后显示。请帮忙。另外,我希望文本标签靠近圆圈,并且可以随时点击修改

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow,QPushButton,QWidget
from PyQt5 import QtGui
from PyQt5.QtCore import QRect,Qt
from PyQt5.QtGui import QPainter,QBrush, QPen
from PyQt5 import QtCore


class Window(QMainWindow):
   def __init__(self):
        super(Window,self).__init__()
        title="layout management"
        left=500
        top=200
        width=500
        height=400
        iconName="fosseeicon.jpg"
        self.setWindowTitle(title)
        self.setWindowIcon(QtGui.QIcon(iconName))
        self.setGeometry(left, top, width, height)
        self.should_paint_circle = False
        self.windowcomponents()
        self.initUI()
        self.show()
   def initUI(self):
       if self.should_paint_circle:
           self.label=QtWidgets.QLabel(self)
           self.label.setText('<h2>circle<h2>')
   def windowcomponents(self):
       button=QPushButton("Add", self)
       button.setGeometry(QRect(0, 0, 50, 28))
       button.setIcon(QtGui.QIcon("addbutton.png"))
       button.setToolTip("<h3>This is for creating random circles<h3>")
       button.clicked.connect(self.paintcircle)
       button=QPushButton("Generate Report", self)
       button.setGeometry(QRect(49,0,150,28))
       button.setIcon(QtGui.QIcon("generatereport.png"))
       button.setToolTip("This is for generating pdf report of connection between two circles")
       button=QPushButton("Save", self)
       button.setGeometry(QRect(199,0,120,28))
       button.setIcon(QtGui.QIcon("saveicon.png"))
       button.setToolTip("This is for saving an image of canvas area")

   def paintEvent(self, event):
       super().paintEvent(event)
       if self.should_paint_circle:
           painter = QtGui.QPainter(self)
           painter.setRenderHint(QPainter.Antialiasing)
           painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
           painter.drawEllipse(100, 100, 100, 100)
           self.initUI()
           self.label.move(60,100)
   def paintcircle(self, painter):
       self.should_paint_circle = True
       self.update()
app = QApplication(sys.argv)
circle=Window()
circle.show()
sys.exit(app.exec_())

使用 parent 创建的小部件,在它们的 __init__(或它们的 parent 之外),但 没有 添加到布局,必须明确显示;你错过了这个:

    self.label.show()

除此之外,您不得在 paintEvent 中创建新的小部件。

画画是经常发生的事情,一般有以下几种情况(经常发生

  • 第一次显示小部件时
  • 每当小部件被隐藏并再次显示时(例如,在最小化和恢复 window 之后)
  • 每当鼠标进入或退出它时 and/or 它的 children
  • 当小部件或其任何 parent 调整大小时
  • 当显示新的 children 时

结果是,如果您为每个绘画事件添加一个小部件,您最终可能会得到几十个(如果不是数百或数千个)小部件,而且最重要的是,如果您还显示它,它会导致无限递归。

class Window(QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        title="layout management"
        left=500
        top=200
        width=500
        height=400
        iconName="fosseeicon.jpg"
        self.setWindowTitle(title)
        self.setWindowIcon(QtGui.QIcon(iconName))
        self.setGeometry(left, top, width, height)
        self.should_paint_circle = False
        self.windowcomponents()
        self.label = QtWidgets.QLabel(self)
        self.label.hide()

    # ...

    def paintEvent(self, event):
        super().paintEvent(event)
        if self.should_paint_circle:
            painter = QtGui.QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing)
            painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
            painter.drawEllipse(100, 100, 100, 100)

    def paintcircle(self, painter):
        self.should_paint_circle = True
        self.label.setText('<h2>circle<h2>')
        self.label.move(60,100)
        self.label.show()
        self.update()

也就是说,基于这个问题和上一个问题,我建议您更仔细地研究文档,尤其是与 QMainWindow, the Layout management, painting in Qt and the related QPainter 文档相关的内容。