我试图在Qt中执行经典连接,我做了一千次。但由于某些原因我不知道,我现在不能:我收到错误
error: no matching function for call to 'gameView::connect(QComboBox*&, const char*, gameLogic*&, const char*)'
connect(_dropdown, SIGNAL(activated(int)), _model, SLOT(resize(int)));
^
我的代码如下: (gameview.h)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPushButton>
#include <QComboBox>
#include <QVector>
#include <QTime>
#include "gamelogic.h"
class gameView : public QWidget
{
Q_OBJECT
public:
...
QComboBox*_dropdown;
...
gameLogic* _model;
...
explicit gameView(QWidget *parent = 0);
~gameView();
};
#endif // MAINWINDOW_H
(gameview.cpp)
#include "gameview.h"
gameView::gameView(QWidget *parent) :
QWidget(parent)
{
_model = new gameLogic();
...
_dropdown = new QComboBox();
...
_dropdown->addItem("4");
_dropdown->addItem("6");
_dropdown->addItem("8");
connect(_dropdown, SIGNAL(activated(int)), _model, SLOT(resize(int))); //error here
...
}
(gamelogic.h)
#ifndef GAMELOGIC_H
#define GAMELOGIC_H
#include <QVector>
#include <QTimer>
class gameLogic
{
Q_OBJECT
public:
gameLogic();
...
public slots:
void resize(int newn);
};
#endif // GAMELOGIC_H
请帮忙!
答案 0 :(得分:3)
您的gameLogic
类应继承QObject,否则Qt信号/插槽机制将无效。尝试将class gameLogic
更改为class gameLogic : public QObject
答案 1 :(得分:1)
显然只是添加Q_OBJECT
是不够的。将gameLogic
设置为class gameLogic : public QObject
后,连接就可以了。