QT Udp socket
-
QObject::connect: No such signal QUdpSocket::Read
I get an error.
MainWindow.h looks like this...........................................
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QUdpSocket>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void Hello1();signals:
public slots:
void Hello2(); void Read1();
private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;
QUdpSocket *socket;};
#endif // MAINWINDOW_HMainWindow.cpp look like this.............................................
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include<iostream>QTimer *timer = new QTimer();
static QUdpSocket *socket;MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);socket = new QUdpSocket(this); socket->bind(QHostAddress::LocalHost, 1234); connect(socket, SIGNAL(Read1()), this, SLOT(Read1())); timer = new QTimer(this); connect(timer, SIGNAL(timeout()),this,SLOT(Hello2())); timer -> start(1000);
// connect(timer, SIGNAL(timeout()),this,SLOT(Hello1()));
// timer -> start(1000);ui -> pushButton -> setCheckable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::Hello2()
{
qDebug() << "update..";
}void MainWindow::Hello1()
{QByteArray Data; Data.append("Hello from UDP land");
// char Name[1000];
// std::cout << "Your message : ";
// std::cin >> Name;
// qDebug() << "Hello " << Name << "\n";socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
}
void MainWindow::Read1()
{
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize()); //bekleyen data miktarıQHostAddress sender; quint16 senderPort;
if ( socket->readDatagram(buffer.data(), buffer.size(),&sender,&senderPort) != -1)
{qDebug() << "Message from: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message: " << buffer;
} else {
;// handle error
}
}void MainWindow::on_pushButton_clicked()
{ui ->label->setText("1");
}
and main.cpp look like .......................
#include "MainWindow.h"
#include "MyUDP.h"#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);MainWindow client; client.Hello1(); MainWindow w; w.show(); return a.exec();
}
-
@Tibet said in QT Udp socket:
QObject::connect: No such signal QUdpSocket::Read
And that's correct - QUdpSocket has no such signal.
/edit: and please fix your post to use the code tags to make the code more readable!
-
QObject::connect: No such signal QUdpSocket::Read
Not only as @Christian-Ehrlicher says, but in addition tell us clearly which line you get this error on, and what the code of the line is? Because I don't see any call at all to
QUdpSocket::Read
anywhere in the code you show, so I don't even see how you could get that error message; if I didn't know better, I'd say you either have not copied & pasted the error message, or your code is not as shown.... -
14:15:32: Starting /home/tibet/build-qtDeneme12-Desktop-Debug/qtDeneme12 ...
QObject::connect: No such signal QUdpSocket::Read1() in ../qtDeneme12/MainWindow.cpp:19
QObject::connect: (receiver name: 'MainWindow')
QObject::connect: No such signal QUdpSocket::Read1() in ../qtDeneme12/MainWindow.cpp:19
QObject::connect: (receiver name: 'MainWindow') -
@Tibet Again: there is no such signal in QUdpSocket!
-
@Tibet
So, as suspected, what you said was the error message never was the error message.... Please, if you post questions take the time to copy & paste the correct code and the correct error message, else people waste time trying to help you.And again additional to @Christian-Ehrlicher .
I'm sorry, but if you have not already done so you really need to read Signals & Slots and have a proper understanding before you will get anywhere in Qt. Just trying things won't help you.
Also I strongly advise that you change over to the New Signal Slot Syntax before you go further. It will hep you by generating compiler messages when you have done something wrong.
-
@Tibet said in QT Udp socket:
connect (socket, SIGNAL (Read1 ()), this, SLOT (Read1 ())); --- this part doesn't work
And again: we know that it does not work because QUdpSocket has no such signal (said already two times before but you're ignoring it). The documentation has a working code snippet so why don't you follow them?
-
connect (socket, SIGNAL (Read1 ()), this, SLOT (Read1 ()));
Yes, it does not work. Because
socket
is aQUdpSocket
, and as per that documentation page there is no such signal (or any method) asQUdpSocket::Read1
. There is a signalQUdpSocket::readRead
, and there is a methodQUdpSocket::readDatagram()
which you might want to call in a slot on that signal.... -
@Tibet said in QT Udp socket:
it was fixed.
so if your issue is solved now, please don't forget to mark your post as such!