无法将QPushButton信号连接到QGraphicsView插槽

时间:2012-11-30 21:42:32

标签: c++ qt qgraphicsview qpushbutton qt-signals

我无法将QPushButton中的信号连接到QGraphicsView的插槽中。

我的按钮标题:

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

};

你需要Q_Object宏来编译这个插槽,但是当我编译时,我一直得到一个vtable引用,如下所示:

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

当我拿出宏时,我可以很好地编译它,但是当我运行时我一直收到这个错误:

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

我的game_controller扩展了QGRaphicsView,这是我尝试连接按钮的代码:

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()));

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

保留Q_OBJECT,moc需要它

不要写信号的主体,moc会为所有信号生成代码。

不要处理mousePressedEvent,处理QAbstractButton及其所有子类上可用的clicked()信号