PyQt5: Draw a line by clicking QPushButton

风流意气都作罢 提交于 2021-02-08 09:37:05

问题


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

回答1:


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.

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



回答2:


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


来源:https://stackoverflow.com/questions/49952610/pyqt5-draw-a-line-by-clicking-qpushbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!