【发布时间】:2019-08-21 15:44:33
【问题描述】:
我正在尝试学习 PySide2,并尝试将常规组件与一些图形视图框架混合使用。
问题是显示 2 个 QGraphicsSimpleTextItems 时不考虑 QGraphicsGridLayout 要求:两者都在同一个地方(因此生成的文本不可读)。
这是我的小而独立的代码:
# 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_())
谢谢。会
【问题讨论】:
标签: python qt pyqt qgraphicsview pyside2