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

问题描述 投票:-1回答:1

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

这是我的基类的.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 *&,const char *,home *,const char *)'QObject ::连接(BUTTON3,SIGNAL(点击()),对此,SLOT(startgame1()));

                                                                   ^
c++ qt qt-signals
1个回答
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();    
};
© www.soinside.com 2019 - 2024. All rights reserved.