目标是在窗口中看到滚动条:
绘制滚动条的功能已从此处获取:https://www.programcreek.com/python/example/52415/PyQt4.QtGui.QGraphicsScene
错误是什么意思,我应该怎么做才能解决它?
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_())
错误:
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'
答案 0 :(得分:1)
您表示Window
是QMainWindow
:
class Window(QtGui.QMainWindow):
所以Window
不是QGraphicsView。
所以在示例中显示隐含的Window必须是QGraphicsView
,解决方法是将QMainWindow
更改为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_())