Python PySide2-将模型绑定到视图
希望有人能帮我,因为我在网上找不到任何有用的东西 我正在为AI项目构建一个简单的GUI,并使用PySide2和QML。我已经设法理解了如何将函数绑定到按钮并使其工作。但我似乎不知道如何从python字符串列表中填充组合框(然后使用python中的选择)。我知道它与属性和模型有关,但我无法让它工作 以下是我的python代码:Python PySide2-将模型绑定到视图,python,qt,qml,pyside2,Python,Qt,Qml,Pyside2,希望有人能帮我,因为我在网上找不到任何有用的东西 我正在为AI项目构建一个简单的GUI,并使用PySide2和QML。我已经设法理解了如何将函数绑定到按钮并使其工作。但我似乎不知道如何从python字符串列表中填充组合框(然后使用python中的选择)。我知道它与属性和模型有关,但我无法让它工作 以下是我的python代码: 将多处理作为mp导入 导入系统 进口米多 从Fuzzy.aidrummer导入aidrummer 从PySide2.QtWidgets导入QApplication 从Py
将多处理作为mp导入
导入系统
进口米多
从Fuzzy.aidrummer导入aidrummer
从PySide2.QtWidgets导入QApplication
从PySide2.QtQuick导入QQuickView
从PySide2.QtCore导入QUrl、插槽、QObject、属性、信号
def run():
d=AiDrummer('playback',file='../Music/28 coltrane 2.mid',play_instrument='yes',instrument_port='VirtualMidSynth#1 0',
输出(端口=罢工3',可视化=真实)
d、 运行()
类Api(QObject):
proc=None
定义初始化(自):
QObject.\uuuuu初始化\uuuuuu(自)
self.\u midi\u out=mido.get\u output\u names()
打印(自打印)
self.\u midi\u in=mido.get\u input\u names()
@槽()
def播放(自我):
self.proc=mp.Process(target=run)
self.proc.start()
@槽()
def停止(自):
尝试:
断言isinstance(self.proc、mp.Process)
self.proc.kill()
除:
返回
def读取midi(自):
返回自我。_midi_out
def设置midi输出(自身,val):
self.\u midi\u out=val
@信号
def midi_out_已更改(自身):
通过
midi\u out=属性(列表、读取midi\u out、设置midi\u out、通知=midi\u out\u更改)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=QApplication([])
视图=QQuickView()
view.setResizeMode(QQuickView.SizerootObject视图)
url=QUrl(“main.qml”)
api=api()
view.setSource(url)
view.rootContext().setContextProperty('api',api)
sys.exit(app.exec_())
和my main.qml(带有模型的组合框靠近底部):
您的代码有以下错误:
- midi_out属性只能读取和通知,因为您无法写入(创建)midi设备,所以不要实现setter
- midi设备的名称仅在开始时获得。如果连接了其他设备?我将不得不关闭并重新打开应用程序,而是添加了允许更新设备名称的“reload()”函数
- 如果根元素是Window或ApplicationWindow,则必须使用QQmlApplicationEngine,如果是项,则必须使用QQuickView
- 如果要将PySide2中的列表导出为属性,则必须使用QVariantList(1)
将多处理导入为mp
进口米多
从Fuzzy.aidrummer导入aidrummer
从PySide2.QtWidgets导入QApplication
从PySide2.QtQml导入QQmlApplicationEngine
从PySide2.QtCore导入QUrl、插槽、QObject、属性、信号
def run():
d=艾德鲁默(
“播放”,
file=“../Music/28 coltrane 2.mid”,
播放乐器=“是”,
instrument_port=“VirtualMidSynth#1 0”,
out\u port=“罢工3”,
形象化=真实,
)
d、 运行()
类Api(QObject):
midi_in_names_Changed=信号()
midi_out_name_Changed=信号()
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.proc=None
self.reload()
@槽()
def重新加载(自):
self._midi_in_names=mido.get_input_names()
self.\u midi\u out\u names=mido.get\u output\u names()
self.midi\u in\u names\u Changed.emit()
self.midi\u out\u name\u Changed.emit()
def get_midi_in_名称(自身):
返回self.\u midi\u在\u名称中
def get_midi_out_名称(自我):
返回自我。_midi_out_name
midi_in_names=属性(
“QVariantList”,fget=get\u midi\u in\u names,notify=midi\u in\u names\u Changed
)
midi_out_name=属性(
“QVariantList”,fget=get\u midi\u out\u names,notify=midi\u out\u names\u Changed
)
@槽()
def播放(自我):
self.proc=mp.Process(target=run)
self.proc.start()
@槽()
def停止(自):
self.proc.kill()
如果名称=“\uuuuu main\uuuuuuuu”:
导入操作系统
导入系统
app=QApplication(sys.argv)
api=api()
引擎=QQmlApplicationEngine()
engine.rootContext().setContextProperty(“api”,api)
current_dir=os.path.dirname(os.path.realpath(uu文件_uu))
url=QUrl.fromLocalFile(os.path.join(当前目录,“main.qml”))
引擎加载(url)
如果不是engine.rootObjects():
系统出口(-1)
sys.exit(app.exec_())
(一)
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.13
import QtQuick.Controls.Material 2.0
Window {
id: window
visible: true
width: 980
height: 700
title: qsTr("AI Drummer")
Rectangle {
id: rectangle1
color: "#191919"
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent
clip: true
rotation: 0
Rectangle {
id: rectangle
x: 711
width: 400
height: 200
color: "#3a3a3a"
anchors.top: parent.top
anchors.topMargin: -33
anchors.right: parent.right
anchors.rightMargin: -131
rotation: 45
clip: true
Material.theme: Material.Dark
Material.accent: Material.DeepOrange
}
RoundButton {
id: playButton
x: 356
y: 632
width: 100
text: "Play"
anchors.bottom: parent.bottom
anchors.bottomMargin: 28
anchors.right: parent.horizontalCenter
anchors.rightMargin: 70
onClicked: { api.play()}
}
RoundButton {
id: stopButton
x: 462
y: 632
width: 100
text: "Stop"
anchors.bottom: parent.bottom
anchors.bottomMargin: 28
anchors.right: parent.horizontalCenter
anchors.rightMargin: -70
onClicked: { api.stop()}
}
ComboBox {
id: instrument_port
x: 214
y: 637
width: 120
height: 30
anchors.right: parent.horizontalCenter
anchors.rightMargin: 176
anchors.bottom: parent.bottom
anchors.bottomMargin: 33
}
ComboBox {
id: out_port
x: 68
y: 637
width: 120
height: 30
anchors.bottomMargin: 33
anchors.right: parent.horizontalCenter
anchors.rightMargin: 302
anchors.bottom: parent.bottom
model: api.midi_out
}
}
Connections {
target: api
}
}
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.13
import QtQuick.Controls.Material 2.0
Window {
id: window
visible: true
width: 980
height: 700
title: qsTr("AI Drummer")
Rectangle {
id: rectangle1
color: "#191919"
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
anchors.fill: parent
clip: true
rotation: 0
Rectangle {
id: rectangle
x: 711
width: 400
height: 200
color: "#3a3a3a"
anchors.top: parent.top
anchors.topMargin: -33
anchors.right: parent.right
anchors.rightMargin: -131
rotation: 45
clip: true
Material.theme: Material.Dark
Material.accent: Material.DeepOrange
}
RoundButton {
id: playButton
x: 356
y: 632
width: 100
text: "Play"
anchors.bottom: parent.bottom
anchors.bottomMargin: 28
anchors.right: parent.horizontalCenter
anchors.rightMargin: 70
onClicked: { api.play()}
}
RoundButton {
id: stopButton
x: 462
y: 632
width: 100
text: "Stop"
anchors.bottom: parent.bottom
anchors.bottomMargin: 28
anchors.right: parent.horizontalCenter
anchors.rightMargin: -70
onClicked: { api.stop()}
}
ComboBox {
id: instrument_port
x: 214
y: 637
width: 120
height: 30
anchors.right: parent.horizontalCenter
anchors.rightMargin: 176
anchors.bottom: parent.bottom
anchors.bottomMargin: 33
}
ComboBox {
id: out_port
x: 68
y: 637
width: 120
height: 30
anchors.bottomMargin: 33
anchors.right: parent.horizontalCenter
anchors.rightMargin: 302
anchors.bottom: parent.bottom
model: api.midi_out_names
}
}
}