【问题标题】:QGraphicsGridLayout without effect : all components are at the same placeQGraphicsGridLayout 没有效果:所有组件都在同一个地方
【发布时间】: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


    【解决方案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_())
    

    【讨论】:

    • 好的,它正在工作......但我认为它非常复杂,因为在 QGraphicsView 中,QLabels 被添加到场景中两次:一次直接,另一次间接:通过变量itemLabelTitlex,添加到 QGraphicsGridLayout 中,它规则 panelWidget 的定位,进而添加到场景中...
    【解决方案2】:

    这不是QGraphicsLayoutQGraphicsWidget 的工作方式。

    title=wids.QGraphicsWidget(title0) 这一行没有将QGraphicsItem 嵌入到QGraphicsWidget 中,而是创建了一个以title0 作为父对象的新对象。

    如果您想将QGraphicsItem 放入QGraphicsLayout(用于QGraphicsWidget),您必须:

    • 创建一个继承自QGraphicsLayoutItemQGraphicsItem 的新类。
    • 实现抽象方法

    Qt documentation中有一个例子

    【讨论】:

      猜你喜欢
      • 2016-05-08
      • 2021-03-19
      • 2023-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多