无法从抽象类调用按钮信号

cannot call button signal from abstract class

本文关键字:按钮 信号 调用 抽象类      更新时间:2023-10-16

我想在单击按钮时调用功能。该按钮的实现是在抽象类中。但是当我编译时,我会遇到此错误。

这是我的.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: user user documents breakout_final home.cpp:16:错误:无匹配 呼叫'qobject :: connect(qpushbutton*&amp;,const char*, 家*,const char*)' qObject ::连接(button 3,signal(clicked()),slot(startgame1()));

                                                                   ^

您在代码中有一个错误:为了使用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();    
};