c++ - Cannot connect slots from another class -
i try write little application experiments , more. write in own file (menu -> menu.cpp etc.). want create menu action, works interacting action doesnt. thats i've done far:
menu.cpp
#include "menu.h" #include <qmenubar> #include <qmenu> #include <qaction> void menu::setupmenu(qmainwindow *window) { qmenubar *mb = new qmenubar(); qmenu *filemenu = new qmenu("file"); qaction *newact = new qaction("new...", window); window->connect(newact, signal(triggered()), this, slot(newfile())); filemenu->addaction(newact); mb->addmenu(filemenu); window->setmenubar(mb); } void menu::newfile() { printf("hello world!"); }
menu.h
#ifndef menu_h #define menu_h #include <qobject> #include <qmainwindow> #include <qwidget> #include <qmenubar> class menu : public qobject { public: void setupmenu(qmainwindow *window); private slots: void newfile(); }; #endif // menu_h
but not printing out 'hello world', message is:
qobject::connect: no such slot qobject::newfile() in ../from scratch written ui app c++/src/ui/menu.cpp:11
what can fix this?
~ jan
class menu : public qobject
menu
qobject
needs use q_object
macro.
see qt5 - qobject documentation:
the q_object macro must appear in private section of class definition declares own signals , slots or uses other services provided qt's meta-object system.
next, there confusion in connect
call. here signature of static connect
function.
qt5 - static qobject::connect:
qobject::connect(const qobject * sender, const char * signal, const qobject * receiver, const char * method, qt::connectiontype type = qt::autoconnection)
you can see takes 5 parameters (object pointer, signal, object pointer, signal/slot) , 5th parameter defaulted.
there member function connect
.
qobject::connect(const qobject * sender, const char * signal, const char * method, qt::connectiontype type = qt::autoconnection) const
this takes 4 parameters (object pointer, signal, signal/slot) , 4th parameter defaulted.
your code:
window->connect(newact, signal(triggered()), this, slot(newfile()));
you're calling the connect
member function of window
you're passing parameters static connect
function.
what can fix this?
figure out you're trying , make appropriate call.
for example: connect qaction
signal slot in menu
either call static function follows.
connect(newact, signal(triggered()), this, slot(newfile()));
or using member function.
connect(newact, signal(triggered()), slot(newfile()));
Comments
Post a Comment