#cpp #qt
Почему ругается? Гуглил, не могу найти ответа.
Слот описал, метод описал, связать не получается.. Помогите, пожалуйста.
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timer_overflow()));
C:\Users\Vesbat\Desktop\Test\map.cpp:18: ошибка: no matching function
for call to 'QObject::connect(QTimer*&, const char*, Map*, const
char*)'
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timer_overflow()));
Пытаюсь коннект выполнить в своем классе map.cpp:
#include "map.h"
#include
#include
Map::Map(int height, int weight){
}
void Map::timer_overflow(){
timer->stop();
}
void Map::move(QLabel *_hero){
hero=_hero;
timer = new QTimer;
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timer_overflow()));
timer->start(110);
}
void Map::initialize(int i, int j, QPushButton *btn){
massMap[i][j]=btn;
}
Map::~Map(){
}
Собственно, вот map.h:
#ifndef MAP_H
#define MAP_H
#include
#include
#include
#include
#include
#include
class Map{
Q_OBJECT
public slots:
void timer_overflow();
public:
struct coordinate{
int _x,_y;
};
QLabel *hero;
QTimer *timer;
QPushButton *massMap[5][5];
Map(int height, int weight);
~Map();
void move(QLabel *hero);
void initialize(int i, int j, QPushButton *btn);
};
#endif // MAP_H
Ответы
Ответ 1
Для реализации механизма сигналов и слотов, необходимо, чтобы ваш класс был наследником QObject и имел макрос Q_OBJECT. Также вам необходимо запустить qmake во вкладке Сборка (QtCreator). Подробнее вы можете прочитать по ссылкам: https://habrahabr.ru/post/50812/ https://habrahabr.ru/post/141983/ http://doc.qt.io/qt-4.8/signalsandslots.html Ваш же код должен выглядеть вот так: map.h #ifndef MAP_H #define MAP_H #include#include #include #include #include #include #include #include class Map: public QObject { Q_OBJECT public: struct coordinate{ int _x,_y; }; QLabel *hero; QTimer *timer; QPushButton *massMap[5][5]; Map(int height, int weight, QObject * parent = 0); ~Map(); void move(QLabel *hero); void initialize(int i, int j, QPushButton *btn); public slots: void timer_overflow(); }; #endif // MAP_H map.cpp #include "map.h" Map::Map(int height, int weight, QObject *parent): QObject(parent) { qDebug() << "Конструктор класса Map" << height << weight; } void Map::timer_overflow(){ timer->stop(); } void Map::move(QLabel *_hero) { hero = _hero; timer = new QTimer; connect(timer, SIGNAL(timeout()), this, SLOT(timer_overflow())); timer->start(110); } void Map::initialize(int i, int j, QPushButton *btn) { massMap[i][j] = btn; } Map::~Map() { } Ответ 2
И источник и приёмник сигналов должен наследоваться от QObject и иметь макрос Q_OBJECT. Это если использовать старый синтаксис, если это Qt5, то можно воспользоваться новым синтаксисом: QObject::connect(timer, &QTimer::timeout, [this]{timer_overflow();});
Комментариев нет:
Отправить комментарий