2012-11-20 7 views
1

QTableView.setItemDelegateForRow() 메서드를 사용하여 각 데이터 행의 특정 QTableView에 편집기 대리자를 설정하려고합니다. 대리자를 여러 행에 설정하면 PyQt4가 세그먼트 오류가됩니다. 그것은 적어도 부분적으로 관련 대리자 클래스의 다른 인스턴스를 저장할과 같이 동일한 변수를 사용하는 것 : 나는 다른 변수를 사용하는 경우Segmentation 오류를 일으키는 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) 

끝에 세그먼트 오류가 발생하는 코드 예가 ​​있습니다. 필자는 우분투 리눅스와 윈도우 7 모두에서 Python 2.7과 64 비트를 비교 테스트했습니다.

빠른 수정은 별도의 변수를 사용하는 것이지만 실제 코드 (이 샘플 코드는 아님)에서는 루프에서 대리자를 설정하고 사전 또는 목록을 사용하여 대의원들은 문제를 해결하지 못하는 것 같습니다.

샘플 응용 프로그램 :

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_()) 

이 스택 트레이스 난 정말 그것을 해석하는 방법을 알고하지 않습니다하지만 (우분투 상자에서) : 물론

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'을 어떻게 유지하는 것이 도움이되지 않는지 궁금하다. 샘플 코드의 경우 델리 게이트를'list'에 저장하면 잘 작동합니다. – Avaris

답변

2

setItemDelegateForRow의 Qt는 문서이 꽤 분명하다

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

그래서 당신은 이미 당신의 문제를 진단하지만, 그것을 통해 따르지했습니다.

테이블 뷰를 초기화하기 전에 일부 대리자가 가비지 수집되기 때문에 segfault가 발생합니다.

는 대표의 내부 목록을 유지 테이블 뷰 서브 클래스를 만들어보십시오 :

...  

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

에코 모로 감사합니다! 가비지 수집되는 대리인이 문제였습니다. 실제 응용 프로그램에 대한 테스트에서 문제를 해결하지 못한 이유는 메서드 내에서 목록을 초기화했기 때문에 메서드가 완료되면 목록에서 가비지 수집이 완료 되었기 때문입니다. – freakinhippie