Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay

    Qt for Python
    qt for python python pyside2
    3
    16
    491
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      andre_sophia last edited by andre_sophia

      Hello,

      When I use my app for some time, opening and closing many windows, the open time for each window is longer.
      One thing I note is, the memory used becames bigger for each window opened.
      594ef9cf-b918-4dfd-b587-4dac0e914cfe-image.png

      I think one code, capable to clear this memory could be usefull, or some other solution.

      Someone could help me please? Thank you

      jsulm JonB 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @andre_sophia last edited by JonB

        @andre_sophia
        One thing: I think you are re-connect()-ing all the time, setting up slots on signals again & again.

        E.g.

        def Menu_Cadastro_Func():
        	Menu_Cadastro.pushButton.clicked.connect(Menu_Principal_Func)
        
        def Menu_Principal_Func():
        	Menu_Principal.pushButton_3.clicked.connect(Menu_Cadastro_Func)
        

        Since you are closing (but not destroying/getting rid of) these windows all the time, the connect() will build up. ( @jsulm will correct me if I'm wrong, but I do not think Qt checks for an exact duplicate connnect() and skips it, I think it adds it again.) If that's right, then I think you start to show/hide (the same) windows multiple times the more you click around....

        A 1 Reply Last reply Reply Quote 1
        • jsulm
          jsulm Lifetime Qt Champion @andre_sophia last edited by

          @andre_sophia said in Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay:

          I think one code, capable to clear this memory could be usefull

          Well, it sounds like memory leak. Use a tool to analyse memory consumption of your app.
          With the information you provided it is impossible to tell you what exactly the problem is.
          How many windows do you open?
          What is your app doing?
          What does each window do?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply Reply Quote 1
          • JonB
            JonB @andre_sophia last edited by JonB

            @andre_sophia
            As @jsulm has said/asked.
            My guess: you are opening windows without the old ones being properly disposed/freed, so the memory gets worse.
            Make sure your Python code is not keeping references to closed windows. Consider using Qt::WA_DeleteOnClose flag. Use QApplication.allWidgets()/QApplication.topLevelWidgets() to look through the widgets which are still in the application to see if there are old, closed ones you would not expect to be around any longer.

            A 1 Reply Last reply Reply Quote 1
            • A
              andre_sophia @jsulm last edited by

              @jsulm Thanks for your help, The app has 13 windows, but when I open one, I close the other, so always with only one window open.

              At this moment, my windows don't have functionality, only buttons connections (to to navigate between windows) as below:

              b993bc94-649f-44ee-bad6-1e7af42e4e0e-image.png

              When I navigate for 10 windows (opening and closing them, without close the app) the delay begin, and only increase.

              If you want to know something more, please reply me.
              Thank you.

              JonB 1 Reply Last reply Reply Quote 0
              • A
                andre_sophia @JonB last edited by

                @JonB Thanks for your help. I always close a window to open another one, but the memory does't decrease.
                Could you please send me an example of how I can use the Qt::WA_DeleteOnClose in Python?

                Thank you so much!

                jsulm 1 Reply Last reply Reply Quote 0
                • jsulm
                  jsulm Lifetime Qt Champion @andre_sophia last edited by

                  @andre_sophia said in Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay:

                  Could you please send me an example of how I can use the Qt::WA_DeleteOnClose in Python?

                  See https://stackoverflow.com/questions/32477930/qtwa-deleteonclose - is for C++, but works same with Python. Also you can delete the window manually: assign None to the variable holding the window object or use del.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @jsulm last edited by

                    @jsulm
                    Hi. I believe you are a native C++-er, not Pythonista. Be aware of the following nasty for Python + Qt. I will keep it brief here, no example, as I don't suppose it applies to this OP's current situation.

                    In Python you cannot delete an object, assigning None does not do it, and nor does going del. Python maintains its own reference count to objects, and only deletes them when that reaches 0. Having, say, a QDialog with a connect() to a class-member method does allow the dialog to be deleted when the referencing variable goes out of scope/assign None/use del. However, if you connect() to a Python lambda for the slot --- as we all often do --- this creates a "dangling" reference to it which Python never collects and keeps the reference count > 0. Such a QDialog will never be deleted!

                    Consequently, I set Qt.WA_DeleteOnClose on my Python dialogs as the only way to achieve deletion in such a case.

                    jsulm 1 Reply Last reply Reply Quote 2
                    • jsulm
                      jsulm Lifetime Qt Champion @JonB last edited by

                      @JonB said in Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay:

                      Python maintains its own reference count to objects, and only deletes them when that reaches 0

                      Yes, I know. But didn't think about lambdas :-)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply Reply Quote 0
                      • JonB
                        JonB @andre_sophia last edited by

                        @andre_sophia said in Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay:

                        but when I open one, I close the other
                        When I navigate for 10 windows (opening and closing them, without close the app) the delay begin, and only increase.
                        If you want to know something more, please reply me.

                        Just closing windows does not necessarily delete them. We would need to see your code. Just how many times do you hit a line creating a new window?

                        Having said that: if you are only going through your code a few times, even if it "leaks" windows I would not expect it to get noticeably slower till you had done this a lot of times. So again, we need to know what your code is doing.

                        A 1 Reply Last reply Reply Quote 1
                        • A
                          andre_sophia @JonB last edited by jsulm

                          @JonB my main code below:

                          # -*- coding: utf-8 -*- --noconsole
                          
                          import sys
                          from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
                              QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
                          from PySide2 import QtCore, QtGui, QtWidgets
                          from Av_Audicao import Ui_Av_Audicao
                          from Av_Fala import Ui_Av_Fala
                          from Av_Linguagem import Ui_Av_Linguagem
                          from Av_Voz import Ui_Av_Voz
                          from Cadastro_Paciente import Ui_Cadastro_Paciente
                          from Consulta import Ui_Consulta
                          from Editar_Paciente import Ui_Editar_Paciente
                          from Fatores_Desencadeantes import Ui_Fatores_Desencadeantes
                          from Fatores_Relevantes import Ui_Fatores_Relevantes
                          from Menu_Cadastro import Ui_Menu_Cadastro
                          from Menu_Principal import Ui_Menu_Principal
                          from Menu_Triagem import Ui_Menu_Triagem
                          from Queixas import Ui_Queixas
                          import time
                          
                          
                          '''Declaração das telas/Classes'''
                          
                          class Av_Audicao(QtWidgets.QMainWindow, Ui_Av_Audicao):
                              def __init__(self):
                                  super(Av_Audicao, self).__init__()
                                  self.setupUi(self)
                          
                          class Av_Fala(QtWidgets.QMainWindow, Ui_Av_Fala):
                              def __init__(self):
                                  super(Av_Fala, self).__init__()
                                  self.setupUi(self)
                          
                          class Av_Linguagem(QtWidgets.QMainWindow, Ui_Av_Linguagem):
                              def __init__(self):
                                  super(Av_Linguagem, self).__init__()
                                  self.setupUi(self)
                          
                          class Av_Voz(QtWidgets.QMainWindow, Ui_Av_Voz):
                              def __init__(self):
                                  super(Av_Voz, self).__init__()
                                  self.setupUi(self)
                          
                          class Cadastro_Paciente(QtWidgets.QMainWindow, Ui_Cadastro_Paciente):
                              def __init__(self):
                                  super(Cadastro_Paciente, self).__init__()
                                  self.setupUi(self)
                          
                          class Consulta(QtWidgets.QMainWindow, Ui_Consulta):
                              def __init__(self):
                                  super(Consulta, self).__init__()
                                  self.setupUi(self)
                          
                          class Editar_Paciente(QtWidgets.QMainWindow, Ui_Editar_Paciente):
                              def __init__(self):
                                  super(Editar_Paciente, self).__init__()
                                  self.setupUi(self)
                          
                          class Fatores_Desencadeantes(QtWidgets.QMainWindow, Ui_Fatores_Desencadeantes):
                              def __init__(self):
                                  super(Fatores_Desencadeantes, self).__init__()
                                  self.setupUi(self)
                          
                          class Fatores_Relevantes(QtWidgets.QMainWindow, Ui_Fatores_Relevantes):
                              def __init__(self):
                                  super(Fatores_Relevantes, self).__init__()
                                  self.setupUi(self)
                          
                          class Menu_Cadastro(QtWidgets.QMainWindow, Ui_Menu_Cadastro):
                              def __init__(self):
                                  super(Menu_Cadastro, self).__init__()
                                  self.setupUi(self)
                          
                          class Menu_Principal(QtWidgets.QMainWindow, Ui_Menu_Principal):
                              def __init__(self):
                                  super(Menu_Principal, self).__init__()
                                  self.setupUi(self)
                          
                          class Menu_Triagem(QtWidgets.QMainWindow, Ui_Menu_Triagem):
                              def __init__(self):
                                  super(Menu_Triagem, self).__init__()
                                  self.setupUi(self)
                          
                          class Queixas(QtWidgets.QMainWindow, Ui_Queixas):
                              def __init__(self):
                                  super(Queixas, self).__init__()
                                  self.setupUi(self)
                          
                          
                          ''' FUNÇÕES UTILITÁRIAS'''
                          
                          
                          
                          
                          
                          ''' FUNÇÕES DAS TELAS'''
                          
                          def Fatores_Relevantes_Func():
                          	Fatores_Relevantes.showMaximized()
                          
                          	Fatores_Relevantes.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          def Fatores_Desencadeantes_Func():
                          	Fatores_Desencadeantes.showMaximized()
                          
                          	Fatores_Desencadeantes.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          def Queixas_Func():
                          	Queixas.showMaximized()
                          
                          	Queixas.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          def Av_Voz_Func():
                          	Av_Voz.showMaximized()
                          
                          	Av_Voz.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          def Av_Fala_Func():
                          	Av_Fala.showMaximized()
                          
                          	Av_Fala.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          
                          def Av_Linguagem_Func():
                          	Av_Linguagem.showMaximized()
                          
                          	Av_Linguagem.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          
                          def Av_Audicao_Func():
                          	Av_Audicao.showMaximized()
                          
                          	Av_Audicao.pushButton.clicked.connect(Menu_Triagem_Func)
                          
                          def Menu_Triagem_Func():
                          	Menu_Triagem.showMaximized()
                          	Menu_Principal.close()
                          	Fatores_Relevantes.close()
                          	Fatores_Desencadeantes.close()
                          	Queixas.close()
                          	Av_Voz.close()
                          	Av_Fala.close()
                          	Av_Linguagem.close()
                          	Av_Audicao.close()
                          
                          
                          	Menu_Triagem.pushButton.clicked.connect(Menu_Principal_Func)
                          
                          	Menu_Triagem.pushButton_10.clicked.connect(Fatores_Relevantes_Func)
                          	Menu_Triagem.pushButton_12.clicked.connect(Fatores_Desencadeantes_Func)
                          	Menu_Triagem.pushButton_11.clicked.connect(Queixas_Func)
                          	Menu_Triagem.pushButton_6.clicked.connect(Av_Voz_Func)
                          	Menu_Triagem.pushButton_7.clicked.connect(Av_Fala_Func)
                          	Menu_Triagem.pushButton_8.clicked.connect(Av_Linguagem_Func)
                          	Menu_Triagem.pushButton_9.clicked.connect(Av_Audicao_Func)
                          
                          
                          
                          
                          
                          def Consulta_Func():
                          	Consulta.showMaximized()
                          	Menu_Principal.close()
                          
                          	Consulta.pushButton_4.clicked.connect(Menu_Principal_Func)
                          
                          def Criar_Paciente_Func():
                          	Cadastro_Paciente.showMaximized()
                          	Menu_Cadastro.close()
                          
                          	Cadastro_Paciente.pushButton_4.clicked.connect(Menu_Cadastro_Func)
                          
                          
                          def Editar_Paciente_Func():# Buscar pacientes e abrir formulário de cadastro com campos já preenchidos
                          	Menu_Cadastro.close()
                          	Editar_Paciente.showMaximized()	
                          
                          	Editar_Paciente.pushButton_4.clicked.connect(Menu_Cadastro_Func)
                          
                          def Menu_Cadastro_Func():
                          	Editar_Paciente.close()
                          	Cadastro_Paciente.close()
                          	Menu_Cadastro.showMaximized()
                          	Menu_Principal.close()
                          
                          	Menu_Cadastro.pushButton.clicked.connect(Menu_Principal_Func)
                          	Menu_Cadastro.pushButton_2.clicked.connect(Editar_Paciente_Func)
                          	Menu_Cadastro.pushButton_3.clicked.connect(Criar_Paciente_Func)
                          
                          
                          def Menu_Principal_Func():
                          	Menu_Cadastro.close()
                          	Menu_Triagem.close()
                          	Consulta.close()
                          
                          	Menu_Principal.showMaximized()
                          
                          	Menu_Principal.pushButton_3.clicked.connect(Menu_Cadastro_Func)
                          	Menu_Principal.pushButton.clicked.connect(Menu_Triagem_Func)
                          	Menu_Principal.pushButton_2.clicked.connect(Consulta_Func)
                          
                          
                          
                          
                          
                          
                          
                          
                          if __name__ == "__main__":
                              app = QtWidgets.QApplication(sys.argv)
                          
                              '''instância das classes'''
                          
                              Av_Audicao = Av_Audicao()
                              Av_Fala = Av_Fala()
                              Av_Linguagem = Av_Linguagem()
                              Av_Voz = Av_Voz()
                              Cadastro_Paciente = Cadastro_Paciente()
                              Consulta = Consulta()
                              Editar_Paciente = Editar_Paciente()
                              Fatores_Desencadeantes = Fatores_Desencadeantes()
                              Fatores_Relevantes = Fatores_Relevantes()
                              Menu_Cadastro = Menu_Cadastro()
                              Menu_Principal = Menu_Principal()
                              Menu_Triagem = Menu_Triagem()
                              Queixas = Queixas()
                              
                              '''Início do programa'''
                              Menu_Principal_Func()
                          
                              sys.exit(app.exec_())
                          jsulm JonB 3 Replies Last reply Reply Quote 0
                          • jsulm
                            jsulm Lifetime Qt Champion @andre_sophia last edited by

                            @andre_sophia Please, next time make sure you mark your code as code properly to help others to read and understand your code. I fixed it this time for you.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            A 1 Reply Last reply Reply Quote 0
                            • jsulm
                              jsulm Lifetime Qt Champion @andre_sophia last edited by

                              @andre_sophia said in Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay:

                              Cadastro_Paciente = Cadastro_Paciente()
                              Consulta = Consulta()
                              Editar_Paciente = Editar_Paciente()
                              Fatores_Desencadeantes = Fatores_Desencadeantes()
                              Fatores_Relevantes = Fatores_Relevantes()
                              Menu_Cadastro = Menu_Cadastro()
                              Menu_Principal = Menu_Principal()
                              Menu_Triagem = Menu_Triagem()
                              Queixas = Queixas()

                              Well, you create and keep your windows all the time.
                              You should only create a window if you want to show it. And use Qt::WA_DeleteOnClose as @JonB suggested.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply Reply Quote 1
                              • JonB
                                JonB @andre_sophia last edited by JonB

                                @andre_sophia
                                One thing: I think you are re-connect()-ing all the time, setting up slots on signals again & again.

                                E.g.

                                def Menu_Cadastro_Func():
                                	Menu_Cadastro.pushButton.clicked.connect(Menu_Principal_Func)
                                
                                def Menu_Principal_Func():
                                	Menu_Principal.pushButton_3.clicked.connect(Menu_Cadastro_Func)
                                

                                Since you are closing (but not destroying/getting rid of) these windows all the time, the connect() will build up. ( @jsulm will correct me if I'm wrong, but I do not think Qt checks for an exact duplicate connnect() and skips it, I think it adds it again.) If that's right, then I think you start to show/hide (the same) windows multiple times the more you click around....

                                A 1 Reply Last reply Reply Quote 1
                                • A
                                  andre_sophia @jsulm last edited by

                                  @jsulm I didn't know that, it's my first time here.
                                  Thanks, I'll do it!

                                  1 Reply Last reply Reply Quote 0
                                  • A
                                    andre_sophia @JonB last edited by

                                    @JonB said in Delay to open window after use my desktop app(open and close many windows), when more windows I open, larger will be the delay:

                                    @andre_sophia
                                    One thing: I think you are re-connect()-ing all the time, setting up slots on signals again & again.

                                    E.g.

                                    def Menu_Cadastro_Func():
                                    	Menu_Cadastro.pushButton.clicked.connect(Menu_Principal_Func)
                                    
                                    def Menu_Principal_Func():
                                    	Menu_Principal.pushButton_3.clicked.connect(Menu_Cadastro_Func)
                                    

                                    Since you are closing (but not destroying/getting rid of) these windows all the time, the connect() will build up. ( @jsulm will correct me if I'm wrong, but I do not think Qt checks for an exact duplicate connnect() and skips it, I think it adds it again.) If that's right, then I think you start to show/hide (the same) windows multiple times the more you click around....

                                    The problema was exatly That @JonB, Now I conncted the buttons only once, in the class definition, as below:
                                    bacd6d39-b6cf-49de-8f3e-de68bd9e4772-image.png

                                    thank you so much!!!

                                    JonB 1 Reply Last reply Reply Quote 1
                                    • JonB
                                      JonB @andre_sophia last edited by JonB

                                      @andre_sophia
                                      Glad it solved. Yes, in the normal course do all your connect()s in the __init__(), after the setupUi(), so it's just once.

                                      1 Reply Last reply Reply Quote 1
                                      • First post
                                        Last post