我正在尝试学习PySide2,并尝试将常规组件与某些Graphics View Framework混合使用。
问题在于,在不考虑QGraphicsGridLayout要求的情况下显示了2个QGraphicsSimpleTextItems:它们都位于同一位置(因此所产生的文本不可读)。
这是我自己的小代码:
# coding: utf-8
from PySide2 import QtCore as core, QtWidgets as wids, QtGui as gui
import sys
class Display(wids.QMainWindow):
def __init__(self):
wids.QMainWindow.__init__(self)
self.changeGraphics()
def changeGraphics(self):
self.resize(500, 500)
#au top : un QWidget
self.central_widget = wids.QWidget(self)
self.central_layout = wids.QHBoxLayout()
self.central_widget.setLayout(self.central_layout)
self.setCentralWidget(self.central_widget)
self.label = wids.QLabel(self.central_widget)
self.label.setText('Ca marche')
self.central_layout.addWidget(self.label)
#on ajoute le nécessaire graphique
self.view = wids.QGraphicsView()
self.scene = wids.QGraphicsScene()
self.view.setScene(self.scene)
self.central_layout.addWidget(self.view)
#il faut disposer les éléments dans la scène par un QGraphicsGridLayout
##panel=conteneur général d'éléments graphiques
panel = wids.QGraphicsWidget()
self.scene.addItem(panel)
layout = wids.QGraphicsGridLayout()
panel.setLayout(layout)
#au layout, on ajoute un élément graphique
title0=wids.QGraphicsSimpleTextItem(panel)
title0.setText("JEU DE MASTERMIND")
title=wids.QGraphicsWidget(title0)
layout.addItem(title,0,0)
title1=wids.QGraphicsSimpleTextItem(panel)
title1.setText("super!")
title2=wids.QGraphicsWidget(title1)
layout.addItem(title2,1,0)
if __name__ == "__main__":
app = wids.QApplication(sys.argv)
display=Display()
display.show()
display.changeGraphics()
sys.exit(app.exec_())
谢谢。
答案 0 :(得分:1)
QGraphicsLayout
和QGraphicsWidget
的工作方式不是这样。
此行title=wids.QGraphicsWidget(title0)
不会将QGraphicsItem
嵌入QGraphicsWidget
中,而是创建一个以title0
作为父对象的新对象。
如果要将QGraphicsItem
放在QGraphicsLayout
中(这是为QGraphicsWidget
设计的),则必须:
QGraphicsLayoutItem
和QGraphicsItem
的新类。答案 1 :(得分:1)
尝试一下:
import sys
from PyQt5 import QtCore as core, QtWidgets as wids, QtGui as gui
class Display(wids.QMainWindow):
def __init__(self):
super().__init__()
self.changeGraphics()
def changeGraphics(self):
self.resize(500, 500)
#au top : un QWidget
self.central_widget = wids.QWidget(self)
self.central_layout = wids.QHBoxLayout()
self.central_widget.setLayout(self.central_layout)
self.setCentralWidget(self.central_widget)
self.label = wids.QLabel(self.central_widget)
self.label.setText('Ca marche')
self.central_layout.addWidget(self.label)
#on ajoute le nécessaire graphique
self.view = wids.QGraphicsView()
self.scene = wids.QGraphicsScene()
self.view.setScene(self.scene)
self.central_layout.addWidget(self.view)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.labelTitle0 = wids.QLabel('JEU DE MASTERMIND')
self.labelTitle1 = wids.QLabel('super!')
self.itemLabelTitle0 = self.scene.addWidget(self.labelTitle0)
self.itemLabelTitle1 = self.scene.addWidget(self.labelTitle1)
layout = wids.QGraphicsGridLayout()
layout.addItem(self.itemLabelTitle0, 0, 0)
layout.addItem(self.itemLabelTitle1, 1, 0)
self.panelWidget = wids.QGraphicsWidget()
self.panelWidget.setLayout(layout)
self.scene.addItem(self.panelWidget)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if __name__ == "__main__":
app = wids.QApplication(sys.argv)
display = Display()
display.show()
# display.changeGraphics()
sys.exit(app.exec_())