0

I'm having trouble connecting a signal in a QPushButton to a slot in my QGraphicsView.

My Push Button Header:

class Button : public QPushButton {

    Q_OBJECT

    public://public constructors / destructors

        Button(Game_state * game_state, QWidget *parent = 0);
        ~Button();

    protected://signals / slots etc QT 
        void mousePressEvent(QMouseEvent * event);//

    signals:
        void updated() { std::cout << "HELLO FROM THE UPDATED METHOD" << std::endl;}

    protected:
        Game_state * game_state;//this is the global gamestate method
        char * action_name;//this is the application name that is responsible for setting the game_state so the game controller knows what to delete / update

};

You need the Q_Object macro to compile this with slots, but when I compile I keep getting a vtable reference not found like follows:

Undefined symbols for architecture x86_64:
  "vtable for Button", referenced from:
      Button::~Button()in buttons.o
      Button::~Button()in buttons.o
      Button::~Button()in buttons.o
      Button::Button(Game_state*, QWidget*)in buttons.o
      Button::Button(Game_state*, QWidget*)in buttons.o

When I take out the macro, I can compile it fine, but I keep getting this error when I run:

Object::connect: No such signal QPushButton::updated() in implementation/game_controller.cpp:11

My game_controller extends QGRaphicsView and here is my code where I am attempting to connect the Button:

this->button = new Button(game_state);
this->proxy = new QGraphicsProxyWidget();
this->proxy = this->scene->addWidget(this->button);

connect(this->button, SIGNAL(updated()), this, SLOT(update()));

Any help would be greatly appreciated

Andriy M
  • 76,112
  • 17
  • 94
  • 154
JonMorehouse
  • 1,313
  • 6
  • 14
  • 34
  • I've tripped over this. You definitely need Q_OBJECT for the signal/slots to work. After you put it back, did you re-run qmake? Actually "make clean, qmake and make" just out of paranoia. I didn't put this as an answer, because I'm only 93% sure, but think that will fix it. – Mark Stevens Nov 30 '12 at 21:48
  • @MarkStevens just tried this and still got the missing vtable error like before – JonMorehouse Nov 30 '12 at 21:51
  • OK, the 2nd most common reason you get this in Qt (and more like what the message actually says) is that a virtual method is defined but not implemented. Does your mousePressEvent() have an implementation? How about your constructors/destructors? – Mark Stevens Nov 30 '12 at 21:59
  • Yes, all of those are implemented perfectly. When I take out the qobject, I had the clicking functionality working (but the slot was in the pushbutton itself). I just read somewhere else that I need to use a qwidget which instantiates the pushbutton itself – JonMorehouse Nov 30 '12 at 22:00
  • Sorry, really thought I could help you with that. Here's a guy that had the *exact* same problem - maybe one of those suggestions will work for you: http://stackoverflow.com/questions/9370264/qt-c-error-symbols-not-found-for-architecture-x86-64-collect2-ld-retur – Mark Stevens Nov 30 '12 at 22:03
  • This is is usually caused by a problem with the .o files not being generated/replaced properly. As an additional verification, go to your output directory (where the .o files end up) and delete everything that is in there manually, then rebuild again. – Arnold Spence Nov 30 '12 at 22:51
  • @ArnoldSpence All of my .o files are deleted and it still doesn't work – JonMorehouse Dec 01 '12 at 00:02
  • Well, lets try seeing the .cpp code then :) – Arnold Spence Dec 01 '12 at 00:22
  • Why do you have a body inside the signal declaration? – Daniel Castro Dec 01 '12 at 01:49

1 Answers1

1

Keep Q_OBJECT, you need it for moc

Don't write the body of your signal, moc generates code for all signals.

Don't handle mousePressedEvent, handle the signal clicked () which is available on QAbstractButton and all of its child classes

cppguy
  • 3,611
  • 2
  • 21
  • 36