[qtpy4]RuntimeError: wrapped C/C++ object of %S has been del

开发基于GTK+或QT的应用程序
回复
头像
Hello World!
帖子: 3051
注册时间: 2008-06-23 15:19
系统: ���������
来自: 北欧某国
送出感谢: 14 次
接收感谢: 2 次
联系:

[qtpy4]RuntimeError: wrapped C/C++ object of %S has been del

#1

帖子 Hello World! » 2013-02-09 22:06

截图:
Bedit 2-9.jpg
程序是这样的 document 是 QtGui.QTextEdit 的派生类,然后我把它存储在数组 self.doclist 里。我想每次点击标签页时,就显示一个 document ,代码是这样的:[python] def on_cur_tab(self, cint):
"""切换标签"""
print self.doclist[cint]
self.setCentralWidget(self.doclist[cint])[/python]但是报错

代码: 全选

RuntimeError: wrapped C/C++ object of %S has been deleted
下面是完整的代码:[python]#!/usr/bin/python
#coding:utf8

import sys
from PyQt4 import QtGui, QtCore
import os

class MainWindow(QtGui.QMainWindow):
doclist = []
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(500, 300)
self.setWindowTitle('BEdit')

self.tabWidget = QtGui.QTabBar()

self.tabWidget.setTabsClosable(True)
# 信号
self.connect(self.tabWidget, QtCore.SIGNAL('currentChanged(int)'), self.on_cur_tab)

self.exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
self.exit.setShortcut('Ctrl+Q')
self.exit.setStatusTip('Exit application')
self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))


self.statusBar().showMessage('Ready')

menubar = self.menuBar()
file = menubar.addMenu('&File')
file.addAction(self.exit)

self.toolbar = self.addToolBar('Exit')

self.button_new = QtGui.QAction(QtGui.QIcon('icons/new.png'), u'新建文档', self)
self.toolbar.addAction(self.button_new)
self.connect(self.button_new, QtCore.SIGNAL('triggered()'), self.on_new)

self.button_open = QtGui.QAction(QtGui.QIcon('icons/open.png'), u'打开文件', self)
self.connect(self.button_open, QtCore.SIGNAL('triggered()'), self.on_open)
self.toolbar.addAction(self.button_open)

self.save = QtGui.QAction(QtGui.QIcon('icons/save.png'), u'保存当前文件', self)
self.toolbar.addAction(self.save)
self.printtext = QtGui.QAction(QtGui.QIcon('icons/print.png'), u'打印当前文件', self)
self.toolbar.addAction(self.printtext)
self.up = QtGui.QAction(QtGui.QIcon('icons/up.png'), u'撤销上次操作', self)
self.toolbar.addAction(self.up)
self.forward = QtGui.QAction(QtGui.QIcon('icons/forward.png'), u'重做上次撤销的操作', self)
self.toolbar.addAction(self.forward)

self.toolbar.addAction(self.exit)


self.toolbar.addWidget(self.tabWidget)

self.on_new()

self.setStyleSheet("background-image:url(/home/liu/.local/share/gedit/plugins/wallpaper_74694_1024.jpg)")

def on_new(self):
"""这里新建一个文档,同时新建一个标签。"""
self.doclist.append(document())
self.setCentralWidget(self.doclist[-1])
self.tabWidget.addTab(u"无标题文档")
print len(self.doclist)

def on_open(self):
fileName = QtGui.QFileDialog.getOpenFileName(self, u"打开文件" , "", u"文本文件 (*.txt *.py)")
if fileName != "":
self.on_new()

# 在这里读取文件
file_object = open(fileName)
try:
all_the_text = file_object.read()
self.doclist[-1].setText(all_the_text)
# 将绝对路径分解成路径和文件名
self.doclist[-1].pathname , self.doclist[-1].filename = os.path.split(fileName)
finally:
file_object.close()

def on_cur_tab(self, cint):
"""切换标签"""
print self.doclist[cint]
self.setCentralWidget(self.doclist[cint])

class document(QtGui.QTextEdit):
pathname = ""
filename = ""
def __init__(self):
QtGui.QTextEdit.__init__(self)
self.setStyleSheet("background-image:url(/home/liu/.local/share/gedit/plugins/wallpaper_74694_1024.jpg)")
self.setStyleSheet("background: rgba(255, 255, 255, 150)")

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())[/python]
http://noie.name 网站改版中。
回复

回到 “GTK+和QT”