QMainWindow 上的 QGraphicsScene
QGraphicsScene on a QMainWindow
Python 还是个新手,所以我试图让 QGraphicsScene 位于 QMainWindow 中。现在我的编码方式是两个 windows 会出现,但我只希望一个 window 与 QGraphicsScene "inside" QMainWindow 一起出现。这是我的代码:
import sys
from PyQt4 import QtGui, QtCore
class MyView(QtGui.QGraphicsView):
def __init__(self):
QtGui.QGraphicsView.__init__(self)
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(300,400,100,100)
self.scene.addItem(self.item)
self.setScene(self.scene)
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
view = MyView()
view.show()
sys.exit(app.exec_())
run()
非常感谢!
您必须在 QMainWindow
中创建 QGraphicsScene
(就像您希望它出现的那样)
然后在主体中,您只需创建 Window
的实例并显示它。
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
#create the view
self.view=MyView()
self.setCentralWidget(self.view)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
Qt(和其他 GUI 工具包)相信育儿。有一个 window 或另一个小部件,设置其父级。请注意,您应该在外部调用 show。
import sys
from PyQt4 import QtGui, QtCore
class MyView(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent=parent)
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(300,400,100,100)
self.scene.addItem(self.item)
self.setScene(self.scene)
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
#This initializes the main window or form
super(Window,self).__init__(parent=parent)
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
view = MyView(GUI)
GUI.show()
sys.exit(app.exec_())
run()
Python 还是个新手,所以我试图让 QGraphicsScene 位于 QMainWindow 中。现在我的编码方式是两个 windows 会出现,但我只希望一个 window 与 QGraphicsScene "inside" QMainWindow 一起出现。这是我的代码:
import sys
from PyQt4 import QtGui, QtCore
class MyView(QtGui.QGraphicsView):
def __init__(self):
QtGui.QGraphicsView.__init__(self)
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(300,400,100,100)
self.scene.addItem(self.item)
self.setScene(self.scene)
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
view = MyView()
view.show()
sys.exit(app.exec_())
run()
非常感谢!
您必须在 QMainWindow
中创建 QGraphicsScene
(就像您希望它出现的那样)
然后在主体中,您只需创建 Window
的实例并显示它。
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
#create the view
self.view=MyView()
self.setCentralWidget(self.view)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
Qt(和其他 GUI 工具包)相信育儿。有一个 window 或另一个小部件,设置其父级。请注意,您应该在外部调用 show。
import sys
from PyQt4 import QtGui, QtCore
class MyView(QtGui.QGraphicsView):
def __init__(self, parent=None):
QtGui.QGraphicsView.__init__(self, parent=parent)
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(300,400,100,100)
self.scene.addItem(self.item)
self.setScene(self.scene)
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
#This initializes the main window or form
super(Window,self).__init__(parent=parent)
self.setGeometry(50,50,900,700)
self.setWindowTitle("Pre-Alignment system")
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
view = MyView(GUI)
GUI.show()
sys.exit(app.exec_())
run()