【问题标题】:PyQt5: Draw a line by clicking QPushButtonPyQt5:通过单击 QPushButton 绘制一条线
【发布时间】:2018-10-01 19:38:39
【问题描述】:

我试图让它在我单击 QPushButton 时绘制一条线。然而,我现在拥有的代码在代码启动时在 beginning 处而不是 之后。 QPushButton 似乎不做任何绘图。

我也不太明白为什么在绘图时需要在函数中添加“事件”参数。

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

【问题讨论】:

    标签: python line pyqt5 qpushbutton paintevent


    【解决方案1】:

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

    paintEvent方法中你必须在它不为空的时候画线,所以你应该在连接到点击信号的槽中做的是用一个有效的替换那个空线,并强制@987654325使用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_())
    

    【讨论】:

    • 非常感谢,这正是我所要求的。不过我想知道,如果我有多个按钮,我想使用相同的功能绘制不同的线条,我将如何获得它以便在绘制新线条时保留之前的线条?现在,当绘制一条新线时,它们会被删除。我试图删除更新,但似乎效果不佳。 .抱歉,我提出的问题没有我现在意识到的那么全面。
    【解决方案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_())
    

    【讨论】:

    • 请在您的代码中添加一些 cmets。请解释为什么你的代码回答了这个问题。
    猜你喜欢
    • 2012-08-28
    • 1970-01-01
    • 2014-11-17
    • 2018-04-27
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    相关资源
    最近更新 更多