简体   繁体   English

Pyqt5崩溃,没有错误,特别是我双击鼠标左键(双击鼠标右键起作用)

[英]Pyqt5 Crashes without error when i specifically double left click (double right works)

I'm creating this application in Python that talks with Outlook and retrieve some data about the amount of mails in each folder and some other stuff and I'm using PyQt5 to do so. 我正在用Python创建该应用程序,该应用程序与Outlook进行通讯,并检索有关每个文件夹中的邮件数量以及其他内容的一些数据,而我使用的是PyQt5。

But some werid thing happened when I assigned a signal to a function: 但是当我给函数分配信号时,发生了一些奇怪的事情:

self.table_widget.itemDoubleClicked.connect(self.some_function)

If I double right-click this item, everything runs just fine. 如果我右键单击此项目,一切运行正常。 But if I do it with the left-click , it just freezes, and then crashes without any error screen whatsoever (usually if you execute the program, when it crashes you can see some stack on the console behind, right? In this case literally nothing shows up). 但是,如果我用左键单击它,它会冻结,然后崩溃而没有任何错误屏幕(通常,如果您执行该程序,当它崩溃时,您可以在后面的控制台上看到一些堆栈,对吗?在这种情况下,字面意思是什么都没有显示)。

I'm not sure if I'm using he signals thing right, so there might be something...? 我不确定我是否正在使用他表示正确的信号,所以可能有什么...?

Anyways, since no error occurred, I tried to put some prints to see where it would crash, but it turns out that the t2 screen loads just fine, and apparently it crashes when the code goes back to the main loop...? 无论如何,由于没有发生错误,所以我尝试放置一些打印件以查看崩溃的地方,但是事实证明t2屏幕加载的很好,并且当代码返回主循环时显然崩溃了……? I don't know, it's impossible for me to debug that stuff. 我不知道,我无法调试这些东西。

I'll try to put here only the essential code so it gets easier to see the mistake, but I can show some details if this is not enough. 我将仅在此处放置基本代码,以便更容易发现错误,但如果不够,我可以显示一些详细信息。

EDIT 编辑

As requested, I did a minimum reproducible example below, it does crash in the same way: 根据要求,我在下面做了一个最小的可复制示例,它确实以相同的方式崩溃:

from PyQt5.QtCore import QDateTime, Qt, QTimer, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QTableWidgetItem
from PyQt5 import uic
import sys, os

class t0(QMainWindow):

    switch_window = pyqtSignal()

    def __init__(self):
        super(t0, self).__init__()
        uic.loadUi(Controller.application_path + r'\UI\t0.ui', self)
        #This .ui file has pretty much a button to click
        self.btn_ok.clicked.connect(self.onButtonClick1)

    def onButtonClick1(self):
        self.switch_window.emit()


class t1(QDialog):    

    switch_window = pyqtSignal()

    def __init__(self, parent=None):
        super(t1, self).__init__(parent)
        uic.loadUi(Controller.application_path + r'\UI\t1.ui', self)
        #This .ui file has a QTableWidget

        self.tw_indi.setColumnCount(1)
        self.tw_indi.setRowCount(1)
        self.tw_indi.setItem(0, 0, QTableWidgetItem("*"))

        self.tw_indi.itemDoubleClicked.connect(self.direcionar_detalhes)


    def direcionar_detalhes(self, item):
        self.switch_window.emit()

class t2(QDialog): 

    switch_window = pyqtSignal(str)

    def __init__(self, parent=None):
        super(t2, self).__init__(parent)
        uic.loadUi(Controller.application_path + r'\UI\t2.ui', self)
        #This .ui file is identical as the t1.ui, just some minor changes        


class Controller():


    controller = None
    #questões técnicas do módulo os com pyinstaller
    if getattr(sys, 'frozen', False):
        application_path = sys.exec_prefix
    else:
        application_path = os.path.dirname(os.path.abspath(__file__))

    tela = None
    app = QApplication(sys.argv)

    def __init__(self):       
        Controller.controller = self
        self.mostra_carregamento()

        sys.exit(Controller.app.exec_())

    def mostra_carregamento(self):
        self.view = t0()
        self.view.switch_window.connect(self.mostra_indicador)
        self.view.show()    
    def mostra_indicador(self):
        self.view.close()
        self.view = t1()
        self.view.switch_window.connect(self.sinal_indicador)
        self.view.show()

    def sinal_indicador(self): 
        self.view.close()
        self.view = t2()
        self.view.show()

if __name__ == '__main__':
    control = Controller()

EDIT 2 编辑2

Here's the link for the ui's: https://drive.google.com/file/d/1-iFNaWUGtJ4687nTcTFtb7WRKUL37als/view?usp=drivesdk 这是用户界面的链接: https : //drive.google.com/file/d/1-iFNaWUGtJ4687nTcTFtb7WRKUL37als/view? usp =drivesdk

The problem is not the signal, but when you want to reuse the same variable for a new window, you are eliminating the memory of that object that is still visible and therefore running, generating unreserved memory access. 问题不在于信号,而是当您想在新窗口中重用同一变量时,您将消除仍可见并因此正在运行的对象的内存,从而产生未保留的内存访问。 The solution is to use different variable names: 解决方案是使用不同的变量名:

class Controller:
    # ...
    def mostra_carregamento(self):
        self.view0 = t0()
        self.view0.switch_window.connect(self.mostra_indicador)
        self.view0.show()

    def mostra_indicador(self):
        self.view0.close()
        self.view1 = t1()
        self.view1.switch_window.connect(self.sinal_indicador)
        self.view1.show()

    def sinal_indicador(self):
        self.view1.close()
        self.view2 = t2()
        self.view2.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM