• QT5 QtreeWidget 实现点击item事件以及右键菜单删除item 和 重命名item


    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 = 2;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(0);
        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(0));
        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(0);
        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(0);
        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);
    
    }
  • 相关阅读:
    Mysql 重做日志及与二进制日志的区别
    【MySql】性能优化之分析命令
    ubuntu一些基本软件安装方法
    Linux学习笔记
    exp/imp三种模式——完全、用户、表
    Oracle学习日志20150302
    如何在macOS Sierra中运行CORE Keygen破解程序
    国内各大互联网公司技术站点集合
    React Native资料
    React Native集成到现有项目(非cocoa pods)
  • 原文地址:https://www.cnblogs.com/ling123/p/5503465.html
Copyright © 2020-2023  润新知