2016-08-03 87 views
1

這是對我編寫的應用程序的簡化。將PySide應用程序設置爲QScrollArea時會崩潰

該應用程序的主窗口有一個按鈕和一個複選框。 該複選框位於QScrollArea內部(通過小部件)。 該複選框有一個數字,表明該複選框已創建多少次。 點擊按鈕將打開一個帶有刷新按鈕的對話框。

點擊刷新按鈕將設置一個新的窗口小部件到一個新的複選框。

該複選框的上下文菜單打開了相同的對話框。 然而,使用複選框的上下文菜單中觸發該對話框中創建窗口小部件時,出現以下錯誤的應用程序崩潰:

2016-08-03 09:22:00.036 Python[17690:408202] modalSession has been exited prematurely - check for a reentrant call to endModalSession:

Python(17690,0x7fff76dcb300) malloc: * error for object 0x7fff5fbfe2c0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

的崩潰不會在按鈕上單擊打開對話框出現時並點擊對話框中的刷新。

崩潰發生在Mac和Windows上。 我使用Python 2.7.10與PySide 1.2.4

Context menu of the checkboxMain Window

#!/usr/bin/env python 
import sys 
from itertools import count 
from PySide import QtCore, QtGui 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self, *args, **kwargs): 
     super(MainWindow, self).__init__(*args, **kwargs) 
     self.centralwidget = QtGui.QWidget(parent=self) 

     self.open_diag_btn = QtGui.QPushButton('Open dialog', parent=self) 
     self.open_diag_btn.clicked.connect(self.open_dialog) 
     self.scroll_widget = QtGui.QScrollArea(parent=self) 

     layout = QtGui.QGridLayout(self.centralwidget) 
     layout.addWidget(self.scroll_widget) 
     layout.addWidget(self.open_diag_btn) 
     self.setCentralWidget(self.centralwidget) 
     self.set_scroll_widget() 

    def open_dialog(self): 
     dialog = Dialog(parent=self) 
     dialog.refresh.connect(self.set_scroll_widget) # Connecting the signal from the dialog to set a new widget to the scroll area 
     dialog.exec_() 
     # Even if I call the function here, after the dialog was closed instead of using the signal above the application crashes, but only via the checkbox 
     # self.set_scroll_widget() 

    def set_scroll_widget(self): 
     """Replacing the widget of the scroll area with a new one. 
      The checkbox in the layout of the widget has an class instance counter so you can see how many times the checkbox was created.""" 
     widget = QtGui.QWidget() 
     layout = QtGui.QVBoxLayout(widget) 
     widget.setLayout(layout) 
     open_diag_check = RefreshCheckbox(parent=self) 
     open_diag_check.do_open_dialog.connect(self.open_dialog) # Connecting the signal to open the dialog window 

     layout.addWidget(open_diag_check) 
     self.scroll_widget.setWidget(widget) 


class RefreshCheckbox(QtGui.QCheckBox): 
    """A checkbox class that has a context menu item which emits a signal that eventually opens a dialog window""" 
    do_open_dialog = QtCore.Signal() 
    _instance_counter = count(1) 

    def __init__(self, *args, **kwargs): 
     super(RefreshCheckbox, self).__init__(unicode(self._instance_counter.next()), *args, **kwargs) 
     self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) 
     action = QtGui.QAction(self) 
     action.setText("Open dialog") 
     action.triggered.connect(self.emit_open_dialog) 
     self.addAction(action) 

    def emit_open_dialog(self): 
     self.do_open_dialog.emit() 


class Dialog(QtGui.QDialog): 
    """A dialog window with a button that emits a refresh signal when clicked. 
     This signal is used to call MainWindow.set_scroll_widget()""" 
    refresh = QtCore.Signal() 

    def __init__(self, *args, **kwargs): 
     super(Dialog, self).__init__(*args, **kwargs) 
     self.refresh_btn = QtGui.QPushButton('Refresh') 
     self.refresh_btn.clicked.connect(self.do_refresh) 
     layout = QtGui.QVBoxLayout(self) 
     layout.addWidget(self.refresh_btn) 

    def do_refresh(self): 
     self.refresh.emit() 


if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    mySW = MainWindow() 
    mySW.show() 
    mySW.raise_() 
    app.exec_() 

回答

1

它看起來像Python是試圖刪除一個對象(或子對象之一),後者不再有 - 但是導致這種情況發生的原因並不完全清楚。有問題的對象是在滾動區域上設置的小部件。如果您明確地保留對它的引用:

def set_scroll_widget(self): 
     self._old_widget = self.scroll_widget.takeWidget() 
     ... 

您的示例將不再轉儲核心。

我認爲運行對話框exec可能在某種程度上是問題的近端原因,因爲這意味着它將運行自己的事件循環(這可能會影響刪除相關事件的順序)。我能夠用show運行對話框中找到你的例子更好的解決辦法:

def open_dialog(self): 
    dialog = Dialog(parent=self) 
    dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
    dialog.refresh.connect(self.set_scroll_widget) 
    dialog.setModal(True) 
    dialog.show() 

處事這樣意味着它不再需要保持一個明確的參照以前的滾動區域部件。

+0

用show()替換dialog.exec_()解決了崩潰問題。其他評論也很有用。謝謝! – Nir

相關問題