Reputation:
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
Upvotes: 2
Views: 3913
Reputation: 12215
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)));
Upvotes: 3
Reputation: 12547
Object::connect: No such slot main_application::input_handler(button_back)
Of course, there is, because signature is main_application::input_handler(button_type)
, and button_back
is a value, not type. And even you make right signature, you will not be able to connect that signal and slot due to their signature mismatch.
Also, you can always use QObject::sender()
function to get known, what button was pressed.
Upvotes: 0
Reputation: 11638
You're passing the SLOT()
macro a value when it's expecting a type. More fundamentally this doesn't make much sense anyway as what you're struggling to achieve is to pass the slot a constant. Why not just use button_back
in the slot function directly?
You can define a slot which takes a button_type
value, but then you'd need to connect it to a signal that passes one as a parameter.
What are you actually trying to do?
Upvotes: 1