PyQt5:通过点击QPushButton画一条线

PyQt5: Draw a line by clicking QPushButton

我试图让它在我单击 QPushButton 时绘制一条线。但是,我现在拥有的代码在启动代码时在 beginning 处生成行,而不是 after。 QPushButton 似乎没有做任何绘图。

我也不太明白为什么在绘图时需要在函数中添加一个 'event' 参数。

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是一个QPaintEventreturns一个需要重绘的矩形,这个是为了优化重绘,有时很简单因为这种情况下没必要用它。

paintEvent 方法中,您必须在不为空时绘制线,因此您应该在连接到点击信号的插槽中做的是用有效线替换该空线,并强制使用 update() 方法调用 paintEvent,通知 GUI 它需要重新绘制。

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_())
import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication



from PyQt5.QtCore import QSize, Qt, QLine, QPoint
from PyQt5.QtGui import QPainter, QPen, QCursor


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

        self.setMinimumSize(QSize(300, 300))

        self.pybutton = QPushButton('button', self)
        #self.pybutton.clicked.connect(self.draw_line)
        self.pybutton.resize(100, 100)

        # self.pybutton.move(100, 100)
        self.line = QLine()
        self.statusBar()

    def draw_line(self,x,y):
        #sender = self.sender()
        pos=QPoint(x,y)
        #self.statusBar().showMessage(sender.text() + ' was pressed')
        self.line = QLine(QPoint(),pos)
        self.update()

    def paintEvent(self, event):
        # QMainWindow.paintEvent(self, event)

        qp = QPainter()
        qp.begin(self)
        pen = QPen(Qt.red, 2, Qt.SolidLine)
        qp.setPen(pen)
        qp.drawLine(self.line)
        qp.end()

    #def mousePressEvent(self, event):
     #   self.pybutton.move(event.x(), event.y())

    def mouseMoveEvent(self,vent):
        self.pybutton.move(vent.x(),vent.y())
        self.draw_line(vent.x(),vent.y())



if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    main`enter code here`Win.show()
    sys.exit(app.exec_())