如何将数据从堆栈视图中的 qml 文件传递到数据库中的 table
How do you pass data from qml file in a stack view to a table in database
有四个文件; main.qml 文件,其中有显示 addNew.qml 文件作为初始项目的堆栈视图,并包含两个按钮,即后退和下一个。单击下一步按钮后,文本字段中的文本应插入到数据库 table 客户中。但是,当数据被插入到字段中并单击下一步按钮时,没有任何内容被插入到数据库中。
addNew.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
Rectangle {
id: rectangle
color: "#5b5858"
anchors.fill: parent
Label {
id: label
x: 42
y: 49
width: 105
height: 36
color: "#ffffff"
text: qsTr("Firstname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label1
x: 42
y: 116
width: 105
height: 36
color: "#ffffff"
text: qsTr("Lastname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label2
x: 42
y: 185
width: 105
height: 36
color: "#ffffff"
text: qsTr("Age")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label3
x: 42
y: 241
width: 105
height: 36
color: "#ffffff"
text: qsTr("Sex")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
TextInput {
id: txtfname
x: 203
y: 56
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtlname
x: 203
y: 123
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtage
x: 203
y: 192
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
RadioButton {
id: isMale
x: 223
y: 247
width: 112
height: 40
text: qsTr("Male")
}
RadioButton {
id: isFemale
x: 224
y: 291
width: 112
height: 40
text: qsTr("Female")
}
}
Connections{
target: backend
function onAddPrimaryData(fname, lname, age, ismale) {
txtfname.text = fname
txtlname.txt = lname
txtage.text =age
if (isMale.checked === true){
ismale = true
}
if (isFemale.checked === true){
ismale = false
}
}
}
}
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
property string page: "addNew.qml"
Rectangle {
id: rectangle
color: "#212121"
anchors.fill: parent
Button {
id: button
x: 389
y: 414
text: qsTr("Back")
}
Button {
id: button1
x: 516
y: 414
text: qsTr("Next")
onClicked:{
if (page === "addNew.qml"){
stackView.push("addNewEmpData.qml")
}
backend.addNewPrimaryData
}
}
StackView {
id: stackView
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: button.top
anchors.rightMargin: 10
anchors.leftMargin: 10
anchors.topMargin: 10
anchors.bottomMargin: 10
initialItem: Qt.resolvedUrl(page)
}
}
}
/*##^##
Designer {
D{i:0;formeditorZoom:0.66}D{i:3}D{i:4}D{i:1}
}
##^##*/
main.py
import sys
import os
import mysql.connector
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, Signal
class MainWindow(QObject):
def __init__(self):
QObject.__init__(self)
@Slot(str, str, int, bool)
def addPrimaryData(self, firstName, lastName, age, isMale):
db = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "",
database = "test"
)
mycursor = db.cursor(buffered =True)
sex = 'M'
if isMale == True:
sex = 'M'
else:
sex = 'F'
mycursor.execute("INSERT INTO customer VALUES(%s, %s, %s, %s", (firstName, lastName, age, sex ))
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
#Get Context
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
我要列出一些错误:
- 使用 Connections 用于侦听来自 QObject 的信号,而不是调用槽。
- addNewPrimaryData 未定义。
- 代码
page === "addNew.qml"
永远为真。
逻辑是在页面“addNew.qml”上创建一个方法,当按钮被按下时调用它,可以通过currentItem属性获取item。最好在每个页面的根部添加一个名称或标识符作为 属性 以识别它们。
addNew.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
readonly property string identifier: "addNew"
Rectangle {
id: rectangle
color: "#5b5858"
anchors.fill: parent
Label {
id: label
x: 42
y: 49
width: 105
height: 36
color: "#ffffff"
text: qsTr("Firstname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label1
x: 42
y: 116
width: 105
height: 36
color: "#ffffff"
text: qsTr("Lastname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label2
x: 42
y: 185
width: 105
height: 36
color: "#ffffff"
text: qsTr("Age")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label3
x: 42
y: 241
width: 105
height: 36
color: "#ffffff"
text: qsTr("Sex")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
TextInput {
id: txtfname
x: 203
y: 56
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtlname
x: 203
y: 123
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtage
x: 203
y: 192
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
RadioButton {
id: isMale
x: 223
y: 247
width: 112
height: 40
checked: true
text: qsTr("Male")
}
RadioButton {
id: isFemale
x: 224
y: 291
width: 112
height: 40
text: qsTr("Female")
}
}
function save() {
var ismale = true;
if (isMale.checked)
ismale = true;
if (isFemale.checked)
ismale = false;
backend.addPrimaryData(txtfname.text, txtlname.text, parseInt(txtage.text), ismale)
}
}
main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
id: rectangle
color: "#212121"
anchors.fill: parent
Button {
id: button
x: 389
y: 414
text: qsTr("Back")
onClicked: {
if (stackView.depth > 0)
stackView.pop();
}
}
Button {
id: button1
x: 516
y: 414
text: qsTr("Next")
onClicked: {
if (stackView.currentItem.identifier === "addNew") {
stackView.currentItem.save();
stackView.push("addNewEmpData.qml");
}
}
}
StackView {
id: stackView
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: button.top
anchors.rightMargin: 10
anchors.leftMargin: 10
anchors.topMargin: 10
anchors.bottomMargin: 10
initialItem: Qt.resolvedUrl("addNew.qml")
}
}
}
有四个文件; main.qml 文件,其中有显示 addNew.qml 文件作为初始项目的堆栈视图,并包含两个按钮,即后退和下一个。单击下一步按钮后,文本字段中的文本应插入到数据库 table 客户中。但是,当数据被插入到字段中并单击下一步按钮时,没有任何内容被插入到数据库中。
addNew.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
Rectangle {
id: rectangle
color: "#5b5858"
anchors.fill: parent
Label {
id: label
x: 42
y: 49
width: 105
height: 36
color: "#ffffff"
text: qsTr("Firstname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label1
x: 42
y: 116
width: 105
height: 36
color: "#ffffff"
text: qsTr("Lastname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label2
x: 42
y: 185
width: 105
height: 36
color: "#ffffff"
text: qsTr("Age")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label3
x: 42
y: 241
width: 105
height: 36
color: "#ffffff"
text: qsTr("Sex")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
TextInput {
id: txtfname
x: 203
y: 56
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtlname
x: 203
y: 123
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtage
x: 203
y: 192
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
RadioButton {
id: isMale
x: 223
y: 247
width: 112
height: 40
text: qsTr("Male")
}
RadioButton {
id: isFemale
x: 224
y: 291
width: 112
height: 40
text: qsTr("Female")
}
}
Connections{
target: backend
function onAddPrimaryData(fname, lname, age, ismale) {
txtfname.text = fname
txtlname.txt = lname
txtage.text =age
if (isMale.checked === true){
ismale = true
}
if (isFemale.checked === true){
ismale = false
}
}
}
}
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
property string page: "addNew.qml"
Rectangle {
id: rectangle
color: "#212121"
anchors.fill: parent
Button {
id: button
x: 389
y: 414
text: qsTr("Back")
}
Button {
id: button1
x: 516
y: 414
text: qsTr("Next")
onClicked:{
if (page === "addNew.qml"){
stackView.push("addNewEmpData.qml")
}
backend.addNewPrimaryData
}
}
StackView {
id: stackView
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: button.top
anchors.rightMargin: 10
anchors.leftMargin: 10
anchors.topMargin: 10
anchors.bottomMargin: 10
initialItem: Qt.resolvedUrl(page)
}
}
}
/*##^##
Designer {
D{i:0;formeditorZoom:0.66}D{i:3}D{i:4}D{i:1}
}
##^##*/
main.py
import sys
import os
import mysql.connector
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, Signal
class MainWindow(QObject):
def __init__(self):
QObject.__init__(self)
@Slot(str, str, int, bool)
def addPrimaryData(self, firstName, lastName, age, isMale):
db = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "",
database = "test"
)
mycursor = db.cursor(buffered =True)
sex = 'M'
if isMale == True:
sex = 'M'
else:
sex = 'F'
mycursor.execute("INSERT INTO customer VALUES(%s, %s, %s, %s", (firstName, lastName, age, sex ))
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
#Get Context
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
我要列出一些错误:
- 使用 Connections 用于侦听来自 QObject 的信号,而不是调用槽。
- addNewPrimaryData 未定义。
- 代码
page === "addNew.qml"
永远为真。
逻辑是在页面“addNew.qml”上创建一个方法,当按钮被按下时调用它,可以通过currentItem属性获取item。最好在每个页面的根部添加一个名称或标识符作为 属性 以识别它们。
addNew.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
Item {
readonly property string identifier: "addNew"
Rectangle {
id: rectangle
color: "#5b5858"
anchors.fill: parent
Label {
id: label
x: 42
y: 49
width: 105
height: 36
color: "#ffffff"
text: qsTr("Firstname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label1
x: 42
y: 116
width: 105
height: 36
color: "#ffffff"
text: qsTr("Lastname")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label2
x: 42
y: 185
width: 105
height: 36
color: "#ffffff"
text: qsTr("Age")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
Label {
id: label3
x: 42
y: 241
width: 105
height: 36
color: "#ffffff"
text: qsTr("Sex")
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 14
}
TextInput {
id: txtfname
x: 203
y: 56
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtlname
x: 203
y: 123
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
TextInput {
id: txtage
x: 203
y: 192
width: 149
height: 22
color: "#fdfdfd"
text: qsTr("")
font.pixelSize: 12
}
RadioButton {
id: isMale
x: 223
y: 247
width: 112
height: 40
checked: true
text: qsTr("Male")
}
RadioButton {
id: isFemale
x: 224
y: 291
width: 112
height: 40
text: qsTr("Female")
}
}
function save() {
var ismale = true;
if (isMale.checked)
ismale = true;
if (isFemale.checked)
ismale = false;
backend.addPrimaryData(txtfname.text, txtlname.text, parseInt(txtage.text), ismale)
}
}
main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
id: rectangle
color: "#212121"
anchors.fill: parent
Button {
id: button
x: 389
y: 414
text: qsTr("Back")
onClicked: {
if (stackView.depth > 0)
stackView.pop();
}
}
Button {
id: button1
x: 516
y: 414
text: qsTr("Next")
onClicked: {
if (stackView.currentItem.identifier === "addNew") {
stackView.currentItem.save();
stackView.push("addNewEmpData.qml");
}
}
}
StackView {
id: stackView
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: button.top
anchors.rightMargin: 10
anchors.leftMargin: 10
anchors.topMargin: 10
anchors.bottomMargin: 10
initialItem: Qt.resolvedUrl("addNew.qml")
}
}
}