【问题标题】:How to remove QTreeView indentation如何删除 QTreeView 缩进
【发布时间】:2019-09-22 00:09:39
【问题描述】:

我想要一个QTreeView 没有左侧的缩进在每个嵌套级别增加。我尝试设置QTreeView::setIndentation(0)。它按照我的意愿删除了缩进,但它也隐藏了树形箭头。


默认行为:

  • 带缩进 ✗
  • 带箭头✔


setIndentation(0)之后:

  • 没有缩进 ✔
  • 没有箭头 ✗


期望的行为:

  • 没有缩进 ✔
  • 带箭头✔


那么我怎样才能达到第三个例子所示的结果呢?有没有标准的方法,或者我将不得不重新实现QTreeView::paintEvent()QTreeView::drawBranches()等?

【问题讨论】:

  • 你需要重新实现它...
  • 只是个人意见,但是...我认为您要实现的目标可能会让用户感到非常困惑。在标记为Desired behavior: 的图像中,我如何知道1Test 的子目录还是C: 的子目录?
  • @G.M.我同意,在这个特定的例子中,它会让用户非常困惑。但是,实际的树结构将具有更简单和静态的嵌套。例如,想象一下属性浏览器中的分组。

标签: qt qt5 qtreeview qtwidgets


【解决方案1】:

为了解决这个问题,我使用了一个委托来翻译项目的油漆,并绘制箭头。

#include <QtWidgets>

class BranchDelegate: public QStyledItemDelegate
{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override{
        QStyleOptionViewItem opt(option);
        if(index.column() == 0)
            opt.rect.adjust(opt.rect.height(), 0, 0, 0);
        QStyledItemDelegate::paint(painter, opt, index);
        if(index.column() == 0){
            QStyleOptionViewItem branch;
            branch.rect = QRect(0, opt.rect.y(), opt.rect.height(), opt.rect.height());
            branch.state = option.state;
            const QWidget *widget = option.widget;
            QStyle *style = widget ? widget->style() : QApplication::style();
            style->drawPrimitive(QStyle::PE_IndicatorBranch, &branch, painter, widget);
        }
    }
};

class TreeView: public QTreeView
{
public:
    TreeView(QWidget *parent=nullptr):QTreeView(parent)
    {
        BranchDelegate *delegate = new BranchDelegate(this);
        setItemDelegate(delegate);
        setIndentation(0);
    }
protected:
    void mousePressEvent(QMouseEvent *event) override{
        QModelIndex index = indexAt(event->pos());
        bool last_state = isExpanded(index);
        QTreeView::mousePressEvent(event);
        if(index.isValid() && last_state == isExpanded(index))
            setExpanded(index, !last_state);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TreeView w;
    QFileSystemModel model;
    model.setRootPath(QDir::rootPath());
    w.setModel(&model);
    w.setRootIndex(model.index(QDir::homePath()));
    /*for (int i = 1; i< model.columnCount() ; ++i) {
        w.hideColumn(i);
    }*/
    w.expandAll();
    w.resize(640, 480);
    w.show();
    return a.exec();
}

【讨论】:

    【解决方案2】:

    如果有水平滚动,eyllanesc 的瀑布就会消失。此外,通常视图仅在单击分支指示器时展开/折叠,而不是索引。

    我的解决方案:仅更改具有父级但没有子级的索引的 Rect。也不要将缩进设置为 0。不需要继承 QTreeView。

    #include <QtWidgets>
    class BranchDelegate: public QStyledItemDelegate
    {
    public:
        mIndent = 50;
        using QStyledItemDelegate::QStyledItemDelegate;
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
        {
            QStyleOptionViewItem opt(option);
            if(index.parent().isValid && (!index.model() || !index.model()->index(0, 0, index).isValid())) 
            { 
                opt.rect.adjust(-mIndent, 0, 0, 0); 
            }
            QStyledItemDelegate::paint(painter, opt, index);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTreeView apView;
        BranchDelegate* const apDelegate = new BranchDelegate(apView);
        apDelegate->mIndent = 50;
        apView->setIndentation(apDelegate->mIndent);
        apView->setItemDelegateForColumn(0, apDelegate);
    
    
        QFileSystemModel model;
        model.setRootPath(QDir::rootPath());
        apView.setModel(&model);
        apView.setRootIndex(model.index(QDir::homePath()));
        /*for (int i = 1; i< model.columnCount() ; ++i) {
            apView.hideColumn(i);
        }*/
        apView.expandAll();
        apView.resize(640, 480);
        apView.show();
        return a.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多