如何 select QTreeView 中的前一行?

How to select previous row in a QTreeView?

我有一个单列多行(比如 5 行)的 QTreeView。我想要实现的是,如果我 select 一行,我想在某些情况下重新 select 前一行。

这是我的代码:

MyWidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QModelIndex>
#include <QHBoxLayout>

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = 0);
    ~MyWidget();

private slots:
    void _OnTreeViewCurrentRowChanged(const QModelIndex &rcqmiCurrIndex,
        const QModelIndex &rcqmiPrevIndex);

private:
    QHBoxLayout *_pLayout;

    QTreeView *_pTreeView;
    QStandardItemModel *_pStandardItemModel;
    QStandardItem *_pStandardItem;
};

#endif

MyWidget.cpp

#include "MyWidget.h"

static bool bCondition = false;

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
    _pLayout = new QHBoxLayout(this);

    _pTreeView = new QTreeView(this);
    _pStandardItemModel = new QStandardItemModel(_pTreeView);
    _pStandardItem = new QStandardItem("Column A");

    _pLayout->addWidget(_pTreeView);

    _pStandardItemModel->setColumnCount(1);
    _pStandardItemModel->setHorizontalHeaderItem(0, _pStandardItem);

    _pTreeView->setModel(_pStandardItemModel);
    _pTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    _pTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
    _pTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    // add rows
    _pTreeView->selectionModel()->blockSignals(true);

    for (int i = 0; i < 5; ++i)
    {
        QStandardItem *pStandardItem = new QStandardItem(
            QString("Row: %1").arg(i + 1));

        _pStandardItemModel->appendRow(pStandardItem);
    }

    _pTreeView->selectionModel()->blockSignals(false);

    // update view
    _pTreeView->viewport()->update();

    connect(_pTreeView->selectionModel(),
        SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
        this, SLOT(_OnTreeViewCurrentRowChanged(const QModelIndex &,
        const QModelIndex &)));
}

MyWidget::~MyWidget()
{

}

void MyWidget::_OnTreeViewCurrentRowChanged(
    const QModelIndex &rcqmiCurrIndex, const QModelIndex &rcqmiPrevIndex)
{
    if (bCondition) // some condition
    {
        _pTreeView->selectionModel()->blockSignals(true);

        // select previous index
        _pTreeView->selectionModel()->setCurrentIndex(
            rcqmiPrevIndex, QItemSelectionModel::SelectCurrent);

        _pTreeView->selectionModel()->blockSignals(false);

        // update view
        _pTreeView->viewport()->update();
    }

    bCondition = !bCondition;
}

_OnTreeViewCurrentRowChanged(...) 中,我可以看到树视图的 selection 模型的 "selection" 已更新为 QItemSelectionModel::setCurrentIndex(rcqmiPrevIndex, QItemSelectionModel::SelectCurrent);但是树视图的行 selection 没有更新,rcqmiCurrIndex 仍然是 selected.

我在这里错过了什么?

非常感谢任何帮助。提前致谢。

使用QItemSelectionModel::select()方法。

_pTreeView->selectionModel()->select(rcqmiPrevIndex, QItemSelectionModel::ClearAndSelect);

编辑

可能还有一个问题:_OnTreeViewCurrentRowChanged(...)应该是当前行改变时调用的槽吧?这意味着您在单个事件中两次更改选择。这不是一个好主意。使用QTimer在下一个事件循环周期中执行选择:

// lambda
auto func = [this, rcqmiPrevIndex](){
    _pTreeView->selectionModel()->select(rcqmiPrevIndex, QItemSelectionModel::ClearAndSelect);
}

// A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.
QTimer::singleShot(0, func);