简体   繁体   English

将QPushButton信号连接到QGraphicsView插槽时出现问题

[英]Trouble Connecting QPushButton Signal to QGraphicsView Slot

I'm having trouble connecting a signal in a QPushButton to a slot in my QGraphicsView . 我在将QPushButton的信号连接到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: 您需要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

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: 我的game_controller扩展了QGRaphicsView ,这是我尝试连接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 任何帮助将不胜感激

Keep Q_OBJECT, you need it for moc 保留Q_OBJECT,moc需要它

Don't write the body of your signal, moc generates code for all signals. 不要写信号的主体,moc会为所有信号生成代码。

Don't handle mousePressedEvent, handle the signal clicked () which is available on QAbstractButton and all of its child classes 不处理mousePressedEvent,处理在QAbstractButton及其所有子类上可用的clicked()信号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM