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

Question:

So I have made a popup that works properly. but now I need the function to wait until the popup is filled in. So I started a while loop that loops until the if statement != "empty". But somehow the popup is not working. QML is getting the variable that should start the popup but it’s not opening. It’s starting the popup when the while loop breaks or when it ends.

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
Asked By: Minimumspace

||

Answers:

The reason your popup doesn’t open is because sendQuery never returns until it breaks out of the while loop. You’re blocking the main UI thread with an endless loop. When the QML calls into the backend, the backend ought to return as soon as possible. If it needs to wait for something, it should be done in a separate thread.

But in your example, I don’t even see the point of the while loop at all. I would move your if statement into the updateklantnaam function, so there’s no waiting at all.

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 = ""

Answered By: JarMan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.