在我的QML GUI中,对象是在运行时由JavaScript创建的。 如何访问这些动态创建的对象的属性。 QML手册(http://doc.qt.io/archives/qt-4.8/qdeclarativedynamicobjects.html)中没有关于此的信息
我的traget ID在创建后更改属性,并将其存储在Qt.labs.settings中,以便我的程序保存创建的对象并可以在启动时再次创建这些对象。
使用CreateSquareGaugeScript.createRect()创建对象是可行的,但此后我找不到找到其属性的方法。
按要求提供我的简短示例。
main.cpp:
import sys,time,random
global enemyhp
global playerhp
print("""
░░ ▄▄ ▒█▀▀█ ▒█▀▀█ ▒█▀▀█ ▀▀█▀▀ ▒█░▒█ ▀█▀ ▒█▄░▒█ ▒█▀▀█ ▒█▀▀▀█ ▄▄ ░░
▀▀ ▄▄ ▒█▄▄▀ ▒█▄▄█ ▒█░▄▄ ░▒█░░ ▒█▀▀█ ▒█░ ▒█▒█▒█ ▒█░▄▄ ▒█░░▒█ ▄▄ ▀▀
░░ ░░ ▒█░▒█ ▒█░░░ ▒█▄▄█ ░▒█░░ ▒█░▒█ ▄█▄ ▒█░░▀█ ▒█▄▄█ ▒█▄▄▄█ ░░ ░░
RPG BATTLE THINGY-WINGO
""")
monstertypes = ["BATTY McBATFACE", "SKELETON", "BOSS BAT", "SUPER SKELETON"]
playerhp = 100
#set ID of the attacker
monstername = random.choice(monstertypes)
if monstername == "BATTY McBATFACE":
enemyhp = 10
elif monstername == "SKELETON":
enemyhp = 25
elif monstername == "BOSS BAT":
enemyhp = 50
elif monstername == "SUPER SKELETON":
enemyhp = 80
typing_speed = 50 #wpm
def slow_type(t, d):
for l in t:
sys.stdout.write(l)
sys.stdout.flush()
if d == "":
d = 1
time.sleep(d / 10)
print("")
def attack():
print("You attack the", monstername + "!")
attackdmg = random.randint(5, 20)
time.sleep(1)
if not random.randint(1, 4) == 4:
enemyhp = enemyhp - attackdmg
if attackdmg >= 15:
slow_type("THWACK!", 0.5)
print(attackdmg, "HP of critical damage to the", monstername + "!")
elif attackdmg >= enemyhp:
time.sleep(0.5)
slow_type("KAPOW!", 0.5)
print(attackdmg, "HP of mortal damage to the", monstername + "!")
else:
time.sleep(0.5)
print(attackdmg, "HP of damage to the", monstername + "!")
else:
print("The attack missed!")
approachmsg = ["drew near!", "is in the way!" "attacks!", "approaches...", "wants to fight!", "bumped into you!"]
print(monstername, random.choice(approachmsg))
fighting = True
while fighting:
print("Do you Fight or Run?")
battlecommand = input()
if battlecommand == "Fight" or battlecommand == "fight":
attack()
elif battlecommand == "Run" or battlecommand == "run":
canrun = random.choice([True, False])
slow_type(". . .", 5)
if canrun:
fighting = False
else:
print("couldn't get away...")
else:
print("please type a valid command!")
if playerhp < 1:
print("""
█▀▀▀ █▀▀█ █▀▄▀█ █▀▀ █▀▀█ ▀█░█▀ █▀▀ █▀▀█
█░▀█ █▄▄█ █░▀░█ █▀▀ █░░█ ░█▄█░ █▀▀ █▄▄▀
▀▀▀▀ ▀░░▀ ▀░░░▀ ▀▀▀ ▀▀▀▀ ░░▀░░ ▀▀▀ ▀░▀▀
""")
else:
slow_type("you escaped!", 0.2)
创建的对象:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import "qrc:/script.js" as MyScript
Window {
id: mainWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
id: button
x: 540
y: 0
text: qsTr("Button")
onClicked: MyScript.createRect()
}
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import "qrc:/script.js" as MyScript
Window {
id: mainWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
id: button
x: 540
y: 0
text: qsTr("Button")
onClicked: MyScript.createRect()
}
}
和脚本:
import QtQuick 2.0
Item {
width: 50
height: 50
Drag.active: true
MouseArea {
id: touchArea
anchors.fill: parent
drag.target: parent
}
Rectangle {
id: rectangle
x: 0
y: 0
width: 50
height: 50
color: "#ad0000"
}
}
对象的创建与用户想要的一样多。如何从QML创建的对象中访问属性,然后将它们存储在设置中,以便在程序启动后可以重新创建对象