简体   繁体   English

带有QGraphicsWidget的pyside2应用立即关闭

[英]pyside2 app with QGraphicsWidget is closing immediately

in my project ( a mastermind game), I have the special need to use the Graphics View Framework because I want to manage a graphic game with pawns which are moved by drag'n drop. 在我的项目(策划游戏)中,我特别需要使用Graphics View Framework,因为我想使用通过拖放移动的棋子来管理图形游戏。

I manage displaying the window but there is nothing in the GraphicsWidget: 我设法显示窗口,但是GraphicsWidget中没有任何内容:

outrun.py outrun.py

#!/usr/bin/python

from PySide2 import QtGui, QtWidgets as wids
import sys
import out
import PySide2

class ImageViewer(wids.QMainWindow, out.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ImageViewer, self).__init__(parent)
        self.setupUi(self)

    def main(self):
        self.changeGraphicsView()
        self.show()

    def changeGraphicsView(self):
        scene=wids.QGraphicsScene() 
        self.graphicsView.setScene(scene)

        windowLayout=wids.QGraphicsGridLayout()

        title0=wids.QGraphicsSimpleTextItem()
        title0.setText("JEU DE MASTERMIND")
        title=wids.QGraphicsWidget(title0)
        windowLayout.addItem(title,0,0)

        title1=wids.QGraphicsSimpleTextItem()
        title1.setText("super!")
        title2=wids.QGraphicsWidget(title1)
        windowLayout.addItem(title2,0,1)

        form=wids.QGraphicsWidget()
        form.setLayout(windowLayout)
        scene.addItem(form)

        self.graphicsView.show()


if __name__=='__main__':
    app = wids.QApplication(sys.argv)
    imageViewer = ImageViewer()
    imageViewer.main()
    app.exec_()

and out.py (from the .ui file): The only interesting thing is the field 'self.graphicsView', which is used in the above code. 和out.py(来自.ui文件):唯一有趣的是上面的代码中使用的字段“ self.graphicsView”。

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(10, 100, 761, 441))
        brush = QtGui.QBrush(QtGui.QColor(107, 197, 190))
        brush.setStyle(QtCore.Qt.NoBrush)
        self.graphicsView.setBackgroundBrush(brush)
        self.graphicsView.setObjectName("graphicsView")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 20, 781, 51))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("Mongolian Baiti")
        font.setPointSize(28)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("MainWindow", "HELLO QT !", None, -1))

thank you. 谢谢。

It' not how QGraphicsWidget works: this class is used to embed widgets in a QGraphicsScene . 它不是QGraphicsWidget工作方式:此类用于将窗口小部件嵌入QGraphicsScene

You should reconsider the way you want to build your UI (you don't have to embed everything in the scene). 您应该重新考虑构建UI的方式(不必将所有内容都嵌入到场景中)。

This is how you should use it: 这是您应该如何使用它:

class Display(QGraphicsWidget):
    def __init__(self):
        QGraphicsWidget.__init__(self)
        #création du layout
        self.layout= QGraphicsGridLayout()
        self.setLayout(self.layout)
        self.layout.setRowMinimumHeight(1,400)
        self.layout.setColumnMinimumWidth(0,250)

        #ajout des composants
        title0= QGraphicsSimpleTextItem(self)
        title0.setText("JEU DE MASTERMIND")
        title0.setFont(QFont("Times",20,QFont.ExtraExpanded|QFont.ExtraBold))
        title= QGraphicsWidget(title0)
        self.layout.addItem(title,0,0,1,2)

        title02= QGraphicsSimpleTextItem(self)
        title02.setText("haha")
        title2= QGraphicsWidget(title02)
        self.layout.addItem(title2,1,0,1,1)

import sys

if __name__ == '__main__':
    app =  QApplication(sys.argv)
    window=Display()

    # Create a view and its scene
    scene= QGraphicsScene()
    view= QGraphicsView(scene)

    # Insert your Display item in the view
    scene.addItem(window)
    view.show()
    sys.exit(app.exec_())

Btw, your window is closed immediatly because there is no widget to display (the view was destroyed at the end of your constructor). 顺便说一句,由于没有窗口小部件可显示,因此窗口立即关闭( view在构造函数的末尾被破坏了)。

I once made a Pyside2 framework for a minmimal GUI with all the main features when having the need to use the Graphics View Framework. 当需要使用Graphics View Framework时,我曾经为最小的GUI创建了一个Pyside2框架,具有所有主要功能。 You can find it at my Github page 您可以在我的Github页面上找到它

It contains a view, a scene, graphics items and has mouse and key control capability. 它包含一个视图,一个场景,图形项目,并具有鼠标和键控制功能。 This should serve as a good start or as an example for the implementation of some classes and methods with respect to the Graphics View Framework. 这应该是一个很好的开始,或者是有关Graphics View Framework的某些类和方法的实现的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM