Python还是新手,所以我试图让QGraphicsScene在QMainWindow中。 现在我编码的方式是会出现两个窗口,但我只希望一个窗口与QMainWindow"内部"的QGraphicsScene一起出现。 这是我的代码:
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()
非常感谢!
Qt(和其他GUI工具包)相信育儿。在另一个窗口中有一个窗口或小部件,设置其父级。请注意,您应该在外部调用显示。
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()
你必须在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_())