如何在Qt项目视图中为单个单元格设置委托?

Mat*_*ips 6 c++ model-view-controller qt delegates

相当困惑的是这个省略 - 但在Qt的QAbstractItemView类中,可以使用这些方法将QAbstractItemDelegate(即QItemDelegateQStyledItemDelegate)设置为整个视图,单行或单个列setItemDelegate*.此外,可以查询单个单元格的项目委托QAbstractItemView::itemDelegate(const QModelIndex&),以及行,列的委托.和整个观点.但是,似乎没有办法设置的项目委托给一个单独的单元格.我错过了什么吗?这应该是什么原因?

Blo*_*ood 5

不,您不能仅为一个单元格或一列设置项目委托,但您可以轻松地为整个窗口小部件设置项目委托,并选择要使用自定义绘画或其他内容的单元格,列或行.

例如

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);
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到更多信息

  • -1'不,你不能只为一个单元格或一列设置项目委托'是的你可以,检查`void QAbstractItemView :: setItemDelegateForColumn(int column,QAbstractItemDelegate*delegate)` (9认同)