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

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

任何帮助将不胜感激

最佳答案

保留 Q_OBJECT,moc 需要它

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

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

关于c++ - 无法将 QPushButton 信号连接到 QGraphicsView 插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13653622/

相关文章:

qt - graphicsView scale 在 difficultground 中忽略

c++ - 如何用VC在win32上编译文字转语音节语音免费库

C++:使用for循环创建多个数据结构

c++ - 如果条件为单个和 (&)

c++ - 奇怪的执行时间

c++ - 删除指向小部件 Qt C++ 的指针

qt - QSqlDatabase 在调试中工作正常,但在发布中则不行(需要 QCoreApplication)

c++ - 在 QGraphicsScene 的特定坐标中放置和图像

c++ - 在 QML 插件中注册一个 C++ 抽象类并从 QML 中引用它

c++ - Qt Widgets 和派生类