【QT】QT实现添加右键菜单功能

QT实现添加右键菜单功能

目录

1.添加头文件

2.添加.h文件代码

3.添加.cpp文件代码

删除文件功能

4.效果


1.添加头文件

#include <QContextMenuEvent>
#include <QProcess>
#include <QTableView>
#include <QModelIndex>

2.添加.h文件代码

public:
    QTableView *tbView1;
    QStandardItemModel *model1;
protected:
    virtual void contextMenuEvent(QContextMenuEvent *event);  //右键菜单事件

private slots:

3.添加.cpp文件代码

void Softdog::contextMenuEvent(QContextMenuEvent *event)
{

    if(!tbView1->isHidden()){  //当前在解密文件夹解密

    //创建一个菜单 添加事件
    QMenu *menu1 = new QMenu(tbView1);
    QAction *openFile = new QAction("打开",tbView1);
    QAction *deleteFile = new QAction("删除",tbView1);
    QAction *cancel = new QAction("取消",tbView1);
//    QMenu *menu1 = new QMenu(tWidget);
//    QAction *openFile = new QAction("打开",tWidget);
//    QAction *cancel = new QAction("取消",tWidget);
    menu1->addAction(openFile);
    menu1->addAction(deleteFile);
    menu1->addAction(cancel);


//    QPoint pt = tbView1->mapFromGlobal(QCursor::pos());
//    openfile_rownum = tbView1->indexAt(pt).row();
    QModelIndex index = tbView1->selectionModel()->currentIndex();
    openfile_rownum = index.row();

//    qDebug() << "openfile_rownum:" << openfile_rownum;
    deletefile_rownum = openfile_rownum;


    connect(openFile,SIGNAL(triggered(bool)),this,SLOT(openfile()));
    connect(deleteFile,SIGNAL(triggered(bool)),this,SLOT(handle_deletefile()));
    connect(cancel,SIGNAL(triggered(bool)),this,SLOT(cancel()));

    //让菜单移动鼠标位置并显示
    menu1->exec(QCursor::pos());
    }
}

删除文件功能

void Softdog::handle_deletefile()
{


//    qDebug() <<"deletefile:" << deletefile_rownum;
    QString path = pathList[deletefile_rownum];
//    qDebug() << "path:" << path <<";";
    QString cout = QString::number( pathList.count(),10);

//    qDebug() << "count:" << pathList.count();
    deletefile(path);


    //重新加载
    read_dir(privateName,privatePath);
    //并删除没有更新的最后一栏
    model1->removeRow(pathList.count());

}

bool Softdog::deletefile(const QString &path)
{
    if(path.isEmpty()){
        return false;
    }
    if(deletefile_rownum == -1){
        return false;
    }
//    QDir dir(path);
//    if(!dir.exists()){
//        return true;
//    }
//    if(deletefile_rownum >= 0){

//        dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot );
//        QFileInfoList fileList = dir.entryInfoList();
//        foreach (QFileInfo file, fileList) {

//            if(file.isFile()){ //是文件,删除
//                file.dir().remove(file.fileName());

//                qDebug() << file.fileName();
//                QProcess *pro_del=new QProcess;
//                pro_del->start("rm "+file.fileName());

//            }
//            else{   //递归删除
//                deletefile(dir.absolutePath());
//            }
//        }
//    }
//    return dir.rmpath(dir.absolutePath()) ;
    QProcess *pro_del=new QProcess;
    pro_del->start("rm -rf "+path);
    pro_del->waitForFinished(3000);
    return true;
}

4.效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GLL_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值