添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
    void keyPressEvent(QKeyEvent *event);
    void startRemeberNode();
    void startExpandNode();
private:
    void recursiveRememberNodeState(NodeExpand& ne, QModelIndex root);
    void recursiveExpandNode(NodeExpand& ne, QModelIndex root);
    NodeExpand m_nodeExpand;
#endif // QTREEVIEWNOKBD_H



#include "TreeViewNoKbd.h"


QTreeViewNoKbd::QTreeViewNoKbd()
{
setUniformRowHeights(true);
}


void QTreeViewNoKbd::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Down:
case Qt::Key_Up:
break;
default:
QTreeView::keyPressEvent(event);
break;
}
}


void QTreeViewNoKbd::recursiveRememberNodeState(NodeExpand &ne, QModelIndex root)
{
int nChild = model()->rowCount(root);


for (int i = 0; i < nChild; i++)
{
QModelIndex index = model()->index(i, 0, root);
if (this->isExpanded(index))
{
NodeExpand n;
n.nRowNo = i;
recursiveRememberNodeState(n, index);
ne.arChildren.push_back(n);
}
}
}


void QTreeViewNoKbd::recursiveExpandNode(NodeExpand &ne, QModelIndex root)
{
QModelIndex index = model()->index(ne.nRowNo, 0, root);
if (!index.isValid())
{
return;
}


this->expand(index);
for (int i = 0; i < ne.arChildren.size(); i++)
{
recursiveExpandNode(ne.arChildren[i], index);
}
}




void QTreeViewNoKbd::startExpandNode()
{
QModelIndex root = this->rootIndex();
for (int i = 0; i < m_nodeExpand.arChildren.size(); i++)
{
this->recursiveExpandNode(m_nodeExpand.arChildren[i], root);
}
}


void QTreeViewNoKbd::startRemeberNode()
{
QModelIndex root = this->rootIndex();
recursiveRememberNodeState(m_nodeExpand, root);
}


只能记忆展开状态,滚动状态没支持,因为这个树控件 ,可能不使用了 ,半成品#ifndef QTREEVIEWNOKBD_H#define QTREEVIEWNOKBD_H#include &quot;Header.h&quot;typedef struct _NodeP{ int nRowNo; std::vector&amp;lt;_NodeP&amp;gt; arChildren;}NodeExpand;class ... 在实际用途中,我们需要在Treeview上添加节点后需要 保持 树的 展开 状态 ,但是qt的model刷新后,Treeview会自动收起,因此需要自己记录树的 状态 ,并在 数据 刷新后恢复原来的 展开 状态 //先记录下节点的 展开 状态 void MainWindow::GetExpandNode(QModelIndex root) int row = m_model->rowCount(root); for(int i=0;i<row;i++)
背景:最近用到了treeView控件,来显示指定文件夹里的所有文件,文件因为有 更新 ,因此实现了一个右键菜单点击刷新列表的功能。 本来以为这个功能很简单,只需要把原来的treeview或者model删除了,重新加载一次就好了,也看到有网友提供这样类似的方法,但是自己尝试了多次都没有实现,清空model->clear()后,程序直接崩溃,貌似原因是这样做了model将不能再次被使用。。。在网上看了很多资料,都没有找到一种合适的解决办法。 解决办法:最终自己只能用笨方法解决了。思路:遍历文件夹中的文件与tr
JSON(JavaScriptObjectNotation)是一种轻量级的结构化 数据 格式,相对于XML语法更简洁。它具有6种基本 数据 类型:bool(true或false字符串表示)、double(对应JS中number)、string、array(值的列表)、object(键值对集合)、null 。 虽然cJSON和JsonCpp也是常用的C/C++ JSON解析器,但是在Q... ui->treeView->setModel(model); //设置根目录位置 ui->treeView->setRootIndex(model->index("C:/Users/kashey/Desktop/Test")); ui->treeView->allColumnsShowFocus()...
QTreeView 是Qt框架中常用的视图控件,用于展示树形结构的 数据 展开 指定节点是 QTreeView 中一个常见的需求,通常可以通过以下步骤实现: 1. 获取指定节点的ModelIndex对象。使用 QTreeView 的model()方法获取其所使用的 数据 模型,使用该模型的index()方法获取节点的ModelIndex对象,该对象包含节点的行和列信息以及父子关系。 2. 调用 QTreeView 的expand()方法 展开 节点。使用所获取的ModelIndex对象调用 QTreeView 的expand()方法即可 展开 指定节点,该方法会自动 展开 该节点的所有父节点,并调用 数据 模型中的rowCount()方法获取子节点数量,再使用beginInsertRows()和endInsertRows()方法插入子节点。 示例代码如下: QModelIndex index = model->index(row, col, parentIndex); // 获取指定节点的ModelIndex对象 if(index.isValid()){ treeView->expand(index); // 展开 节点 其中,row、col和parentIndex分别表示节点的行、列和父节点的ModelIndex对象。如果节点在根节点下,则parentIndex可以使用QModelIndex()或treeView->rootIndex()表示。 需要注意的是, 展开 指定节点前需要先设置 QTreeView 数据 模型,可以通过 QTreeView 的setModel()方法设置。同时, 展开 指定节点前建议检查节点是否有效,即使用ModelIndex的isValid()方法判断节点是否存在。