0
votes

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

1
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
@MarkStevens just tried this and still got the missing vtable error like beforeJonMorehouse
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
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 itselfJonMorehouse
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: stackoverflow.com/questions/9370264/…Mark Stevens

1 Answers

1
votes

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