简体   繁体   中英

PyQt5: How to click a button to move the paint?

In the following code, one rectangle is made when the "Paint" button is clicked. It contains also a "Move" button. By clicking the "Move" button I want to move the rectangle 1px in both x and y directions. But I failed here.

I can understand the question is about how to transfer variable values wenn a button is clicked. To a beginner it is very difficult for me. Any help will be highly appreciated!

# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout, QDoubleSpinBox
from PyQt5.QtGui import QPainter, QColor, QBrush
import sys

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        self.flag = False

    def initUI(self):
        self.setFixedSize(400, 400)
        self.setWindowTitle('Colours')
        self.btnPaint = QPushButton("Paint", self)
        self.btnMove = QPushButton("Move x+1 y+1", self)

        self.deltax = 0  # original value
        self.deltay = 0  # original value

        self.btnPaint.clicked.connect(self.onPaint)
        self.btnMove.clicked.connect(self.onMove)


        self.hlayout = QHBoxLayout()
        self.hlayout.addWidget(self.btnPaint)
        self.hlayout.addWidget(self.btnMove)
        self.hlayout.addStretch(1)
        self.setLayout(self.hlayout)


        self.show()

    def onPaint(self):
        self.flag = True
        self.update()

    def onMove(self):
        self.deltax = self.deltax + 1  # button clicked, value +1
        self.deltay = self.deltay + 1  # button clicked, value +1
        self.flag_move = True
        self.update()

    def paintEvent(self, e):
        if self.flag:
            qp = QPainter()
            qp.begin(self)
            self.drawRectangles(qp)
            qp.end()
        if self.flag_move:

            qp = QPainter()
            qp.begin(self)
            self.drawRectangles(qp)
            qp.end()

    def drawRectangles(self, qp):
        x1 = 10
        y1 = 15
        x2 = 90
        y2 = 60

        col = QColor(0, 0, 0)
        col.setNamedColor('#d4d4d4')
        qp.setPen(col)
        qp.setBrush(QColor(200, 0, 0))
        qp.drawRect(x1+self.deltax, y1+self.deltay, x2+self.deltax, y2+self.deltay)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Your code has an error because the self.flag_move is not initially defined, but it is not necessary to use so many variables either.

Qt has the QRect class that has several methods that make the task easier, initially we create a null QRect, and when the btnPaint button is pressed we will enable QRect to a non-null one. in the case of pressing the btnMove button we will use the translate method that moves the rectangle:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout
from PyQt5.QtGui import QPainter, QColor, QBrush
from PyQt5.QtCore import QRect, QPoint

class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setFixedSize(400, 400)
        self.setWindowTitle('Colours')
        self.btnPaint = QPushButton("Paint", self)
        self.btnMove = QPushButton("Move x+1 y+1", self)
        self.rect = QRect()

        self.btnPaint.clicked.connect(self.onPaint)
        self.btnMove.clicked.connect(self.onMove)

        self.hlayout = QHBoxLayout(self)
        self.hlayout.addWidget(self.btnPaint)
        self.hlayout.addWidget(self.btnMove)
        self.hlayout.addStretch(1)
        self.show()

    def onPaint(self):
        if self.rect.isNull():
            self.rect = QRect(10, 15, 80, 45)
            self.update()

    def onMove(self):
        if not self.rect.isNull():
            self.rect.translate(QPoint(1, 1))
            self.update()

    def paintEvent(self, e):
        qp = QPainter(self)
        qp.setPen(QColor("#d4d4d4"))
        qp.setBrush(QColor(200, 0, 0))
        qp.drawRect(self.rect)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM