Qt信号与枚举作为参数 [英] Qt signal with an enum as a parameter

查看:1031
本文介绍了Qt信号与枚举作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将枚举作为一个值传递给我的程序中的一个插槽,但是我有一些问题。在我的头文件中,我创建了枚举:

  Q_ENUMS(button_type); 
枚举button_type {button_back,button_up,button_down,button_ok};
Q_DECLARE_METATYPE(button_type);

在我的.cpp文件中,我试图将它传递给一个插槽:

  QObject :: connect(buttons-> ui.pushButton_back,SIGNAL(clicked()),这个,SLOT(input_handler(button_back))) ; 

但是当我编译代码时,我得到:



main_application.cpp:44
Object :: connect
Object :: connect :((接收者名称:'main_applicationClass')

它编译和工作如果我没有将参数传递给input_handler,则可以使用。



我也读过我应该调用qRegisterMetaType,但是我似乎无法得到正确的语法。这是我试过的:

  qRegisterMetaType< button_type>(button_type); 

但是我收到这个错误:

  main_application.h:15:1:error:specializing member':: qRegisterMetaType< button_type>'require'template<>'语法
pre>

任何人都可以为我做点光了吗?



谢谢!



Marlon

解决方案

信号和插槽需要有相同的参数。你想要的是一个QSignalMapper。



编辑:
这是一个来自我的应用程序的例子。它创建10个菜单操作,每个操作都连接到相同的插槽 gotoHistoryPage ,但每个都使用不同的 int 值调用。 / p>

  m_forwardMenu = new QMenu(); (int i = 1; i <= 10; i ++)
{
QAction * action = m_forwardMenu-> addAction(QString(%1)arg(i) ;
m_forwardActions.push_back(action);
m_signalMapper-> setMapping(action,i);
connect(action,SIGNAL(triggered()),m_signalMapper,SLOT(map()));
}
ui.forwardButton-> setMenu(m_forwardMenu);
connect(m_signalMapper,SIGNAL(mapped(int)),这个,SLOT(gotoHistoryPage(int)));


I'm trying to pass an enum as a value to a slot in my program, but I'm having some problems. In my header file I've created the enum:

Q_ENUMS(button_type);
enum button_type {button_back, button_up, button_down, button_ok};
Q_DECLARE_METATYPE(button_type);

And in my .cpp file I'm trying to pass it to a slot:

QObject::connect(buttons->ui.pushButton_back, SIGNAL(clicked()), this, SLOT(input_handler(button_back)));

But when I compile the code I get:

Object::connect: No such slot main_application::input_handler(button_back) in main_application.cpp:44
Object::connect:  (sender name:   'pushButton_back')
Object::connect:  (receiver name: 'main_applicationClass')

It compiles and works fine if I don't pass an argument to input_handler.

I've also read that I should be calling qRegisterMetaType, but I can't seem to get the syntax correct. Here's what I tried:

qRegisterMetaType<button_type>("button_type");

but I get this error:

main_application.h:15:1: error: specializing member ‘::qRegisterMetaType<button_type>’ requires ‘template<>’ syntax

Can anyone shed some light on this for me?

Thanks!

Marlon

解决方案

Signal and Slot need to have the same parameters. What you want is a QSignalMapper.

edit: Here is an example from an application of mine. It creates 10 menu actions that each are connected to the same slot gotoHistoryPage but each called with a different int value.

m_forwardMenu = new QMenu();
for(int i = 1; i<=10; i++)
{
    QAction* action = m_forwardMenu->addAction(QString("%1").arg(i));
    m_forwardActions.push_back(action);
    m_signalMapper->setMapping(action, i);
    connect(action, SIGNAL(triggered()), m_signalMapper, SLOT(map()));
}
ui.forwardButton->setMenu(m_forwardMenu);
connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(gotoHistoryPage(int)));

这篇关于Qt信号与枚举作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆