如何在Qt项目视图中为单个单元格设置一个委托? [英] How to set a delegate for a single cell in Qt item view?

查看:1468
本文介绍了如何在Qt项目视图中为单个单元格设置一个委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当困惑,但在Qt的 QAbstractItemView 类中,可以设置 QAbstractItemDelegate (即 QItemDelegate QStyledItemDelegate )使用 setItemDelegate * 方法到整个视图,单行或单列。此外,可以使用 QAbstractItemView :: itemDelegate(const QModelIndex&)以及行,列的委托来查询单个单元格的项委托。和整个视图。但似乎没有办法将项目委托设置到单个单元格。我缺少什么?任何原因,这应该是?

Rather perplexed by this omission--but in Qt's QAbstractItemView class, it's possible to set a QAbstractItemDelegate (i.e., QItemDelegate or QStyledItemDelegate) to the entire view, a single row, or a single column, using the setItemDelegate* methods. In addition the item delegate for an individual cell can be queried, with QAbstractItemView::itemDelegate(const QModelIndex&), along with the delegate for rows, columns. and the entire view. But there appears to be no way to set an item delegate to an individual cell. Am I missing something? Any reason this should be?

推荐答案

不,你不能设置项委派只有一个单元格或一列,

No you can't set item delegate only for one cell or one column but you can easly set item delegate for whole widget and choose in which cell, column or row you want to use your custom painting or something.

例如

void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                       const QModelIndex &index) const
{
    if (index.column() == 1) 
    {
        // ohh it's my column
        // better do something creative
    } 
    else // it's just a common column. Live it in default way
        QItemDelegate::paint(painter, option, index);
}

您可以找到更多信息此处

这篇关于如何在Qt项目视图中为单个单元格设置一个委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆