Search code examples
pythonpyqtpyqt4

PyQt: self.setScene(self.scene) AttributeError: 'Window' object has no attribute 'setScene'


Aim is to see the scrollbars in the window:
The function of drawing scrollbars has been taken from here: https://www.programcreek.com/python/example/52415/PyQt4.QtGui.QGraphicsScene

What does the error mean and what am I supposed to do to solve it?

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

from PyQt4.QtGui import QMainWindow, QSizePolicy, QWidget, QVBoxLayout, QAction,\
        QKeySequence, QLabel, QItemSelectionModel, QMessageBox, QFileDialog, QFrame, \
        QDockWidget, QProgressBar, QProgressDialog

from PyQt4.QtCore import SIGNAL, QSettings, QSize, QPoint, QVariant, QFileInfo, QTimer, pyqtSignal, QObject


class Window(QtGui.QMainWindow):

  def __init__(self, parent=None):

        QtGui.QGraphicsView.__init__(self, parent)
        self.scene = QtGui.QGraphicsScene(self)
        self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
        self.setScene(self.scene)

        self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
        self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
        self.viewport().setCursor(QtCore.Qt.CrossCursor)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

        self._pan = False
        self._draw = False
        self._moved = False
        self._sel = False
        self.pen = None
        self.penid = None
        self.cmap = None
        self.penwidth = 4
        self._redoStack = []
        self._histStates = []
        self._baseRects = [] 

app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())

Error:

Traceback (most recent call last):
  File "temp.py", line 42, in <module>
    GUI = Window()
  File "temp.py", line 21, in __init__
    self.setScene(self.scene)
AttributeError: 'Window' object has no attribute 'setScene'

Solution

  • You are indicating that Window is a QMainWindow:

    class Window(QtGui.QMainWindow):
    

    so Window is not a QGraphicsView.

    so in the example that shows this implicit that Window must be a QGraphicsView, the solution is to change QMainWindow to QGraphicsView.

    import sys
    
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    class Window(QtGui.QGraphicsView):
    
      def __init__(self, parent=None):
    
            QtGui.QGraphicsView.__init__(self, parent)
            self.scene = QtGui.QGraphicsScene(self)
            self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.darkGray, QtCore.Qt.SolidPattern))
            self.setScene(self.scene)
    
            self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
            self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
            self.viewport().setCursor(QtCore.Qt.CrossCursor)
            self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    
            self._pan = False
            self._draw = False
            self._moved = False
            self._sel = False
            self.pen = None
            self.penid = None
            self.cmap = None
            self.penwidth = 4
            self._redoStack = []
            self._histStates = []
            self._baseRects = [] 
    
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    GUI.show()
    sys.exit(app.exec_())