Need to draw a image to replace the rectangle in PyQt5

I am writing a labyrinth on PyQt5, and it looks a labyrinth like this: enter image description here

That is, the walls, start, finish, and portals look just colored rectangles. And I had a question, how can I change them into images?

Here I get the color for these rectangles. How can I change this drawRect on the same drawImage to draw pictures.

Walls and Floor

def draw_maze(self, drawer):
    for x in range(self.maze.width):
        for y in range(self.maze.height):
            if self.maze[x][y] == self.maze.WALL:
                drawer.setPen(Qt.white)
                drawer.setBrush(Qt.black)
                drawer.drawRect(x * self.maze.scale,
                                y * self.maze.scale,
                                self.maze.scale,
                                self.maze.scale)
            elif self.maze[x][y] == self.maze.EMPTY:
                drawer.setPen(QColor(200, 200, 200))
                drawer.setBrush(Qt.white)
                drawer.drawRect(x * self.maze.scale,
                                y * self.maze.scale,
                                self.maze.scale,
                                self.maze.scale)

Start and Finish

def draw_start_and_finish(self, drawer):
    if self.maze.start:
        drawer.setPen(Qt.white)
        drawer.setBrush(Qt.green)
        drawer.drawRect(self.maze.start[0] * self.maze.scale,
                        self.maze.start[1] * self.maze.scale,
                        self.maze.scale,
                        self.maze.scale)
    if self.maze.finishes:
        drawer.setPen(Qt.white)
        drawer.setBrush(Qt.blue)
        for finish in self.maze.finishes:
            drawer.drawRect(finish[0] * self.maze.scale,
                            finish[1] * self.maze.scale,
                            self.maze.scale,
                            self.maze.scale)

Portals

def draw_portals(self, drawer):
    colors = [QColor(0, 255, 255), QColor(255, 128, 0),
              QColor(255, 0, 128), QColor(0, 102, 0),
              QColor(0, 0, 102), QColor(153, 204, 255)]
    drawer.setPen(Qt.black)
    if self.portal1:
        drawer.setBrush(colors[len(self.maze.portals)])
        drawer.drawRect(self.portal1[0] * self.maze.scale,
                        self.portal1[1] * self.maze.scale,
                        self.maze.scale,
                        self.maze.scale)
    for i in range(len(self.maze.portals)):
        drawer.setBrush(colors[i])
        for portal in self.maze.portals[i]:
            drawer.drawRect(portal[0] * self.maze.scale,
                            portal[1] * self.maze.scale,
                            self.maze.scale,
                            self.maze.scale)

The path is also drawn with colored rectangles.

def draw_path(self, drawer):
    if self.path:
        if self.draw_instantly:
            for point in self.path:
                if self.maze.is_wall(point[0], point[1]):
                    self.set_color(drawer, Qt.red)
                else:
                    self.set_color(drawer, Qt.yellow)
                drawer.drawRect(point[0] * self.maze.scale,
                                point[1] * self.maze.scale,
                                self.maze.scale,
                                self.maze.scale)
        else:
            for i in range(self.count):
                if self.maze.is_wall(self.path[i][0], self.path[i][1]):
                    self.set_color(drawer, Qt.red)
                else:
                    self.set_color(drawer, Qt.yellow)
                drawer.drawRect(self.path[i][0] * self.maze.scale,
                                self.path[i][1] * self.maze.scale,
                                self.maze.scale,
                                self.maze.scale)
            self.count += 1
            if self.count > len(self.path) - 1:
                self.count = len(self.path) - 1
        self.update()

Method to get color:

@staticmethod
def set_color(drawer, color):
    drawer.setPen(Qt.black)
    drawer.setBrush(color)

I apologize that I can not attach all the code, it is quite large, and scattered across many files.

P.s. Added a few hours later.

I provide code that works just like my:

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor, QBrush


class MapEditor(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.setGeometry(300, 300, 350, 100)
        self.setWindowTitle('MapEditor')
        self.show()


    def paintEvent(self, e):
        drawer = QPainter()
        drawer.begin(self)
        self.drawRectangles(drawer)
        drawer.end()


    def drawRectangles(self, drawer):

        col = QColor(0, 0, 0)
        col.setNamedColor('#d4d4d4')
        drawer.setPen(col)

        drawer.setBrush(Qt.white)
        drawer.setBrush(QColor(200, 0, 0))
        drawer.drawRect(10, 15, 60, 60)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MapEditor()
    sys.exit(app.exec_())

I need instead of this rectangle to have a picture of the same size. I just do not know which method to use correctly. I tried all sorts of methods like DrawImage or something like:

    label = QLabel(self)
    pixmap = QPixmap(image_path)
    label.setPixmap(pixmap)

But it didn't work out for me, and it gave out very incomprehensible errors that I unfortunately already lost.

Upvotes: 2

Views: 1049

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

You have to use the drawPixmap() method of QPainter but before them you must scale the QPixmap with the scaled() method as shown below:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class MapEditor(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 350, 100)
        self.setWindowTitle('MapEditor')

    def paintEvent(self, e):
        drawer = QtGui.QPainter(self)
        drawer.setRenderHint(QtGui.QPainter.Antialiasing)

        r = QtCore.QRect(10, 15, 60, 60)

        image_path = "red.png"
        pixmap = QtGui.QPixmap(image_path)
        pixmap = pixmap.scaled(r.size())
        drawer.drawPixmap(r, pixmap)


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

red.png

enter image description here

Output:

enter image description here

Upvotes: 3

Related Questions