zoukankan      html  css  js  c++  java
  • qt5右键菜单

    win.h

    #ifndef WIN_H
    #define WIN_H
    
    #include <QWidget>
    #include <QContextMenuEvent>  //用于产生右键事件
    #include <QMenu>    //用于生成右键菜单
    #include <QAction>  //用于添加菜单栏动作
    #include <QCursor>  //用于获取当前光标位置
    #include <QProcess> //用于启动记事本
    #include <QDebug>
    #include <QLabel>
    
    
    class win : public QWidget
    {
        Q_OBJECT
    
    public:
        win(QWidget *parent = nullptr);
        ~win();
    
    protected:
        void contextMenuEvent(QContextMenuEvent *event);
    
    private slots:
        //void on_newScreenShotButton_clicked();
        //void shotScreenSlot();
        void saveScreenSlot(); //保存截图
        void startNotepadSlot(); //启动记事本
    
    private:
        QLabel* label;
    
    };
    #endif // WIN_H

    win.cpp

    #include "win.h"
    
    win::win(QWidget *parent)
        : QWidget(parent)
    {
        this->resize(400,300);
        label=new QLabel("右击事件",this);
    
    }
    
    win::~win()
    {
    }
    
    void win::contextMenuEvent(QContextMenuEvent *event)  //右键事件
    {
        QMenu *menu= new QMenu(this); //创建菜单
        QAction *action =new QAction(this); //创建动作
        QAction *processAction =new QAction(this);
        connect(action,SIGNAL(triggered()),this,SLOT(saveScreenSlot()));
        //点击action动作时,调用saveScreenSlot()槽函数
        connect(processAction,SIGNAL(triggered()),this,SLOT(startNotepadSlot()));
        //点击processAction动作时,调用startNotepadSlot()槽函数
        action->setText("另存为");//给动作设置文本
        processAction->setText("启动记事本");
        menu->addAction(action); //把动作添加到菜单
        menu->addSeparator();//添加分割线
        menu->addAction(processAction);
        menu->exec(QCursor::pos()); //在光标当前位置处出现
    
    }
    
    void win::saveScreenSlot()//保存截图槽函数
    {
        qDebug()<<"保存为......";
    }
    
    void win::startNotepadSlot()  //启动记事本槽函数
    {
        QProcess *process =new QProcess;
        process->start("notepad");
    }

  • 相关阅读:
    ruby 中 raise 抛出异常
    ruby中attr_accessor方法的理解
    Redis实现分布式缓存
    应用服务器集群概念
    反向代理和正向代理区别
    如何限制同一用户同时登录多台设备?
    Docker 初始
    Java 的反射机制你了解多少?
    JWT 实战
    判断 uniapp 项目运行到 什么机型
  • 原文地址:https://www.cnblogs.com/liming19680104/p/15647267.html
Copyright © 2011-2022 走看看