送分2 ~关于点击QAction QMenu不消失的问题的解决方法
具体问题http://bbs.csdn.net/topics/390275308
经过分析源码后,发现其实action的触发是由menu来控制的。当QMenu收到QMouseReleased事件后,会根据坐标找到当前的QAction并激发。然后自己隐藏。
#include "mymenu.h"
#include <QApplication>
#include <QDebug>
MyMenu::MyMenu(QWidget *parent) :
QMenu(parent)
{
//add action
QAction *action1 = new QAction("1",this);
QAction *action2 = new QAction("2",this);
QAction *action3 = new QAction("3",this);
QAction *action4 = new QAction("4",this);
action1->setCheckable(true);
action2->setCheckable(true);
action3->setCheckable(true);
action4->setCheckable(true);
action1->setProperty("canHideMenu","true");
action2->setProperty("canHideMenu","true");
action3->setProperty("canHideMenu","false");
action4->setProperty("canHideMenu","false");
this->addAction(action1);
this->addAction(action2);
this->addAction(action3);
this->addAction(action4);
}
void MyMenu::mouseReleaseEvent(QMouseEvent *e)
{
QAction *action = this->actionAt(e->pos());
if(action)
{
QString strCanHide = action->property("canHideMenu").toString();
qDebug() << "MyMenu::mouseReleased: " << strCanHide;
if(strCanHide == "true")
{
QMenu::mouseReleaseEvent(e);
return;
}
else
{
action->activate(QAction::Trigger);
return;
}
}
QMenu::mouseReleaseEvent(e);
}
[最优解释]
mark一下,说不定以后会碰到相同的问题
[其他解释]
周一结贴。。来就送分了。。
[其他解释]
该回复于2012-11-11 09:51:33被管理员删除