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
-
@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 goingdel
. Python maintains its own reference count to objects, and only deletes them when that reaches 0. Having, say, aQDialog
with aconnect()
to a class-member method does allow the dialog to be deleted when the referencing variable goes out of scope/assignNone
/usedel
. However, if youconnect()
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 aQDialog
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. -
@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 :-)
-
@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.
-
@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_())
-
@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.
-
@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. -
@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 duplicateconnnect()
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.... -
@jsulm I didn't know that, it's my first time here.
Thanks, I'll do it! -
@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 duplicateconnnect()
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:
thank you so much!!!
-
@andre_sophia
Glad it solved. Yes, in the normal course do all yourconnect()
s in the__init__()
, after thesetupUi()
, so it's just once.