PyQt5:通过单击QPushButton画一条线 [英] PyQt5: Draw a line by clicking QPushButton

查看:78
本文介绍了PyQt5:通过单击QPushButton画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到这一点,以便当我单击QPushButton时画一条线.但是,我现在拥有的代码在启动代码时在开始而不是在之后.QPushButton似乎没有做任何绘制.

I am trying to make it such that when I click a QPushButton a line is drawn. However the code I have right now makes the line at the beginning when the code is initiated and not after. The QPushButton doesn't seem to do any drawing.

我也不太明白为什么在绘图时需要在函数中使用事件"参数.

I also don't quite understand why when drawing you need an 'event' argument in the function.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QGridLayout,QPushButton, QApplication, QWidget
from PyQt5.QtCore import QSize, QCoreApplication, Qt
from PyQt5.QtGui import QPainter, QPen

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(300, 300)) 

        pybutton = QPushButton('button', self)
        pybutton.clicked.connect(self.paintEvent)
        pybutton.resize(100,100)
        pybutton.move(100, 100) 

    def paintEvent(self,event):
        print('click')
        painter = QPainter(self)
        pen = QPen(Qt.red, 3)
        painter.setPen(pen)
        painter.drawLine(0,0,100,100)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

推荐答案

您不应直接调用 paintEvent 方法,此方法应由Qt处理,因为除了您要GUI所需的内容之外,在其他情况下重新绘制它,例如在调整窗口小部件的大小,移动等时.接收到 paintEvent 的事件是一个 QPaintEvent 返回一个需要重新绘制的矩形,这是优化重绘,有时很简单,因为在这种情况下,没有必要使用它.

You should not call the paintEvent method directly, this should be handled by Qt because in addition to what you want the GUI needs to repaint it on other occasions like when the widget is resized, moved, etc. the event that receives paintEvent is a QPaintEvent returns a rectangle where it is required to repaint, this is to optimize the redrawing, sometimes simple as in this case it is not necessary to use it.

paintEvent 方法中,您必须在不为空的情况下绘制该线,因此,在连接到单击信号的插槽中应该执行的操作是用有效的那条线替换该空的线.,并使用 update()方法强制调用 paintEvent ,该方法通知GUI需要重新绘制.

In the paintEvent method you must draw the line when it is not null, so what you should do in the slot that connects to the clicked signal, is to replace that null line with a valid one, and force paintEvent to be called using the update() method that notifies the GUI that it needs to be repainted.

import sys
from PyQt5.QtWidgets import QMainWindow,QPushButton, QApplication
from PyQt5.QtCore import QSize, Qt, QLine, QPoint
from PyQt5.QtGui import QPainter, QPen

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(300, 300)) 

        pybutton = QPushButton('button', self)
        pybutton.clicked.connect(self.draw_line)
        pybutton.resize(100,100)
        pybutton.move(100, 100) 
        self.line = QLine()

    def draw_line(self):
        button = self.sender()
        self.line = QLine(QPoint(), button.pos())
        self.update()

    def paintEvent(self,event):
        QMainWindow.paintEvent(self, event)
        if not self.line.isNull():
            painter = QPainter(self)
            pen = QPen(Qt.red, 3)
            painter.setPen(pen)
            painter.drawLine(self.line)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

这篇关于PyQt5:通过单击QPushButton画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆