我正在开发游戏,它是用 qml 和 pyqt 编写的,但应该分为两个窗口(启动器 + 游戏)。在这两个 qml 文件之间切换的正确方法是什么?我不想使用 QmlLoader,因为它不会调整窗口大小而且它需要很多信号!我也在尝试这个变体:
view.engine().clearComponentCache()
view.setSource(source())
但它没有工作(qml 窗口停止工作...... -经典窗口错误,但是在 pycharm 控制台中没有写入错误)
我的代码如下所示:
from PyQt5.QtCore import pyqtProperty, QRectF, QUrl, QObject, pyqtSignal, pyqtSlot, QVariant
from PyQt5.QtGui import QColor, QGuiApplication, QPainter, QPen
from PyQt5.QtQml import qmlRegisterType
from PyQt5.QtQuick import QQuickItem, QQuickPaintedItem, QQuickView
from PyQt5 import QtNetwork as QN
from PyQt5 import QtCore as QC
from multiprocessing import Process
import server as S
import client as C
from time import time, sleep
class Launcher(QQuickItem):
PORTS = (9998, 9999)
PORT = 9999
SIZEOF_UINT32 = 4
changeUI = pyqtSignal()
changeName= pyqtSignal(int, str)
connection= pyqtSignal(int, bool)
connected= pyqtSignal(QVariant)
@pyqtSlot(name="startGame")
def start_game(self):
#app.exit()
startGame()
print ("startGame")
@pyqtProperty(str)
def name(self):
print ("return name")
return self._name
@name.setter
def name(self, n):
print ("set name")
self._name= n
@pyqtSlot(name= "terminate")
def terminate_server(self):
if not self.server: return
self.server.terminate() #Bye
self.server.join()
#self.bstopServer.setEnabled(False)
#self.bserver.setEnabled(True)
self.server = None
@pyqtSlot()
def _quit(self):
if self.server:
self.server.terminate() #Bye
self.server.join()
app.exit()
@pyqtSlot()
def back(self):
self.client.disconnect()
def __init__(self, parent=None):
super(Launcher, self).__init__(parent)
self.socket= QN.QTcpSocket() #Yeah, the game will be over internet
self.server = None
self.client = None #client is a QObject
self._turnedOn = False
self._players = 1
self._name = "YourName"
class Game(QQuickItem):
def __init__(self, parent= None):
super(Game, self).__init__(parent)
self.client = True #I should get the client from the launcher, but I don´t know how
def startGame():
view.engine().clearComponentCache()
view.setResizeMode(QQuickView.SizeViewToRootObject)
view.showFullScreen()
view.setSource(
QUrl.fromLocalFile(
os.path.join(os.path.dirname(__file__),'Game.qml')))
view.show()
#app.exec_()
def startLauncher():
view.engine().clearComponentCache()
view.setResizeMode(QQuickView.SizeViewToRootObject)
view.setSource(
QUrl.fromLocalFile(
os.path.join(os.path.dirname(__file__),'Launcher.qml')))
view.show()
app.exec_()
if __name__ == '__main__':
import os
import sys
app = QGuiApplication(sys.argv)
qmlRegisterType(Launcher, "ParanoiaLauncher", 1, 0, "App")
qmlRegisterType(Game, "ParanoiaGame", 1, 0, "App")
view = QQuickView()
startLauncher()
如您所见,我的结构有点混乱,因为我是第一次做这种切换行为,所以我真的不知道应该如何正确地做......欢迎每一个建议!:)