0

How do I scale the drawn pixmap of QGraphicsItem? Can't find any setScaled(), size or height property there. After the picture becomes big, the scrollbars of MyGraphicsView class should be able to scroll the big scaled picture.

#!/usr/bin/env python

from PyQt5.QtCore import (QRectF, Qt)
from PyQt5.QtGui import (QPainter, QPixmap)
from PyQt5.QtWidgets import (QMainWindow, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem)


class TicTacToe(QGraphicsItem):
    def __init__(self, helper):
        super(TicTacToe, self).__init__()
        self.mypixmap = QPixmap("exit.png")

    def paint(self, painter, option, widget):
        painter.setOpacity(1)
        painter.drawPixmap(0,0, 300, 300, self.mypixmap)


    def boundingRect(self):
        return QRectF(0,0,300,300)


class MyGraphicsView(QGraphicsView):
    def __init__(self):
        super(MyGraphicsView, self).__init__()
        scene = QGraphicsScene(self)

        self.tic_tac_toe = TicTacToe(self)

        scene.addItem(self.tic_tac_toe)

        self.setScene(scene)

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)


class Example(QMainWindow):    
    def __init__(self):
        super(Example, self).__init__()

        self.y = MyGraphicsView()
        self.setCentralWidget(self.y)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • I see that it works correctly. What is the problem? You are not very clear about describing your problem, it shows proof of your problem. https://imgur.com/a/wLibR – eyllanesc Apr 17 '18 at 04:13
  • I will add more details. @eyllanesc – Aquarius_Girl Apr 17 '18 at 04:15
  • @Aquarius_Girl. [How to enable Pan and Zoom in a QGraphicsView](https://stackoverflow.com/a/35514531/984421). – ekhumoro Apr 17 '18 at 04:16
  • That's what I'm referring to when you take the time to post a good question. Do not you think that it bores us to see a question so general that it does not show evidence of the problem? So do you think you would encourage us to help? – eyllanesc Apr 17 '18 at 04:17
  • @eyllanesc Please understand that I have no intention of troubling you. In this example at least can you tell me please how to increase the size of the drawn pixmap. That part of the question is clear at least. – Aquarius_Girl Apr 17 '18 at 04:19
  • @Aquarius_Girl Then read [ask], there it tells you how to improve a question and it will not cause these problems. My code is as follows: https://gist.github.com/eyllanesc/5e79f469b82d9075d9bacb2409f410dd/archive/2ab511c16468dfdae88a738a0d6bd68ec988ad66.zip – eyllanesc Apr 17 '18 at 04:21
  • What code is in that link? @eyllanesc – Aquarius_Girl Apr 17 '18 at 04:22
  • @Aquarius_Girl. I already gave you a link to an answer with a complete working example. Any reason why I shouldn't close this question as a duplicate? – ekhumoro Apr 17 '18 at 04:24
  • @Aquarius_Girl There is how I scale the item. – eyllanesc Apr 17 '18 at 04:24
  • @ekhumoro let me please study that question. – Aquarius_Girl Apr 17 '18 at 04:24
  • @eyllanesc thanks. – Aquarius_Girl Apr 17 '18 at 04:25
  • @ekhumoro I already did it :D – eyllanesc Apr 17 '18 at 04:25
  • @eyllanesc you code does increase the size of the picture nicely but it also increases the size of the dots that I draw by painter. That is a big problem. – Aquarius_Girl Apr 17 '18 at 04:28
  • @Aquarius_Girl There are several algorithms to scale images, do you want the change to be smoother? – eyllanesc Apr 17 '18 at 04:30
  • @Aquarius_Girl When you perform a scaling a pixel will occupy more pixels, so there are several methods to create these new pixels, one of them is to copy the value of the pixel, and others calculate an average of the surrounding pixels, look at the following code, the change is smoother. https://gist.github.com/eyllanesc/5e79f469b82d9075d9bacb2409f410dd/archive/5a6b53acd3870c0bb61f180482780dd912d89a64.zip – eyllanesc Apr 17 '18 at 04:33

0 Answers0