qt槽函数如何传递多个参数_怎样把槽函数名称作为函数的参数传递给connect ?

本文介绍了如何在QT中使用QMetaObject::invokeMethod动态调用槽函数,特别是在需要根据不同的参数调用不同槽函数的场景下。通过将槽函数名称作为字符串传递,然后转换为char*,可以实现动态调用,同时检查了多种对象进行方法调用,确保函数执行成功。
摘要由CSDN通过智能技术生成

我倒是有一个方案,不要使用connect 试图来创建你的槽函数。 QT有一个全局的QMetaObject::invokeMethod(this,actionMember,Qt::DirectConnection);

具体实现:void CGmRbmMenuBaseConfiger::slotActionResponse()

{

//warning : do not change any char....all RBM action call this function....

QAction *pClickedAction = dynamic_cast(sender());

QString strActionMember = pClickedAction->data().toString();

QByteArray arr (strActionMember.toLatin1());

const char* actionMember = arr.constData();

//invoke method member must slot function.

QObject *pInvokeObj=  reinterpret_cast(m_pRbmMenu->property("InvokeObj").value());

bool b = QMetaObject::invokeMethod(m_pRbmResponser,actionMember,Qt::DirectConnection);

if(!b)//invoke method from "m_pActionResponser".

{

b = QMetaObject::invokeMethod(m_pActionResponser,actionMember,Qt::DirectConnection);

}

if(!b)

{

b =QMetaObject::invokeMethod(this,actionMember,Qt::DirectConnection);

}

if(!b && pInvokeObj != NULL)

{

b =QMetaObject::invokeMethod(pInvokeObj,actionMember,Qt::DirectConnection);

}

#ifndef NDEBUG

if(!b)

{

QMessageBox::information(NULL,"Invoked Method failed. ","slot function "+strActionMember+" connect failed ,check you function name.");

}

#endif

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值