malone
24th January 2019, 13:11
Hello community,
i have one class called mInterface(mInterface.cpp + mInterface.h) that reads data from udpSocket and then send those data to mupltiple(2) Forms(.ui) and mainwindow. i use Signals and slots communication to emit the data to all another forms.
normally, when i start my project i have to read datagram from udpsocket and then directly emit received data to Forms of my projects. Reading of datagram runs fine but the transfer of data to other forms doesn't works well. Only one Form receives transmitted data and the ather not. all classes Form are written in the same way but i don't understand why only one Form receives transmitted signal(data).
according to the signals/slots communication we can emit one signal to more slots from multiple objects or Form(.ui). if yes, why my project not works? somebody have an idea?
in advance, thank you for your help.
//mInterface.cpp class
#include "minterface.h"
mInterface::mInterface(QObject *parent) : QObject(parent)
{
readSocket = new QUdpSocket(this);
readSocket->bind(QHostAddress::LocalHost, 50885);
connect(readSocket, SIGNAL(readyRead()), this, SLOT(receiveData()));
}
void mInterface::receiveData()
{
QByteArray rDatagram;
do
{
rDatagram.resize(int(readSocket->pendingDatagramSize()));
readSocket->readDatagram(rDatagram.data(), rDatagram.size());
} while(readSocket->hasPendingDatagrams());
QDataStream stream(&rDatagram, QIODevice::ReadOnly);
stream.setVersion(QDataStream::Qt_5_9);
memccpy(&arrChannels, (const void *)rDatagram.data(), sizeof(arrChannels), sizeof(arrChannels));
//int nombre = qrand()%(101);
qDebug()<<"mInterface : "<<arrChannels[0].AC_Volt;
emit dataAllChannels(arrChannels[0].AC_Volt);
}
form1.cpp
#include "readform.h"
#include "ui_readform.h"
ReadForm::ReadForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::ReadForm)
{
ui->setupUi(this);
connect(&interface, SIGNAL(dataAllChannels(float)), this, SLOT(setReadData(float)));
qDebug()<<"readform constructor";
}
ReadForm::~ReadForm()
{
delete ui;
}
void ReadForm::setReadData(float d)
{
qDebug()<<"readform : "<<d;
ui->lcdNumber->display(d);
}
void ReadForm::on_pushButton_clicked()
{
mform2.show();
}
form2.cpp
#include "form2.h"
#include "ui_form2.h"
Form2::Form2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form2)
{
ui->setupUi(this);
connect(&form2, SIGNAL(dataAllChannels(float)), this, SLOT(meinSlot(float)));
qDebug()<<"readform2 constructor";
}
Form2::~Form2()
{
delete ui;
}
void Form2::meinSlot(float f2)
{
ui->lcdNumber->display(f2);
qDebug()<<"readform2 : "<< f2;
}
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m, SIGNAL(dataAllChannels(float)), this, SLOT(readAllValue(float)));
qDebug()<<"Mainwindow constructor";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readAllValue(float c)
{
qDebug()<<"Read Mainwindow : "<<c;
ui->lcdNumber->display(c);
}
void MainWindow::on_pushButton_clicked()
{
readform.show();
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
i have one class called mInterface(mInterface.cpp + mInterface.h) that reads data from udpSocket and then send those data to mupltiple(2) Forms(.ui) and mainwindow. i use Signals and slots communication to emit the data to all another forms.
normally, when i start my project i have to read datagram from udpsocket and then directly emit received data to Forms of my projects. Reading of datagram runs fine but the transfer of data to other forms doesn't works well. Only one Form receives transmitted data and the ather not. all classes Form are written in the same way but i don't understand why only one Form receives transmitted signal(data).
according to the signals/slots communication we can emit one signal to more slots from multiple objects or Form(.ui). if yes, why my project not works? somebody have an idea?
in advance, thank you for your help.
//mInterface.cpp class
#include "minterface.h"
mInterface::mInterface(QObject *parent) : QObject(parent)
{
readSocket = new QUdpSocket(this);
readSocket->bind(QHostAddress::LocalHost, 50885);
connect(readSocket, SIGNAL(readyRead()), this, SLOT(receiveData()));
}
void mInterface::receiveData()
{
QByteArray rDatagram;
do
{
rDatagram.resize(int(readSocket->pendingDatagramSize()));
readSocket->readDatagram(rDatagram.data(), rDatagram.size());
} while(readSocket->hasPendingDatagrams());
QDataStream stream(&rDatagram, QIODevice::ReadOnly);
stream.setVersion(QDataStream::Qt_5_9);
memccpy(&arrChannels, (const void *)rDatagram.data(), sizeof(arrChannels), sizeof(arrChannels));
//int nombre = qrand()%(101);
qDebug()<<"mInterface : "<<arrChannels[0].AC_Volt;
emit dataAllChannels(arrChannels[0].AC_Volt);
}
form1.cpp
#include "readform.h"
#include "ui_readform.h"
ReadForm::ReadForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::ReadForm)
{
ui->setupUi(this);
connect(&interface, SIGNAL(dataAllChannels(float)), this, SLOT(setReadData(float)));
qDebug()<<"readform constructor";
}
ReadForm::~ReadForm()
{
delete ui;
}
void ReadForm::setReadData(float d)
{
qDebug()<<"readform : "<<d;
ui->lcdNumber->display(d);
}
void ReadForm::on_pushButton_clicked()
{
mform2.show();
}
form2.cpp
#include "form2.h"
#include "ui_form2.h"
Form2::Form2(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form2)
{
ui->setupUi(this);
connect(&form2, SIGNAL(dataAllChannels(float)), this, SLOT(meinSlot(float)));
qDebug()<<"readform2 constructor";
}
Form2::~Form2()
{
delete ui;
}
void Form2::meinSlot(float f2)
{
ui->lcdNumber->display(f2);
qDebug()<<"readform2 : "<< f2;
}
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m, SIGNAL(dataAllChannels(float)), this, SLOT(readAllValue(float)));
qDebug()<<"Mainwindow constructor";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readAllValue(float c)
{
qDebug()<<"Read Mainwindow : "<<c;
ui->lcdNumber->display(c);
}
void MainWindow::on_pushButton_clicked()
{
readform.show();
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}