首页 > 解决方案 > 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

标签: pythonpyqt5qml

解决方案


您的弹出窗口未打开的原因是因为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 = ""


推荐阅读