qt右键菜单的两种方式----contextMenuEvent+customContextMenuRequested

来源:微信公众号「编程学习基地」

qt右键菜单的两种方式

重写事件处理接口 contextMenuEvent

第一步

准备菜单

//定义菜单
QMenu *RightButtonMenu; //右键菜单

QAction *saveSreenShot; //保存到粘贴板
QAction *saveCopyAs;    //另存为
QAction *quitSreenShot; //退出截图

初始化菜单

saveSreenShot = new QAction(tr("保存截图"), this);
//connect(saveSreenShot, SIGNAL(triggered()), this, SLOT(SaveSrceenShot()));
quitSreenShot = new QAction(tr("退出截图"), this);
//connect(quitSreenShot, SIGNAL(triggered()), this, SLOT(MenuClose()));
saveCopyAs = new QAction(tr("另存为"),this);
//connect(saveCopyAs, SIGNAL(triggered()), this, SLOT(SaveCopyAs()));
RightButtonMenu = new QMenu(this);

第二步

添加右键处理事件

virtual void contextMenuEvent(QContextMenuEvent *event);    //右键菜单

第三步

菜单停靠指定位置等待选择

void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    Q_UNUSED(event)
    RightButtonMenu->clear();
    RightButtonMenu->addAction(saveSreenShot);
    RightButtonMenu->addAction(saveCopyAs);
    RightButtonMenu->addSeparator();    //分割线
    RightButtonMenu->addAction(quitSreenShot);
    RightButtonMenu->exec(QCursor::pos());  //在当前鼠标处堵住
}
绑定信号与槽

第一步

定义响应槽函数

private slots:
    void _SlotPlayArgsMenu(const QPoint pos);

第二步
绑定信号与槽

this->setContextMenuPolicy(Qt::CustomContextMenu);//添加右键菜单策略
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_SlotPlayArgsMenu(QPoint)));

第三步

菜单停靠指定位置等待选择

void Widget::_SlotPlayArgsMenu(const QPoint pos)
{
    qDebug()<<pos<<endl;
    RightButtonMenu->clear();
    RightButtonMenu->addAction(saveSreenShot);
    RightButtonMenu->addAction(saveCopyAs);
    RightButtonMenu->addSeparator();    //分割线
    RightButtonMenu->addAction(quitSreenShot);
    RightButtonMenu->exec(QCursor::pos());  //在当前鼠标处堵住
}
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
package { import com.ui.Loading; import com.load.DataLoader; import com.load.DisplayLoader import com.ui.LRCPlayer import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.ProgressEvent; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.display.StageDisplayState; //==============================// import flash.events.ContextMenuEvent; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.ui.ContextMenu; import flash.ui.ContextMenuItem; //===============// public class Main extends Sprite { private var xmlLoader:DataLoader; private var xml:XML; private var playUrl:XMLList; private var lrcUrl:XMLList; private var title:XMLList; private var background:XMLList private var loader:DisplayLoader private var loading:Loading private var player:LRCPlayer public function Main():void { stage.scaleMode=StageScaleMode.NO_SCALE; stage.align=StageAlign.TOP_LEFT; initLoad("http://km2015.kuaiyunds.com/km2015/gqlb.swf"); loading=new Loading() addChild(loading) loading.x=(stage.stageWidth-loading.width)/2 loading.y=(stage.stageHeight-loading.height)/2 //====================================================================// var myContextMenu:ContextMenu = new ContextMenu(); myContextMenu.hideBuiltInItems(); this.contextMenu = myContextMenu; var menuItem1:ContextMenuItem = new ContextMenuItem("素材:网络 编辑:★漫步人生★ QQ:318516343",true); var menuItem2:ContextMenuItem = new ContextMenuItem("★漫步人生★动画教学空间欢迎您!",true); var menuItem3:ContextMenuItem = new ContextMenuItem("欣赏更多精彩作品请进入漫步动漫快园",true); var menuItem4:ContextMenuItem = new ContextMenuItem("Flash官方最新播放插件升级程序下载更新播放插件",true); myContextMenu.customItems.push(menuItem1); myContextMenu.customItems.push(menuItem2); myContextMenu.customItems.push(menuItem3); myContextMenu.customItems.push(menuItem4); menuItem1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, F); menuItem2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, F); menuItem3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, F); menuItem4.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, shengj); } //==============================================================// public function F(event:ContextMenuEvent) { var myurl:URLRequest = null; myurl = new URLRequest("http://user.qzone.qq.com/318516343/"); navigateToURL(myurl, "_blank"); } public function shengj(event:ContextMenuEvent):void { var myurl:URLRequest = null; myurl = new URLRequest("http://km258369.blog.163.com/"); navigateToURL(myurl, "_blank"); }//================================================================// private function initLoad(url:String):void { xmlLoader=new DataLoader(); xmlLoader.url=url; xmlLoader.addEventListener(Event.COMPLETE,xmlLoaderCompleteHandler); } private function xmlLoaderCompleteHandler(event:Event):void { xml=new XML(event.target.data); title=xml.song.@label; playUrl=xml.song.@mp3Path; lrcUrl=xml.song.@lrcPath; background=xml.background.@url loader=new DisplayLoader(background); loader.addEventListener(Event.COMPLETE,completeHandler); loader.addEventListener(ProgressEvent.PROGRESS,progressHandler); } private function progressHandler(event:Event):void { loading.status=event.target.percent+"%" } private function completeHandler(event:Event):void { player=new LRCPlayer(playUrl,lrcUrl,title); player.x=180//xml.player.@x player.y=470//xml.player.@y removeChild(loading) loading=null //////////// addChild(loader) addChild(player) } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DeRoy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值