0
votes

I have this screen:

enter image description here

On it I draw a line which goes out of the board:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

    def initUI(self):
        self.drawingPanel = DrawingPanel(self)

        verticalLayout = QtGui.QVBoxLayout( self )
        verticalLayout.addWidget(self.drawingPanel)

        self.setLayout( verticalLayout )
        # self.setGeometry(300, 300, 400, 300)
        self.resize( 400, 300 )

        self.setWindowTitle('Review')
        self.show()


class DrawingPanel(QtGui.QGraphicsView):

    def __init__(self, parent):
        QtGui.QGraphicsView.__init__(self, parent)
        self.setScene(QtGui.QGraphicsScene(self))
        self.setSceneRect(QtCore.QRectF(self.viewport().rect()))

        # self.setSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)

        pencil = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
        pencil.setStyle(QtCore.Qt.DotLine)

        self.scene().addLine(QtCore.QLineF(0, 0, 300, 600), pencil)


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

How to make the panel create a scroll bar and be able to show the whole line drawn?

I could find some other questions as:

  1. QGraphicsView / QGraphicsScene size matching
  2. QGraphicsView / QGraphicsScene image size
1
in C++, I use something like this: view.fitInView(scene.itemsBoundingRect());ni1ight
Also this could help you: stackoverflow.com/questions/19434019/…ni1ight
Thanks, but I want the panel to allow scroll bars. Currently the drawing is cut as on the figure.user

1 Answers

0
votes

I fixed it just removing this line from the code:

self.setSceneRect(QtCore.QRectF(self.viewport().rect()))

enter image description here

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

    def initUI(self):
        self.drawingPanel = DrawingPanel(self)

        verticalLayout = QtGui.QVBoxLayout( self )
        verticalLayout.addWidget( self.drawingPanel )

        self.setLayout( verticalLayout )
        # self.setGeometry( 300, 300, 400, 300 )
        self.resize( 400, 300 )

        self.setWindowTitle('Review')
        self.show()


class DrawingPanel(QtGui.QGraphicsView):

    def __init__( self, parent ):
        super( DrawingPanel, self ).__init__( parent )

        scene = QtGui.QGraphicsScene()
        self.setScene( scene )

        # self.setSceneRect( QtCore.QRectF( self.viewport().rect() ) )
        # self.setSizePolicy( QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum )

        pencil = QtGui.QPen( QtCore.Qt.black, 2, QtCore.Qt.SolidLine )
        pencil.setStyle( QtCore.Qt.DotLine )

        self.scene().addLine( QtCore.QLineF(0, 0, 300, 600), pencil )


def main():

    app = QtGui.QApplication( sys.argv )
    ex = Example()
    sys.exit( app.exec_() )


if __name__ == '__main__':
    main()