QML self.emit 启动功能以打开弹出窗口。不起作用

QML self.emit to start function to open popup. Does not work

所以我制作了一个可以正常工作的弹出窗口。但现在我需要函数等待弹出窗口被填充。所以我开始了一个 while 循环,循环直到 if 语句!=“空”。但不知何故,弹出窗口不起作用。 QML 正在获取应启动弹出窗口但未打开的变量。它在 while 循环中断或结束时启动弹出窗口。

Main.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 循环之前从未 returns。您正在用无限循环阻塞主 UI 线程。当QML调用到后端时,后端应该尽快return。如果需要等待,应该在单独的线程中完成。

但是在你的例子中,我什至没有看到 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 = ""