PyQt5:单击QPushButton绘制一条线

时间:2018-04-21 05:18:20

标签: python line pyqt5 qpushbutton paintevent

我试图使其在点击QPushButton时绘制一条线。但是,我现在使用的代码在启动代码时使开头处的行 之后。 QPushButton似乎没有做任何绘图。

我也不太明白为什么在画画时你需要一个'事件'函数中的参数。

enter image description here

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

2 个答案:

答案 0 :(得分:1)

你不应该直接调用paintEvent方法,这应该由Qt处理,因为除了你想要的GUI之外,GUI需要在其他场合重新绘制它,例如调整小部件的大小,移动等等。接收paintEvent的事件是QPaintEvent,返回需要重新绘制的矩形,这是为了优化重绘,有时很简单,因为在这种情况下不需要使用它。

paintEvent方法中,您必须在非空时绘制线条,因此您应该在连接到单击信号的插槽中执行的操作是将该空行替换为有效的,并强制使用使用paintEvent方法调用update(),该方法通知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_())

答案 1 :(得分:0)

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