Green Cell
Green Cell

Reputation: 4777

PySide QGraphicsView size

I have 2 issues with QGraphicsView.

I can't get the size of the QGraphicsView object. All methods I'm using is giving me values I wouldn't expect.

If I print out the mouse's position on the area's lower-right corner (scrollbars included), I get a random 400 value. After setting sceneRect to 500 I was expecting to get that back.

from PySide import QtGui, QtCore

class View(QtGui.QGraphicsView):
    def __init__(self, parent = None):
        super(View, self).__init__(parent)
        self.setScene( QtGui.QGraphicsScene(self) )
        self.setSceneRect( 0, 0, 500, 500 )

        print self.viewport().width() # outputs 96
        print self.width() # outputs 100
        print self.rect() # outputs QRect(0, 0, 100, 30)
        print self.size() # outputs QSize(100, 30)

    def mouseMoveEvent(self, event):
        print event.pos().toTuple() # prints (413, 423) at lower-right corner

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

        self.resize(500, 500)

        self.view = View(self)
        hLayout = QtGui.QHBoxLayout()
        hLayout.addWidget(self.view)

        buttonsLayout = QtGui.QVBoxLayout()
        buttonsLayout.setSpacing(0)
        for i in range(10):
            newButton = QtGui.QPushButton()
            buttonsLayout.addWidget(newButton)
        hLayout.addLayout(buttonsLayout)

        self.tempButton = QtGui.QPushButton()

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addLayout(hLayout)
        mainLayout.addWidget(self.tempButton)
        self.setLayout(mainLayout)

    def run(self):
        self.show()

win = MainWindow()
win.run()

Thank you!

Upvotes: 3

Views: 3543

Answers (1)

Jean-Sébastien
Jean-Sébastien

Reputation: 2697

Regarding your first issue, I believe you are not getting the sizes you are expecting for two reasons:

  1. You are not explicitly setting the size of the QGraphicsView widget to 500, but the QGraphicsScene instead.
  2. You are fetching the sizes too early in the construction of your application, before the layout of the MainWindow has been properly painted.

Regarding your second issue, depending of what is desired, it is possible to use the method mapFromScene to get the position of the mouse event in regard to the QGraphicsScene instead of the QGraphicsView widget.

More specifically, this can be achieve in your code by:

  1. Setting the size of the QGraphicsView widget with setFixedSize ;
  2. Moving the "size-fetching" calls in the run method, after the MainWindow has been painted ;
  3. Adding a mapToScene transformation on the mouseMoveEvent coordinate.

Below is the code that was modified accordingly to the points listed above:

from PySide import QtGui, QtCore
import sys

class View(QtGui.QGraphicsView):
    def __init__(self, parent = None):
        super(View, self).__init__(parent)
        self.setScene(QtGui.QGraphicsScene(self) )
        self.setSceneRect( 0, 0, 1000, 1000 )
        self.setFixedSize(500, 500)

    def mouseMoveEvent(self, event):
        print
        print self.mapToScene(event.pos()).toTuple() 
        # prints (1000, 1000) at lower-right corner
        print event.pos().toTuple()
        # prints (500, 500) at lower-right corner

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

        self.view = View(self)

        hLayout = QtGui.QHBoxLayout()
        hLayout.addWidget(self.view)

        buttonsLayout = QtGui.QVBoxLayout()
        buttonsLayout.setSpacing(0)
        for i in range(10):
            newButton = QtGui.QPushButton()
            buttonsLayout.addWidget(newButton)
        hLayout.addLayout(buttonsLayout)

        self.tempButton = QtGui.QPushButton()

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addLayout(hLayout)
        mainLayout.addWidget(self.tempButton)
        self.setLayout(mainLayout)

    def run(self):
        self.show()  
        print
        print self.view.viewport().width() # outputs 485
        print self.view.width() # outputs 500
        print self.view.rect() # outputs QRect(0, 0, 500, 500)
        print self.view.size() # outputs QSize(500, 500)
        print self.view.sceneRect() #outputs QRect(0, 0, 1000, 1000)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    win = MainWindow()  
    win.run()  

    sys.exit(app.exec_())

With the code above, the value returned for the size of QGraphicView is 500x500, while it is 1000x1000 for the QGraphicsScene, as expected.

Upvotes: 1

Related Questions