代码应该获取数据库表中的行数,并将其用作模型数,并且在单击btnShowList后显示ListView上的所有行 但是,单击后不会发生任何事情。有两个文件
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 var numberOfModels: 1
property var listofCust: []
Rectangle{
id: listViewContainer
color: "pink"
anchors.fill: parent
ListModel {
id: displayCustomers
ListElement{
firstName: "First Name"
LastName: "Last Name"
age: "Age"
Sex: "Sex"
}
}
Component{
id:customersDelegate
Row{
spacing: 50
Text{
text: firstName
}
Text {
text: LastName
}
Text {
text: age
}
Text {
text: Sex
}
}
}
ListView{
anchors.fill: parent
anchors.bottomMargin: 52
model: displayCustomers
delegate: customersDelegate
}
Button {
id: btnShowList
x: 518
y: 440
text: qsTr("show list")
onClicked: {
displayCustomers.append(backend.getCount)
}
}
}
Connections {
target: backend
function onGetCount(total, listofCust){
count = total
numberOfModels = total
listofCus = listofCust
return listofCust
}
}
}
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)
count = Signal(int)
listOfCus = Signal([])
@Slot()
def getCount(self):
db = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "",
database = "test"
)
mycursor = db.cursor()
mycursor.execute("SELECT * FROM customer")
listofCustomers = []
total = 0
for x in mycursor:
total = total + 1
print(x)
listofCustomers.append(x)
self.count.emit(total)
print(total)
values = mycursor
print(values)
print(listofCustomers)
self.listOfCus.emit()
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_())
执行时没有错误。当我打印客户名单和总数时,我得到了预期的输出。但单击按钮后,listView不会显示任何内容
OP希望数据库信息神奇地显示在视图中,而不必至少将该信息发送到.qml
另一方面,“displayCustomers.append(backend.getCount)”一行令人困惑,您打算如何处理该代码?在该代码中,getCount函数被添加到模型中,这显然是不合逻辑的
这个想法是要有一个模型(在python或QML中),该模型的角色与委托使用的角色相同,插槽应该只用于填充模型,在本例中,我将使用python模型
main.qml
我想你已经混淆了什么是模型和代理。您的模型应该包含要显示的数据。您的代理定义了数据的显示方式。在您的示例中,您的委托不是可视项。看起来它正在检索数据,这些数据应该进入模型。下面是它应该是什么样子的示例:
相关问题 更多 >
编程相关推荐