从 qml 中的组合框发送字符串信号
Send string signal from combobox in qml
我正在尝试将字符串从组合框发送到 api。很遗憾,没有发送任何内容。
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
id: mainWindow
width: 1120
height: 680
visible: true
Rectangle {
id: mainBody
color: "#F4F4F5"
anchors.fill: parent
ComboBox {
id: comboBox
x: 219
y: -9
width: 504
height: 40
model: [ "hp", "lenovo", "acer"]
}
Button {
id: bt
width: 24
height: 24
onClicked: backend.add(comboBox.text)
}
}
Connections {
target: backend
}
}
main.py
# This Python file uses the following encoding: utf-8
import os
# from pathlib import Path
import sys
import json
import requests
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import Slot, Signal, QObject
class MainWindow(QObject):
def __init__(self):
QObject.__init__(self)
signalcombo = Signal(str)
@Slot(str, str, str)
def add(self, getCombo):
putdata1 = {"category_name": getCombo}
data1 =
requests.post("random api", json=putdata1)
print(data1.text)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
# engine.load(os.fspath(Path(__file__).resolve().parent / "qml/main.qml"))
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
# Get Context
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
现在的问题是当打印 data1.text 时显示“插入成功”,但数据库中没有任何内容甚至空白 space。没有数据正在发送。谁能帮我。此方法适用于 textField 但不适用于组合框。
您的代码存在各种问题。
首先,add
的插槽装饰器参数数量错误。在 QML 中,您只用一个参数调用它,但插槽有三个。将其更改为:
@Slot(str)
def add(self, getCombo):
# ...
然后,ComboBox
没有属性命名为text
,而是currentText
(or any other valid property available for it,如displayText
).
Button {
id: bt
width: 24
height: 24
onClicked: backend.add(comboBox.currentText)
}
最后,虽然不重要,但您应该先设置上下文,然后然后加载QML:
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
我正在尝试将字符串从组合框发送到 api。很遗憾,没有发送任何内容。
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
id: mainWindow
width: 1120
height: 680
visible: true
Rectangle {
id: mainBody
color: "#F4F4F5"
anchors.fill: parent
ComboBox {
id: comboBox
x: 219
y: -9
width: 504
height: 40
model: [ "hp", "lenovo", "acer"]
}
Button {
id: bt
width: 24
height: 24
onClicked: backend.add(comboBox.text)
}
}
Connections {
target: backend
}
}
main.py
# This Python file uses the following encoding: utf-8
import os
# from pathlib import Path
import sys
import json
import requests
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import Slot, Signal, QObject
class MainWindow(QObject):
def __init__(self):
QObject.__init__(self)
signalcombo = Signal(str)
@Slot(str, str, str)
def add(self, getCombo):
putdata1 = {"category_name": getCombo}
data1 =
requests.post("random api", json=putdata1)
print(data1.text)
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
# engine.load(os.fspath(Path(__file__).resolve().parent / "qml/main.qml"))
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
# Get Context
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
现在的问题是当打印 data1.text 时显示“插入成功”,但数据库中没有任何内容甚至空白 space。没有数据正在发送。谁能帮我。此方法适用于 textField 但不适用于组合框。
您的代码存在各种问题。
首先,add
的插槽装饰器参数数量错误。在 QML 中,您只用一个参数调用它,但插槽有三个。将其更改为:
@Slot(str)
def add(self, getCombo):
# ...
然后,ComboBox
没有属性命名为text
,而是currentText
(or any other valid property available for it,如displayText
).
Button {
id: bt
width: 24
height: 24
onClicked: backend.add(comboBox.currentText)
}
最后,虽然不重要,但您应该先设置上下文,然后然后加载QML:
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))