我将纯虚拟方法QStyledItemDelegate::paint定义为:
void FooViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
bool selected = option.state & QStyle::State_Selected;
// ...
// drawing code
}但我不知道如何知道绘图项当前或无(与来自QListView::currentIndex()的相同项)。
发布于 2016-07-27 13:23:43
Qt MVC不是为这样的用途而设计的,因为理论上,委托不应该知道您使用的是什么视图(可能是QListView或QTableView)。
因此,“好方法”是将此信息保存在委托中(因为模型可能由sevaral视图使用)。Fox示例(伪代码):
class FooViewDelegate : ...
{
private:
QModelIndex _currentIndex;
void connectToView( QAbstractItemView *view )
{
connect( view, &QAbstractItemView::currentChanged, this, &FooViewDelegate ::onCurrentChanged );
}
void onCurrentChanged( const QModelIndex& current, const QModelIndex& prev )
{
_currentIndex = current;
}
public:
void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
bool selected = index == _currentIndex;
// ...
// drawing code
}
}发布于 2017-08-21 10:55:15
委托的父级是视图,您可以直接从视图中获取当前索引。
void FooViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
bool selected = index == parent()->currentIndex();
}发布于 2016-07-27 12:49:49
你走在正确的轨道上:
auto current = option.state & QStyle::State_HasFocus;具有焦点的项是当前项。
https://stackoverflow.com/questions/38612462
复制相似问题