不能从抽象类调用按钮信号

时间:2017-12-03 10:35:47

标签: c++ qt qt-signals

我想在点击按钮时调用一个函数。按钮的实现在抽象类中。但是当我编译时,我收到了这个错误。

这是基类的.h文件

#ifndef HOME_H
#define HOME_H
#include<QGraphicsScene>
#include <QGraphicsScene>
#include<QPushButton>

class home
{
  Q_OBJECT
public:
  home();

  virtual void set_home_background()=0 ;
  QGraphicsScene *scene3;
  QPushButton *button3;

private slots:
  virtual void startgame1();
};

#endif // HOME_H

这是基类

#include "home.h"
#include<QGraphicsScene>
#include<QGraphicsProxyWidget>
#include "QMessageBox"

home::home()
{

}

void home::set_home_background()
{
  button3 = new QPushButton;
  QObject::connect(button3,SIGNAL(clicked()),this,SLOT(startgame1()));
  QGraphicsProxyWidget *proxy = this->scene3->addWidget(button3);
  button3->setAutoFillBackground(true);
  button3->setIcon(QIcon(":/Images/ng.png"));
  button3->setIconSize(QSize(131,41));
  proxy->setPos(130,430);
  scene3->addItem(proxy);
}

void home::startgame1()
{
  QMessageBox q;
  q.setText("");
  q.exec();
}

我收到此错误

  

C:\ Users \ User \ Documents \ breakout_final \ home.cpp:16:错误:无匹配   函数调用'QObject :: connect(QPushButton *&amp;,const char *,   home *,const char *)'   的QObject ::连接(BUTTON3,SIGNAL(点击()),对此,SLOT(startgame1()));

                                                                   ^

1 个答案:

答案 0 :(得分:0)

你的代码中有错误:为了使用Qt信号和插槽,你应该从QObject继承你的类,Q_OBJECT声明本身是不够的:

#include <QObject>

class home : public QObject
{
    Q_OBJECT
public:
    home();


    virtual void set_home_background()=0 ;
     QGraphicsScene *scene3;
     QPushButton *button3;

private slots:
    virtual void startgame1();    
};