如何在QTreeView中选择前一行
How to select previous row in a QTreeView?
我有一个QTreeView单列和多行(让我们说5行)。我想要实现的是,如果我选择了一行,我想在某些条件下重新选择前一行。
下面是我的代码:
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(…)中,我可以看到树视图的选择模型的"选择"与QItemSelectionModel::setCurrentIndex(rcqmiPrevIndex, QItemSelectionModel::SelectCurrent);
更新,但树视图的行选择未更新,仍然选择rcqmiCurrIndex
。
我在这里错过了什么?
任何帮助都是非常感激的。使用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);
相关文章:
- 如何使用默认参数等选择模板专业化
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 选择要调用的构造函数
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++选择排序算法中的逻辑错误
- 从C++dll访问C#中的一行主要参数
- QTreeView幻灯片多选后无法使用单击选择
- 选择的行为更改() 删除第一行
- 如何在按下按钮时在Qt表格视图中自动选择下一行?
- 我如何使一行代码可以选择,但在已经选择过一次后不重新选择
- C++Qt创建者,我可以评论每一行,而不仅仅是评论选择
- 何时选择一行的信号
- 从cin中选择下一行
- 如何以编程方式选择QTableView中的下一行
- 找到文本文件中的最后一行,选择前10个字符并打印到一个新文件中
- 当从表视图中选择一行时,为组合框指定一个int值
- QTreeWidget选择第一行
- 如何在QTreeView中选择前一行