This is inside a Menu class. The problem is addAction. This works, but there is no connection to slot:
QMenu* menu2 = new QMenu("Test");
menu2->addAction("Test");
When I do this:
QMenu* menu2 = new QMenu("Test");
menu2->addAction("Test", Menu, test);
I get compiler error: “error: expected primary-expression before ‘,’ token”
I mean to call the test() function in the Menu class. What am I doing wrong?
Well, the error comes from passing
Menuas an argument. You sayMenuis a class, and classes are not expressions on themselves.If you need to call
teston an instance ofMenu, whereMenuis not a derivate fromQObject(ie. no slots available), then you can just create a slot in the widget that contains theQMenuitself (probably aQMainWindow), and implement the call in there!Edit: to add an example.
Now, say that you’re populating the main window inside its constructor:
Of course, if you’re creating the menu outside that class, you’d need to make the slot public, but that’s another issue