我只在linux上遇到此行为。在Windows上,这个问题不会发生。
我有一个使用模型/视图框架的表。其中一列是可编辑的,当我输入它来更改数据时,旧数据在后台仍然可见,而我在前台编辑数据。
我不确定是什么导致这种情况,我尝试过翻转各种设置,但我一直无法改变行为。
也许一个更简单的问题仍然可以帮助我:我正确地查看此问题的视图代码是否正确?该模型可能与此有关吗?我是否需要在模型中设置任何当前正在编辑的标志?
假设视图是问题的所在,我使用的是大多数视图逻辑。以下代码有一些注意事项:我注入了QTableViews和QTreeViews之间使用的公共代码,所以这个类有一些没有明确列为方法的函数:
from __future__ import absolute_import, division, print_function
from guitool.__PYQT__ import QtCore, QtGui
from guitool import api_item_view
from guitool.guitool_decorators import signal_, slot_
API_VIEW_BASE = QtGui.QTableView
class APITableView(API_VIEW_BASE):
rows_updated = signal_(str, int)
contextMenuClicked = signal_(QtCore.QModelIndex, QtCore.QPoint)
API_VIEW_BASE = API_VIEW_BASE
def __init__(view, parent=None):
API_VIEW_BASE.__init__(view, parent)
api_item_view.injectviewinstance(view)
view._init_table_behavior()
view._init_header_behavior()
view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
view.customContextMenuRequested.connect(view.on_customMenuRequested)
#---------------
# Initialization
#---------------
def _init_table_behavior(view):
view.setCornerButtonEnabled(False)
view.setWordWrap(True)
view.setSortingEnabled(True)
view.setShowGrid(True)
# Selection behavior #view.setSelectionBehavior(QtGui.QAbstractItemView.SelectColumns)
view.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems)
view._defaultEditTriggers = QtGui.QAbstractItemView.AllEditTriggers
view.setEditTriggers(view._defaultEditTriggers)
view.setIconSize(QtCore.QSize(64, 64))
def _init_header_behavior(view):
""" Header behavior """
# Row Headers
verticalHeader = view.verticalHeader()
verticalHeader.setVisible(True)
#verticalHeader.setSortIndicatorShown(True)
verticalHeader.setHighlightSections(True)
verticalHeader.setResizeMode(QtGui.QHeaderView.Interactive)
verticalHeader.setMovable(True)
# Column headers
horizontalHeader = view.horizontalHeader()
horizontalHeader.setVisible(True)
horizontalHeader.setStretchLastSection(True)
horizontalHeader.setSortIndicatorShown(True)
horizontalHeader.setHighlightSections(True)
# Column Sizes
# DO NOT USE ResizeToContents. IT MAKES THINGS VERY SLOW
#horizontalHeader.setResizeMode(QtGui.QHeaderView.ResizeToContents)
#horizontalHeader.setResizeMode(QtGui.QHeaderView.Stretch)
horizontalHeader.setResizeMode(QtGui.QHeaderView.Interactive)
#horizontalHeader.setCascadingSectionResizes(True)
# Columns moveable
horizontalHeader.setMovable(True)
#---------------
# Qt Overrides
#---------------
def setModel(view, model):
""" QtOverride: Returns item delegate for this index """
api_item_view.setModel(view, model)
def keyPressEvent(view, event):
assert isinstance(event, QtGui.QKeyEvent)
API_VIEW_BASE.keyPressEvent(view, event)
if event.matches(QtGui.QKeySequence.Copy):
#print('Received Ctrl+C in View')
view.copy_selection_to_clipboard()
#print ('[view] keyPressEvent: %s' % event.key())
def mouseMoveEvent(view, event):
assert isinstance(event, QtGui.QMouseEvent)
API_VIEW_BASE.mouseMoveEvent(view, event)
def mousePressEvent(view, event):
assert isinstance(event, QtGui.QMouseEvent)
API_VIEW_BASE.mousePressEvent(view, event)
#print('no editing')
view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
def mouseReleaseEvent(view, event):
assert isinstance(event, QtGui.QMouseEvent)
#print('editing ok')
view.setEditTriggers(view._defaultEditTriggers)
API_VIEW_BASE.mouseReleaseEvent(view, event)
def clearSelection(view, *args, **kwargs):
print('[table_view] clear selection')
API_VIEW_BASE.clearSelection(view, *args, **kwargs)
#---------------
# Slots
#---------------
@slot_(str, int)
def on_rows_updated(view, tblname, num):
# re-emit the model signal
view.rows_updated.emit(tblname, num)
@slot_(QtCore.QPoint)
def on_customMenuRequested(view, pos):
index = view.indexAt(pos)
view.contextMenuClicked.emit(index, pos)
# ----
# Injected funcs from api_item_view
@register_view_method
def infer_delegates(view, **headers):
""" Infers which columns should be given item delegates """
get_thumb_size = headers.get('get_thumb_size', None)
col_type_list = headers.get('col_type_list', [])
num_cols = view.model().columnCount()
num_duplicates = int(num_cols / len(col_type_list))
col_type_list = col_type_list * num_duplicates
for colx, coltype in enumerate(col_type_list):
if coltype in qtype.QT_PIXMAP_TYPES:
if VERBOSE:
print('[view] colx=%r is a PIXMAP' % colx)
thumb_delegate = APIThumbDelegate(view, get_thumb_size)
view.setItemDelegateForColumn(colx, thumb_delegate)
elif coltype in qtype.QT_BUTTON_TYPES:
if VERBOSE:
print('[view] colx=%r is a BUTTON' % colx)
button_delegate = APIButtonDelegate(view)
view.setItemDelegateForColumn(colx, button_delegate)
else:
if VERBOSE:
print('[view] colx=%r does not have a delgate' % colx)
@register_view_method
def set_column_persistant_editor(view, column):
""" Set each row in a column as persistant """
num_rows = view.model.rowCount()
print('view.set_persistant: %r rows' % num_rows)
for row in range(num_rows):
index = view.model.index(row, column)
view.view.openPersistentEditor(index)
答案 0 :(得分:0)
item-delegate编辑器需要绘制自己的背景:
editor.setAutoFillBackground(True)
显然,这必须在自定义委托类(APIThumbDelegate
和/或APIButtonDelegate
)中修复,这些类创建编辑器小部件(即通过其createEditor
函数)。