qt - Implement slots in derived class - qt4/c++ -


i'm trying make example communicate (which can found @ web-tutorial first program in qt4), in different way: buttons 'plus' , 'minus' not built @ constructor, calling member function of child class, inherits qwidget class attributes:

mywidget class mywidget.h

#ifndef _mywidget_h #define _mywidget_h #include <qapplication> #include <qdesktopwidget> #include <qwidget> #include <qicon> #include <qpushbutton> #include <qlabel> class mywidget : public qwidget{      public:         mywidget(qwidget *parent = 0);          void setpmbutton(int,int,int,int,int,int);     private:         qlabel *label;     private slots:         void onplus();         void onminus();    }; #endif 

method's implementations mywidget.cpp

#include "mywidget.h" mywidget::mywidget(qwidget *parent) : qwidget(parent){     label = new qlabel("0", this);     label->setgeometry(190, 80, 20, 30); } void mywidget::onplus(){     int val = label->text().toint();     val++;     label->settext(qstring::number(val)); } void mywidget::onminus(){     int val = label->text().toint();     val--;     label->settext(qstring::number(val)); } void mywidget::setpmbutton(int x1, int y1, int x2, int y2, int w, int h){     qpushbutton *plus = new qpushbutton("+", this);     plus->setgeometry(x1, y1, w, h);      qpushbutton *minus = new qpushbutton("-", this);     minus->setgeometry(x2, y2, w, h);      connect(plus, signal(clicked()), this, slot(onplus()));     connect(minus, signal(clicked()), this, slot(onminus())); } 

and in main file test.cpp, written:

#include "mywidget.h" int main(int argc, char *argv[]){      qapplication app(argc, argv);          mywidget window;          window.setpmbutton(50,120,50,200,75,30);                 window.show();      return app.exec(); } 

but once run application, shows window, buttons don't work. @ terminal shows message:

object::connect: no such slot qwidget::onplus() object::connect: no such slot qwidget::onminus() 

is there way make work?

you forgot add q_object macro.

#includes here ...  class mywidget : public qwidget{  q_object   ///      <---- here  public:     mywidget(qwidget *parent = 0);  ... ... 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -