c++ - QTreeView 合并一些单元格

标签 c++ qt model-view-controller qtreeview

我有一个自定义 QAbstractItemModel 和自定义 TreeView 。
是否可以合并 QTreeView 中的一些单元格?

它看起来像这样:

Num | Name      | Qty | .... |
----|-----------|-----|------|
1   | Unit one  |  5  | .... |
1.1 | Sub unit1 |  3  | .... |
1.2 | Very very big string   |
1.3 | Sub unit2 |  2  | .... |

此外,QTreeWidget::setFirstColumnSpanned() 也不是必需的。

最佳答案

这是我的第一次尝试,它成功了:

void YourClassDerivedFromQTreeView::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (ThisIsTheBigOne)
    {
        QStyleOptionViewItem opt = option;

        if (selectionModel()->isSelected(index)) {
            opt.state |= QStyle::State_Selected;
        }

        int firstSection = header()->logicalIndex(0);
        int lastSection = header()->logicalIndex(header()->count() - 1);
        int left = header()->sectionViewportPosition(firstSection);
        int right = header()->sectionViewportPosition(lastSection) + header()->sectionSize(lastSection);
        int indent = LevelOfThisItem * indentation();

        left += indent;

        opt.rect.setX(left);
        opt.rect.setWidth(right - left);

        itemDelegate(index)->paint(painter, opt, index);
    }
    else {
        QTreeView::drawRow(painter, option, index);
    }
}

不使用委托(delegate)类。只需自定义 QTreeView 的 drawRow 函数,如果它很大,做一些数学运算并调用 itemDelegate(index)->paint,这是 QTreeView 的默认行为,并且它对样式表很友好。

关于c++ - QTreeView 合并一些单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27149733/

相关文章:

c++ - HP-35计算器基本功能代码

c++ - 将字符串文字添加到 static_assert

c++ - 如何在父级大小更改时更改子级小部件的大小?

c++ - Qt 4.8 信号/插槽在 moveToThread() 之后未调用

c++ - Qt 信号和槽混淆

iphone - 模型- View - Controller 模式如何应用于 iPhone 开发?

c++ - 将 vector 中的元素放入队列然后清除 vector

php - PHP 扩展(在 C++ 中)函数中的 pthread_create 从不返回

asp.net-mvc - 在 MVC 中,我可以使用数据注释将默认文本替换为图像吗?

java - @Valid 在 Spring java 中不起作用