I’m writing a program that send an UDP frame every 10 mS. Here’s how my program is supposed to work :
I’ve got a client class :
//Constructor
clientSupervision::clientSupervision()
{
}
void clientSupervision::sendDataUDP(){
//Create a frame and send it
...
}
void clientSupervision::sendDataUDPTimer(int timer){
QTimer *tempsEnvoieTrameSupervision = new QTimer();//Create a timer
tempsEnvoieTrameSupervision->setInterval(timer);//Set the interval
//Mise en place des connections
QObject::connect (tempsEnvoieTrameSupervision,SIGNAL (timeout()),this, SLOT (envoiTrameSupervision())); //Connect the timer to the function
tempsEnvoieTrameSupervision->start();// Start the timer
}
//Call sendDataUDP
void clientSupervision::envoiTrameSupervision(){
std::cout << "Envoi de la trame de supervision";
sendDataUDP();
}
My header file of clienSupervision.h :
#ifndef CLIENTSUPERVISION_H
#define CLIENTSUPERVISION_H
#include <winsock2.h> // pour les fonctions socket
#include <cstdio> // Pour les Sprintf
#include "StructureSupervision.h"
#include "utilitaireudp.h"
#include <QTimer>
#include <QObject>
#include <iostream>
class clientSupervision
{
Q_OBJECT
public:
clientSupervision();
void sendDataUDP();
void sendDataUDPTimer(int timer);
public slots:
void envoiTrameSupervision();
};
#endif // CLIENTSUPERVISION_H
Then I use this in my main :
int main(int argc, char *argv[])
{
clientSupervision c;
c.sendDataUDPTimer(10);
QCoreApplication a(argc, argv);
return a.exec();
}
I’ve got the error :
no matching function for call to ‘QObject::connect(QTimer*&, const char*, clientSupervision* const, const char*)
I don’t understand why the connect function can’t find a matching function.
What should I change?
Hi,
I made a very simple test project, just a QMainWindow. I instantiated a QSpinBox in the constructor and tried to connect it to a public slot of MainWindow. Yet I get the error «no matching member function for call to ‘connect’. This using both the old and new style connect syntax. My header includes Q_OBJECT. Both QMainWindow and QSpinBox derive from QObject.
I must be missing/forgetting something very simple here. Why on earth doesn’t this work?
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void doTest(int val);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSpinBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSpinBox* oSpinBox = new QSpinBox(this);
connect(oSpinBox, &QSpinBox::valueChanged, this, &MainWindow::doTest);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::doTest(int val)
{
// do something
}
I’m trying to port one of my programs from Python to C++, but I can’t figure out some simple Qt stuff.
In my mainwindow header, I’ve got:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void recordButton_slot();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
and in my mainwindow.cpp, I’ve got:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Connect signals/slots
connect(ui->recordButton, SIGNAL( clicked()) ), this, SLOT(record_button_slot());
}
MainWindow::~MainWindow()
{
delete ui;
}
When I try to compile, I get
no matching member function for call to 'connect'
If I comment out the line
connect(ui->recordButton, SIGNAL( clicked()) ), this, SLOT(record_button_slot());
the code compiles. I’ve tried using QObject::connect, but that doesn’t seem to affect anything.
Do you see where I went wrong? I’ve found quite a few similar questions but I don’t quite understand how to fix this.
MyThread наследует QThread, w.getScene() возвращает указатель на объект класса Scene, унаследованный от QGraphicsScene
Создаю в main’e 2 коннекта:
#include <QtGui/QApplication>
#include «mainwindow.h»
#include <time.h>
#include «MyThread.h»
#include «Ship.h»
#include «Port.h»
int main(int argc, char *argv[])
{
//Generating random number
const time_t timer = time(NULL);
tm *timerstruct = localtime(&timer);
uint sec = timerstruct->tm_sec;
qsrand(sec);
int random = qrand()%3;
QApplication a(argc, argv);
MainWindow w;
w.setFixedSize(550,600);
w.show();
Port* port = new Port;
MyThread thread(random,port);
QObject::connect(&thread,SIGNAL(shipMoves(Ship*,qreal)),w.getScene(),SLOT(moveShip(Ship*,qreal)));
QObject::connect(&thread,SIGNAL(newShip(Ship*)),w.getScene(),SLOT(addNewShip(Ship*)));
thread.start();
return a.exec();
}
Получаю для каждого из коннектов
error: no matching function for call to ‘QObject::connect(MyThread*, const char*, Scene*, const char*)’
note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
в чём дело?