'Internal C++ object (PySide2.QtWidgets.QTextEdit) already deleted
I have tried to simulated below error but it is not consistent. may I know what does it really mean and how we can debug such issue.
The error generally occur from event function only
Error: elif self.review_waiver_box and obj is self.review_waiver_box.viewport():
RuntimeError: Internal C++ object (PySide2.QtWidgets.QTextEdit) already deleted.
import os
import sys
from PySide2.QtCore import QEvent, QSize, QRegularExpression
from PySide2.QtWidgets import (QPlainTextEdit, QApplication, QLabel,
QGridLayout, QGroupBox, QMessageBox,
QTextEdit, QCheckBox, QTableView,
QTableWidgetItem, QHeaderView, QWidget, QVBoxLayout,
QSizePolicy, QButtonGroup, QAbstractItemView,
QSplitter, QHBoxLayout, QComboBox)
from PySide2.QtWidgets import (QDesktopWidget, QMainWindow, QSizePolicy, QMessageBox, QToolBar, QAction, QLabel,
QMenu, QWidget, QVBoxLayout, QColorDialog, QDialog, QPushButton)
from functools import partial
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.width = 1100
self.height = 700
self.set_main_window()
self.create_violation_text()
def set_main_window(self):
"""
Setting main window position
"""
self.setWindowTitle("GUI %s" % (os.path.abspath(__file__)))
self.setFixedSize(QSize(self.width, self.height))
wid = QDesktopWidget()
screen_width = wid.screen().frameGeometry().width()
screen_height = wid.screen().frameGeometry().height()
self.setGeometry(screen_width / 2 - self.width / 2,
screen_height / 2 - self.height / 2,
self.width, self.height)
def create_violation_text(self):
"""
creating main violation window which contain all violations
"""
self.plain_textedit = QPlainTextEdit()
self.plain_textedit.setLineWrapMode(QPlainTextEdit.NoWrap)
self.plain_textedit.setStyleSheet(
"""QPlainTextEdit {font-size: 14pt;
font-family: Courier;}""")
self.plain_textedit.setReadOnly(True)
self.plain_textedit.setMouseTracking(True)
self.plain_textedit_format = self.plain_textedit.currentCharFormat()
self.plain_textedit.setCursorWidth(5)
self.plain_textedit.viewport().installEventFilter(self)
self.plain_textedit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
class ReviewWindow(MainWindow):
def __init__(self, app):
"""creating Review mode"""
super().__init__()
self.app = app
def create_widget(self):
self.waiver_approval_window = QGroupBox()
self.waiver_approval_layout = QGridLayout()
self.create_violation_window()
self.create_waiver_approval_list()
self.waiver_approval_window.setLayout(self.waiver_approval_layout)
# order matter
main_layout = QVBoxLayout()
main_layout.addWidget(self.create_violation_box)
main_layout.addWidget(self.waiver_approval_window)
# creating widgeth object as main window does not
# add layout directly it add only widget
window = QWidget()
window.setLayout(main_layout)
self.setCentralWidget(window)
def create_violation_window(self):
"""
creating violation window which contain text editor,
violation type and checkbox
"""
self.create_violation_box = QGroupBox()
self.large_file_batch_selection = QComboBox()
self.large_file_batch_selection.setGeometry(10, 15, 10, 15)
self.large_file_batch_selection.currentIndexChanged.connect(partial(self.load_range))
self.large_file_batch_selection.addItems(["0-1", "1-2"])
layout = QGridLayout()
layout.addWidget(self.large_file_batch_selection, 1, 0)
layout.addWidget(self.plain_textedit, 2, 0, 12, 1)
layout.setColumnMinimumWidth(0, 10)
self.create_violation_box.setLayout(layout)
def create_waiver_approval_list(self):
"""
Adding plain text to editor to window
"""
# waiver review pane
print("coming here")
self.waiver_file_path = QLabel()
self.waiver_file_path.setText("path")
# layout.addWidget(self.waiver_file_path, 0, 0)
self.waiver_approval_layout.addWidget(self.waiver_file_path, 0, 0)
font_family = """QTextEdit {font-size: 14pt;
background-color: #ffffe6;
color: #404040;
font-family: Courier;}"""
self.review_waiver_box = QTextEdit("Waiver_window")
self.review_waiver_box.setReadOnly(True)
self.review_waiver_box.setMouseTracking(True)
self.review_waiver_box.viewport().installEventFilter(self)
# layout.addWidget(self.review_waiver_box, 1, 0)
self.waiver_approval_layout.addWidget(self.review_waiver_box, 1, 0)
self.review_waiver_box.setPlainText("data")
# approval pane
self.approval_waiver_box = QTextEdit()
self.approval_file_path = QLabel()
self.approval_file_path.setText("path")
# layout.addWidget(self.approval_file_path, 0, 1)
self.waiver_approval_layout.addWidget(self.approval_file_path, 0, 1)
self.approval_waiver_box.setPlainText("data")
font_family = """QTextEdit {font-size: 14pt;
background-color: #ffd9b3;
color: #404040;
font-family: Courier;}"""
self.approval_waiver_box.setStyleSheet(font_family)
self.approval_waiver_box.setHidden(True)
self.approval_file_path.setHidden(True)
self.approval_waiver_box.setMouseTracking(True)
self.approval_waiver_box.setReadOnly(True)
self.approval_waiver_box.viewport().installEventFilter(self)
# layout.addWidget(self.approval_waiver_box, 1, 1)
# self.waiver_approval_window.setLayout(layout)
self.waiver_approval_layout.addWidget(self.approval_waiver_box, 1, 1)
def load_range(self, index):
print ("index")
def eventFilter(self, obj, event):
print ("event")
if obj is self.plain_textedit.viewport() and event.type() == QEvent.MouseButtonPress:
print ("text edit event")
elif self.review_waiver_box and obj is self.review_waiver_box.viewport():
if event.type() == QEvent.MouseButtonPress:
print ("button press")
return super().eventFilter(obj, event)
def closeEvent(self, event):
print ("pass")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ReviewWindow(app)
window.create_widget()
window.show()
app.exec_()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
