2012-11-20 100 views
1

我試圖通過使用QTableView.setItemDelegateForRow()方法設置一個編輯器委託在我的QTableView特定於每一行數據。當我在多行上設置委託時,它會導致PyQt4出現分段錯誤。這似乎至少部分與使用相同的變量來存儲委託類的不同實例,就像這樣:PyQt4 setItemDelegateForRow導致段錯誤

# This code causes a segmentation fault 
delegate = ListDelegate(choices0) 
tableView.setItemDelegateForRow(0,delegate) 
delegate = ListDelegate(choices1) 
tableView.setItemDelegateForRow(1,delegate) 

但是,如果我用不同的變量,它似乎正常工作,如:

# This code works to set the delegate by row 
delegate0 = ListDelegate(choices0) 
tableView.setItemDelegateForRow(0,delegate0) 
delegate1 = ListDelegate(choices1) 
tableView.setItemDelegateForRow(1,delegate1) 

我有一個代碼的例子,導致我將在最後包括的分段錯誤。我已經在Ubuntu Linux和Windows 7上(都是64位)對python 2.7進行了測試。我試圖設置一個循環中的委託,並使用字典或列表來存儲的代表似乎沒有解決問題。

示例應用程序:

from PyQt4 import QtGui, QtCore 
import sys 

class PaletteListModel(QtCore.QAbstractListModel): 

    def __init__(self, colors = [], parent = None): 
     QtCore.QAbstractListModel.__init__(self,parent) 
     self.__colors = colors 

    # required method for Model class 
    def rowCount(self, parent): 
     return len(self.__colors) 

    # optional method for Model class 
    def headerData(self, section, orientation, role): 
     if role == QtCore.Qt.DisplayRole: 
      if orientation == QtCore.Qt.Horizontal: 
       return QtCore.QString("Palette") 
      else: 
       return QtCore.QString("Color %1").arg(section) 

     if role == QtCore.Qt.ToolTipRole: 
      if orientation == QtCore.Qt.Horizontal: 
       return QtCore.QString("Horizontal Header %s Tooltip" % str(section)) 
      else: 
       return QtCore.QString("Vertical Header %s Tooltip" % str(section)) 


    # required method for Model class 
    def data(self, index, role): 
     # index contains a QIndexClass object. The object has the following 
     # methods: row(), column(), parent() 

     row = index.row() 
     value = self.__colors[row] 

     # keep the existing value in the edit box 
     if role == QtCore.Qt.EditRole: 
      return self.__colors[row].name() 

     # add a tooltip 
     if role == QtCore.Qt.ToolTipRole: 
      return "Hex code: " + value.name() 

     if role == QtCore.Qt.DecorationRole: 
      pixmap = QtGui.QPixmap(26,26) 
      pixmap.fill(value) 

      icon = QtGui.QIcon(pixmap) 

      return icon 

     if role == QtCore.Qt.DisplayRole: 

      return value.name() 

    def setData(self, index, value, role = QtCore.Qt.EditRole): 
     row = index.row() 

     if role == QtCore.Qt.EditRole: 
      color = QtGui.QColor(value) 

      if color.isValid(): 
       self.__colors[row] = color 
       self.dataChanged.emit(index, index) 
       return True 

     return False 

    # implment flags() method 
    def flags(self, index): 
     return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable 

class ListDelegate(QtGui.QStyledItemDelegate): 
    def __init__(self, colors = QtCore.QStringList(), parent = None): 
     #super(ListDelegate, self).__init__() 
     QtGui.QStyledItemDelegate.__init__(self,parent) 
     self.__colors = colors 
     #self.__current = current 

    def createEditor(self, parent, option, index): 
     ed = QtGui.QComboBox(parent) 
     ed.setEditable(True) 

     # add each settings option to the combobox 
     for s in self.__colors: 
      ed.addItem(s) 

     return ed 


if __name__ == '__main__': 


    app = QtGui.QApplication(sys.argv) 
    app.setStyle("plastique") 

    data = QtCore.QStringList() 
    data << "one" << "two" << "three" << "four" << "five" 

    tableView = QtGui.QTableView() 
    tableView.show() 

    red = QtGui.QColor(255,0,0) 
    green = QtGui.QColor(0,255,0) 
    blue = QtGui.QColor(0,0,255) 

    model = PaletteListModel([red, green, blue]) 

    choices0 = QtCore.QStringList(["#FF0000","#00FF00","#0000FF"]) 
    choices1 = QtCore.QStringList(["#FF0000","#0000FF"]) 

    # This code works to set the delegate by row 
    delegate0 = ListDelegate(choices0) 
    tableView.setItemDelegateForRow(0,delegate0) 
    delegate1 = ListDelegate(choices1) 
    tableView.setItemDelegateForRow(1,delegate1) 

    # This code causes a segmentation fault 
    delegate = ListDelegate(choices0) 
    tableView.setItemDelegateForRow(0,delegate) 
    delegate = ListDelegate(choices1) 
    tableView.setItemDelegateForRow(1,delegate) 

    tableView.setModel(model) 
    tableView.horizontalHeader() 

    sys.exit(app.exec_()) 

堆棧跟蹤(從Ubuntu的框),雖然我真的不知道如何解釋它:

gdb python2.7-dbg 
GNU gdb (GDB) 7.5-ubuntu 
Copyright (C) 2012 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "x86_64-linux-gnu". 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>... 
Reading symbols from /usr/bin/python2.7-dbg...done. 
(gdb) run TestDelegateForRow.py 
Starting program: /usr/bin/python2.7-dbg TestDelegateForRow.py 
[Thread debugging using libthread_db enabled] 
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". 
[New Thread 0x7fffe6a78700 (LWP 12070)] 
[New Thread 0x7fffe6277700 (LWP 12071)] 
[New Thread 0x7fffe5442700 (LWP 12072)] 

Program received signal SIGSEGV, Segmentation fault. 
0x00007ffff5ca7cf8 in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
(gdb) backtrace 
#0 0x00007ffff5ca7cf8 in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#1 0x00007ffff5cb27f6 in QTableView::paintEvent(QPaintEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#2 0x00007ffff64f54d3 in sipQTableView::paintEvent (this=0xfbfa60, a0=0x7fffffffc0a0) at sipQtGuipart2.cpp:35590 
#3 0x00007ffff57be802 in QWidget::event(QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#4 0x00007ffff5b6db66 in QFrame::event(QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#5 0x00007ffff5c7959b in QAbstractItemView::viewportEvent(QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#6 0x00007ffff64fbc9b in viewportEvent (a0=0x7fffffffc0a0, this=0xfbfa60) at sipQtGuipart2.cpp:36150 
#7 sipQTableView::viewportEvent (this=0xfbfa60, a0=0x7fffffffc0a0) at sipQtGuipart2.cpp:36142 
#8 0x00007ffff524a6d6 in QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject*, QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4 
#9 0x00007ffff576ee6c in QApplicationPrivate::notify_helper(QObject*, QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#10 0x00007ffff577330a in QApplication::notify(QObject*, QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#11 0x00007ffff67b62a6 in notify (a1=0x7fffffffc0a0, a0=0xe31b10, this=0xcfbcb0) at sipQtGuipart9.cpp:35916 
#12 sipQApplication::notify (this=0xcfbcb0, a0=0xe31b10, a1=0x7fffffffc0a0) at sipQtGuipart9.cpp:35908 
#13 0x00007ffff524a56e in QCoreApplication::notifyInternal(QObject*, QEvent*)() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4 
#14 0x00007ffff57ba524 in QWidgetPrivate::drawWidget(QPaintDevice*, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#15 0x00007ffff57bb01f in QWidgetPrivate::paintSiblingsRecursive(QPaintDevice*, QList<QObject*> const&, int, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*)() 
    from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#16 0x00007ffff57bae64 in QWidgetPrivate::paintSiblingsRecursive(QPaintDevice*, QList<QObject*> const&, int, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*)() 
    from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#17 0x00007ffff57bae64 in QWidgetPrivate::paintSiblingsRecursive(QPaintDevice*, QList<QObject*> const&, int, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*)() 
    from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#18 0x00007ffff57bae64 in QWidgetPrivate::paintSiblingsRecursive(QPaintDevice*, QList<QObject*> const&, int, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*)() 
    from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#19 0x00007ffff57ba0b5 in QWidgetPrivate::drawWidget(QPaintDevice*, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#20 0x00007ffff5988958 in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#21 0x00007ffff5988d1e in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#22 0x00007ffff57eb6aa in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#23 0x00007ffff57ec6bb in QApplication::x11ProcessEvent(_XEvent*)() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#24 0x00007ffff5813fa2 in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#25 0x00007ffff44b7ab5 in g_main_context_dispatch() from /lib/x86_64-linux-gnu/libglib-2.0.so.0 
#26 0x00007ffff44b7de8 in ??() from /lib/x86_64-linux-gnu/libglib-2.0.so.0 
#27 0x00007ffff44b7ea4 in g_main_context_iteration() from /lib/x86_64-linux-gnu/libglib-2.0.so.0 
#28 0x00007ffff5278bf6 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4 
#29 0x00007ffff5813c1e in ??() from /usr/lib/x86_64-linux-gnu/libQtGui.so.4 
#30 0x00007ffff52492bf in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4 
#31 0x00007ffff5249548 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4 
#32 0x00007ffff524e708 in QCoreApplication::exec()() from /usr/lib/x86_64-linux-gnu/libQtCore.so.4 
#33 0x00007ffff677762b in meth_QApplication_exec_ (sipArgs=<optimized out>) at sipQtGuipart9.cpp:37856 
#34 0x0000000000486b1a in PyCFunction_Call (func=<built-in method exec_ of QApplication object at remote 0xe891e0>, arg=(), kw=0x0) at ../Objects/methodobject.c:81 
#35 0x0000000000526610 in call_function (pp_stack=0x7fffffffd960, oparg=0) at ../Python/ceval.c:4021 
#36 0x00000000005213fb in PyEval_EvalFrameEx (f=Frame 0xb89c00, for file TestDelegateForRow.py, line 131, in <module>(), throwflag=0) at ../Python/ceval.c:2666 
#37 0x0000000000523de9 in PyEval_EvalCodeEx (co=0xcbd510, globals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), locals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), args=0x0, argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at ../Python/ceval.c:3253 
---Type <return> to continue, or q <return> to quit--- 
#38 0x0000000000519e56 in PyEval_EvalCode (co=0xcbd510, globals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), locals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated)) at ../Python/ceval.c:667 
#39 0x000000000055685c in run_mod (mod=0xb91aa8, filename=0x7fffffffe374 "TestDelegateForRow.py", globals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), locals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), flags=0x7fffffffde50, arena=0x9f8ae0) at ../Python/pythonrun.c:1365 
#40 0x00000000005567e2 in PyRun_FileExFlags (fp=0xb3e1b0, filename=0x7fffffffe374 "TestDelegateForRow.py", start=257, globals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), locals= 
    {'choices0': <QStringList at remote 0x10fc2e0>, 'choices1': <QStringList at remote 0x10fc380>, 'app': <QApplication at remote 0xe891e0>, 'delegate0': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89420>, 'delegate1': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe894e0>, 'blue': <QColor at remote 0x10fc240>, 'QtGui': <module at remote 0xcd2a20>, 'data': <QStringList at remote 0x10fc060>, '__package__': None, 'ListDelegate': <PyQt4.QtCore.pyqtWrapperType at remote 0xcfb6a0>, '__doc__': None, 'red': <QColor at remote 0x10fc100>, 'tableView': <QTableView at remote 0xe892a0>, '__builtins__': <module at remote 0x7ffff7fc5470>, '__file__': 'TestDelegateForRow.py', 'sys': <module at remote 0x7ffff7fc55a8>, '__name__': '__main__', 'PaletteListModel': <PyQt4.QtCore.pyqtWrapperType at remote 0xb955b0>, 'green': <QColor at remote 0x10fc1a0>, 'delegate': <ListDelegate(_ListDelegate__colors=<...>) at remote 0xe89660>, 'model': <PaletteListModel(_PaletteListModel__colors=[<...>, <...>, <...>]) at remote ...(truncated), closeit=1, flags=0x7fffffffde50) at ../Python/pythonrun.c:1351 
#41 0x0000000000555002 in PyRun_SimpleFileExFlags (fp=0xb3e1b0, filename=0x7fffffffe374 "TestDelegateForRow.py", closeit=1, flags=0x7fffffffde50) at ../Python/pythonrun.c:943 
#42 0x00000000005546b5 in PyRun_AnyFileExFlags (fp=0xb3e1b0, filename=0x7fffffffe374 "TestDelegateForRow.py", closeit=1, flags=0x7fffffffde50) at ../Python/pythonrun.c:747 
#43 0x0000000000570290 in Py_Main (argc=2, argv=0x7fffffffe068) at ../Modules/main.c:639 
#44 0x00000000004171fc in main (argc=2, argv=0x7fffffffe068) at ../Modules/python.c:23 
(gdb) ^Z 

當然,任何幫助或指導將不勝感激。

+0

我沒有在Win7得到段錯誤。對於錯誤的代碼,我只是讓'row 0'返回到默認代理。我也很好奇,如何保持代表的「list」或「dict」沒有幫助。對於示例代碼,將代表存儲在「列表」中使得事情很好地工作。 – Avaris

回答

2

setItemDelegateForRow Qt文檔是對這個很清楚:

Any existing row delegate for row will be removed, but not deleted. 
QAbstractItemView does not take ownership of delegate. 

所以,你已經診斷您的問題,但通過它沒有跟上。

段錯誤發生,因爲有些代理正在初始化表視圖之前被垃圾收集。

嘗試創建一個保持其委託的內部列表表視圖子類:

...  

class TableView(QtGui.QTableView): 
    def __init__(self): 
     QtGui.QTableView.__init__(self) 
     colours = [ 
      QtGui.QColor(255,0,0), 
      QtGui.QColor(0,255,0), 
      QtGui.QColor(0,0,255), 
      ] 
     self._delegates = [ 
      ListDelegate(["#FF0000","#00FF00","#0000FF"]), 
      ListDelegate(["#FF0000","#0000FF"]), 
      ] 
     for row in range(len(colours)): 
      self.setItemDelegateForRow(
       row, self._delegates[bool(row % 2)]) 
     self.setModel(PaletteListModel(colours)) 

if __name__ == '__main__': 

    app = QtGui.QApplication(sys.argv) 
    app.setStyle("plastique") 

    table = TableView() 
    table.show() 

    sys.exit(app.exec_()) 
+0

謝謝ekhumoro!代表被垃圾收集是問題。在我對實際應用程序的測試中,它沒有解決問題的原因是因爲我正在初始化一個方法內的列表,並且一旦方法完成列表就被垃圾收集。 – freakinhippie