質問

I have a window with a push-button. Once the button is clicked , I would like to change the position of line that is initially drawn.This is my code till now.

import sys 
from PyQt4 import QtGui , QtCore

class trial(QtGui.QWidget):
    def __init__(self):
        super(trial , self).__init__()
        self.window()

    def window(self):
        self.a = 0
        button = QtGui.QPushButton('Push')
        button.setCheckable(True)
        button.clicked.connect(self.fun)
        grid = QtGui.QGridLayout()
        grid.addWidget(button , 0 , 0)
        self.setLayout(grid)
        self.setWindowTitle('Trial')
        self.setGeometry(500 , 500 , 500 , 500)
        self.show()

    def fun(self , text):
        if text == 1:
            self.a = 0

        else:
            self.a = 1


    def paintEvent(self , e):
        qp = QtGui.QPainter()
        qp.begin(self)
        pen = QtGui.QPen(QtCore.Qt.red , 5 , QtCore.Qt.SolidLine)
        qp.setPen(pen)

        if self.a == 0:

            qp.drawLine(20 , 20 , 20 , 480)
        else:
            qp.drawLine(480 , 20 , 480 , 480) 
        qp.end()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    t = trial()
    sys.exit(app.exec_())

I know my code looks a bit awkward but I'm facing the following problems. 1. It takes quite a bit of time after the button is pressed for the line to change position.

2.Also how do I rub the line that is previously drawn.

It would be nice if someone could modify the code for me.Thanks.

役に立ちましたか?

解決

1) Add a call to self.update() in your fun() function to let Qt know that your widget needs to be redrawn

2) (Not totally sure on the python syntax, so I'll give the example in C++)

You can paint the background explicitly in your paint event:

qp.setBrush( Qt::white );  // Or whatever color you want the background to be 
qp.drawRect( rect() );

or you can try messing with QWidget::setAutoFillBackground()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top