1、new 一个QTreeWidget 对象,并设置头标签,和根节点(个人程序需要)

   QTreeWidget* treeWidget = ui.treeWidget;//我已经在ui设计师中拖了一个QTreeWidget

    QString headers;
headers = "Name" ;
treeWidget->setHeaderLabel(headers); QStringList rootText;
rootText << "wells";
root = new QTreeWidgetItem(treeWidget, rootText);

2、添加 QTreeWidgetItem(这里是读取文件夹里文件的名字作为item的名字)

    for(int i = ;i < fileList.size();i++)
{ QTreeWidgetItem *leaf = new QTreeWidgetItem(root, QStringList() <<fileList.at(i).fileName());
leaf->setFlags(leaf->flags() | Qt::ItemIsEditable);
root->addChild(leaf);
}

3、点击item事件

 

connect(treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)), this,SLOT(checkself(QTreeWidgetItem* ,int)));//检测点击事件,信号槽机制

4、checkself(QTreeWidgetItem* ,int);函数是点击后的响应函数。在private slot:  下声明(这里的具体实现就不贴了)

5、右键弹出菜单

connect(treeWidget,SIGNAL(customContextMenuRequested(const QPoint&)), this,SLOT(popMenu(const QPoint&)));//检测鼠标右键

6、弹出菜单的响应函数  popMenu(const QPoint&)

void LWD::popMenu(const QPoint&)
{
QTreeWidgetItem* curItem=treeWidget->currentItem(); //获取当前被点击的节点
if(curItem==NULL)return; //这种情况是右键的位置不在treeItem的范围内,即在空白位置右击
QString wellName = curItem->text();
if(wellName != "wells")
{
QAction deleteWell(QString::fromLocal8Bit("&删除该井"),this);//删除井
QAction reNameWell(QString ::fromLocal8Bit("&重命名井"),this);//重命名井
//在界面上删除该item
connect(&deleteWell, SIGNAL(triggered()), this, SLOT(deleteItem()));
connect(&reNameWell,SIGNAL(triggered()),this,SLOT(renameWell())); QPoint pos;
QMenu menu(ui.treeWidget);
menu.addAction(&deleteWell);
menu.addAction(&reNameWell);
menu.exec(QCursor::pos()); //在当前鼠标位置显示 }
}

7、deleteItem()

void LWD::deleteItem()
{
root->removeChild(treeWidget->currentItem());
if(myW != NULL)
{
myW->setParent(NULL);
ui.verticalLayout_4->removeWidget(myW);
}
//删除井数据文件
QString dirPath = "../Data1/";
dirPath.append(treeWidget->currentItem()->text());
dirPath.append("/");
DeleteDirectory(dirPath);//实现在下面 }
bool LWD::DeleteDirectory(const QString &path)
{
if (path.isEmpty())
return false; QDir dir(path);
if(!dir.exists())
return true; dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
QFileInfoList fileList = dir.entryInfoList();
foreach (QFileInfo fi, fileList)
{
if (fi.isFile())
fi.dir().remove(fi.fileName());
else
DeleteDirectory(fi.absoluteFilePath());
} return dir.rmpath(dir.absolutePath());
}

8、renameWell()

void LWD::renameWell()
{
preName = treeWidget->currentItem()->text();
prePath = "../Data1/";
prePath.append(preName);
ui.treeWidget->editItem(ui.treeWidget->currentItem());
//t通过监控itemChanged事件来确定修改后的名字!!!!
connect(treeWidget,SIGNAL(itemChanged( QTreeWidgetItem *,int )),this,SLOT(nameChanged(QTreeWidgetItem* ))); } void LWD::nameChanged(QTreeWidgetItem* item)
{
//先重命名文件夹
QString newName = treeWidget->currentItem()->text();
QString newPath = "../Data1/";
newPath.append(newName);
QFile::rename(prePath,newPath);
prePath = newPath.append("/");
prePath.append(preName);
prePath.append(".txt");
//重命名井眼轨迹处理后的文件
newPath.append("/");
newPath.append(newName);
newPath.append(".txt");
QFile::rename(prePath,newPath); }

QT5 QtreeWidget 实现点击item事件以及右键菜单删除item 和 重命名item的更多相关文章

  1. Extjs 4.2 panel 添加 click 事件及右键菜单

    listeners: { render: function(c) { c.body.on('click', function() { //TODO 添加点击事件功能 }); c.body.on('co ...

  2. HTML5事件-自定义右键菜单

    WEB领域中,为实现上下文菜单,开发人员面临的主要问题是如何确定应该显示这个上下文菜单(Windows 中,右键单击:Mac 中,Ctrl+单击), 以及如何屏蔽与该操作相关联的默认上下文菜单. 解决 ...

  3. c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 .

    // c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 分类: c# 2013-02-06 15:18 3008人阅读 评论(0) 收藏 举报 可以实现 ...

  4. 昨天所写的JQ 点击隐藏事件,关键性原理

    JQ 点击隐藏事件,关键性原理 1.JQ 库的调用 一般选择为: 1)库越小越好 2)库的功能越强大越好 <script src="js/jquery.js" type=&q ...

  5. 深入A标签点击触发事件而不跳转的详解

    本文介绍下,当点击A标签时,触发事件但不跳转的实现方法,有需要的朋友参考下吧. 点击页面上的空链接,点击后页面自动刷新,并会定位到页面顶端. 不过,有时需要点击#页面但不作跳转,可以这样写: < ...

  6. v-charts修改点击图例事件,legendselectchanged

    html: <!--折线图--><ve-line :extend="item.chartExtend" :data-zoom="dataZoom&quo ...

  7. Vue 框架-02-事件:点击, 双击事件,鼠标移上事件

    Vue 框架-02-事件:点击, 双击事件,鼠标移上事件 1.单击事件:v-on:click 源码 app2.js : //实例化 vue 对象 new Vue({ //注意代码格式 //el:ele ...

  8. js去掉浏览器右键点击默认事件(+vue项目开启右键行为)

    js去掉浏览器右键点击默认事件 1.阻止整个页面所有的右击事件 document.oncontextmenu = function(){ return false;} 2.特定的区域/元素 docum ...

  9. jq自定义下拉菜单,在点击非当前下拉菜单区域时,关闭下拉菜单(点击事件的对象不是目标元素本身)

    jq自定义下拉菜单,在点击非当前下拉菜单区域时,关闭下拉菜单(点击事件的对象不是目标元素本身) //点击非当前下拉菜单区域时,关闭下拉菜单 $(document).mousedown(function ...

随机推荐

  1. JavaScript数据结构与算法-列表练习

    实现列表类 // 列表类 function List () { this.listSize = 0; // 列表的元素个数 this.pos = 0; // 列表的当前位置 this.dataStor ...

  2. 【转】 HMC与VIOS对新LPAR提供存储与网络虚拟化的支持

    前面的几篇博文的操作环境都是在IVM下,IVM可以看作是VIOS的一部分,或者是对VIOS功能的一个扩展,一个IVM只能管理1台物理服务器,而HMC则是一对多.在有HMC来管理物理服务器的情形下,VI ...

  3. 一篇搞定spring Jpa操作数据库

    开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...

  4. 常用代码块:java使用系统浏览器打开url

    方法一:用于windows try { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+url) ...

  5. python实现复制整个目录的方法

    import shutil #复制文件 shutil.copyfile('listfile.py', 'd:/test.py') #复制目录 shutil.copytree('d:/temp', 'c ...

  6. 深度学习:Keras入门(一)之基础篇(转)

    转自http://www.cnblogs.com/lc1217/p/7132364.html 1.关于Keras 1)简介 Keras是由纯python编写的基于theano/tensorflow的深 ...

  7. 剑指offer 面试22题

    面试22题: 题目:链表中倒数第k个节点 题:输入一个链表,输出该链表中倒数第k个结点. 解题思路:为了实现只遍历链表一次就能找到倒数第k个节点,我们可以定义两个指针.让第一个指针先向前走k-1步,第 ...

  8. ZOJ 3959 Problem Preparation 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 AC代码 #include <cstdio> ...

  9. C# 二进制序列化(BinaryFormatter),Xml序列化(XmlSerializer),自己模拟写一个Xml序列化过程。

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 财经世界(2)A股B股和H股

    在发行过程中,公司通过章程给不同股份形式赋予不同的权益,使公司股份出现A股.B股等形式.我国上市公司的股票有A股.B股.H股.N股.S股等的区分.这一区分的主要依据股票的上市地点和所面对的投资者而定. ...