python - QML self.emit 启动函数以打开弹出窗口。不工作
问题描述
所以我制作了一个可以正常工作的弹出窗口。但现在我需要该函数等到弹出窗口被填充。所以我开始了一个循环,直到 if 语句!=“空”。但不知何故,弹出窗口不起作用。QML 正在获取应该启动弹出窗口但它没有打开的变量。当 while 循环中断或结束时,它会启动弹出窗口。
主.qml
ApplicationWindow
{
property var openpopup: "" // this prints yes when console.log()
// connectie met de backend van python
Connections
{
target: backend
function onPopupemail(variable)
{ popupemail = variable}
}
}
start_popup.qml
Button
{
onClicked:
{
backend.sendQuery() // this starts the sendQuery function
if(openpopup == "yes"){
popup.open()
}
}
}
Popup
{
id: popup
Button
{
onClicked:
{
popup.close()
backend.updateklantnaam(popupemail.text, klantnieuw.text)
// starts updateklantnaam
}
}
}
Funcy.py
global pauseloop, thread_popupemail, thread_popupname
pauseloop = False
thread_popupemail = ""
thread_popupname = ""
def sendQuery (self)
openpopup = "yes"
self.openpopup.emit(openpopup)
global pauseloop, thread_popupname, thread_popupemail
pauseloop = True
while pauseloop == True:
time.sleep(2)
if thread_popupemail != "" and thread_popupname != "":
cursor.execute "INSERT INTO " #insert query
conn.commit()
thread_popupemail = ""
thread_popupname = ""
pauseloop = False
break
print("break loop")
@pyqtSlot(str, str)
def updateklantnaam (self, popupemail, popupname):
global thread_popupname, thread_popupemail
thread_popupemail = popupemail
thread_popupname = popupname
解决方案
您的弹出窗口未打开的原因是因为sendQuery
在退出 while 循环之前永远不会返回。您正在使用无限循环阻塞主 UI 线程。当 QML 调用后端时,后端应该尽快返回。如果它需要等待某些东西,它应该在一个单独的线程中完成。
但是在您的示例中,我什至根本看不到 while 循环的意义。我会将您的if
语句移入updateklantnaam
函数中,因此根本无需等待。
def sendQuery (self)
openpopup = "yes"
self.openpopup.emit(openpopup)
@pyqtSlot(str, str)
def updateklantnaam (self, popupemail, popupname):
global thread_popupname, thread_popupemail
thread_popupemail = popupemail
thread_popupname = popupname
if thread_popupemail != "" and thread_popupname != "":
cursor.execute "INSERT INTO " #insert query
conn.commit()
thread_popupemail = ""
thread_popupname = ""
推荐阅读
- sql - 如何使用来自多个表的数据在 SQL 中执行条件更新?
- mysql - MS Access 超时获取表
- cassandra - 如何处理磁盘上 Cassandra 中的空目录?
- node.js - 等待异步函数的 while 循环 (Node.js/ES6)
- rename - 当我们可以增加架构中的寄存器数量时,为什么要进行寄存器重命名?
- json - 我的 JSON 例程不再给我数据。为什么?
- php - 使用“退出”后,如何让用户跳转到另一个链接;防止访问用户受限页面?
- node.js - 如何使用 Expressjs 提供更新的 Json 文件?
- pdf - 使用 Forge Design Automation API 导出 Revit 视图以转换为 PDF 的最佳选项是什么?
- python - Mac Catalina pip3 找不到头文件 stdio.h