Results 1 to 6 of 6

Thread: Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

  1. #1
    Join Date
    Jan 2019
    Posts
    11
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

    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

    Qt Code:
    1. #include "minterface.h"
    2.  
    3. mInterface::mInterface(QObject *parent) : QObject(parent)
    4. {
    5. readSocket = new QUdpSocket(this);
    6. readSocket->bind(QHostAddress::LocalHost, 50885);
    7. connect(readSocket, SIGNAL(readyRead()), this, SLOT(receiveData()));
    8. }
    9.  
    10. void mInterface::receiveData()
    11. {
    12. QByteArray rDatagram;
    13.  
    14. do
    15. {
    16. rDatagram.resize(int(readSocket->pendingDatagramSize()));
    17. readSocket->readDatagram(rDatagram.data(), rDatagram.size());
    18. } while(readSocket->hasPendingDatagrams());
    19.  
    20. QDataStream stream(&rDatagram, QIODevice::ReadOnly);
    21. stream.setVersion(QDataStream::Qt_5_9);
    22. memccpy(&arrChannels, (const void *)rDatagram.data(), sizeof(arrChannels), sizeof(arrChannels));
    23.  
    24. //int nombre = qrand()%(101);
    25. qDebug()<<"mInterface : "<<arrChannels[0].AC_Volt;
    26. emit dataAllChannels(arrChannels[0].AC_Volt);
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    form1.cpp
    Qt Code:
    1. #include "readform.h"
    2. #include "ui_readform.h"
    3.  
    4. ReadForm::ReadForm(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::ReadForm)
    7. {
    8. ui->setupUi(this);
    9. connect(&interface, SIGNAL(dataAllChannels(float)), this, SLOT(setReadData(float)));
    10. qDebug()<<"readform constructor";
    11. }
    12.  
    13. ReadForm::~ReadForm()
    14. {
    15. delete ui;
    16. }
    17.  
    18.  
    19. void ReadForm::setReadData(float d)
    20. {
    21.  
    22. qDebug()<<"readform : "<<d;
    23. ui->lcdNumber->display(d);
    24. }
    25.  
    26. void ReadForm::on_pushButton_clicked()
    27. {
    28. mform2.show();
    29. }
    To copy to clipboard, switch view to plain text mode 

    form2.cpp
    Qt Code:
    1. #include "form2.h"
    2. #include "ui_form2.h"
    3.  
    4. Form2::Form2(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Form2)
    7. {
    8. ui->setupUi(this);
    9. connect(&form2, SIGNAL(dataAllChannels(float)), this, SLOT(meinSlot(float)));
    10. qDebug()<<"readform2 constructor";
    11. }
    12.  
    13. Form2::~Form2()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void Form2::meinSlot(float f2)
    19. {
    20. ui->lcdNumber->display(f2);
    21. qDebug()<<"readform2 : "<< f2;
    22. }
    To copy to clipboard, switch view to plain text mode 

    //mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. connect(&m, SIGNAL(dataAllChannels(float)), this, SLOT(readAllValue(float)));
    10. qDebug()<<"Mainwindow constructor";
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::readAllValue(float c)
    19. {
    20. qDebug()<<"Read Mainwindow : "<<c;
    21. ui->lcdNumber->display(c);
    22. }
    23.  
    24. void MainWindow::on_pushButton_clicked()
    25. {
    26. readform.show();
    27. }
    To copy to clipboard, switch view to plain text mode 

    //main.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

    At 99, (9)% error is on your side.
    1. You did not say which form receives the signal and which is not.
    2. You did not show h files.

  3. The following user says thank you to Lesiok for this useful post:

    malone (24th January 2019)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

    I agree with Lesiok. You need to show the -complete code- not just the part you think is important.

    My guess is that in every one of the signal / slot connections you are making, you are using a different instance of your "mInterface" class. I say this because in each of the connect() statements, it looks like you are using the address of a member variable (&interface) instead of a pointer. That tells me you are probably creating that variable on the stack during the construction of your forms and are not passing in the address of the single mInterface instance that is the one receiving the data. Only the first instance created does anything, because after it has been bound to the read socket, the other calls to bind() fail (but you don't bother to check to see if the bind() was successful...).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #4
    Join Date
    Jan 2019
    Posts
    11
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

    Thank you for your feedback,

    Lesiok
    At 99, (9)% error is on your side.
    1. You did not say which form receives the signal and which is not.
    mainwindow, readform and form2 have to receive the signal. i can see that.


    mInterface.h

    Qt Code:
    1. #ifndef MINTERFACE_H
    2. #define MINTERFACE_H
    3.  
    4. #include <QUdpSocket>
    5. #include <QDataStream>
    6.  
    7.  
    8. class mInterface : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit mInterface(QObject *parent = nullptr);
    13.  
    14.  
    15.  
    16. signals:
    17. void dataAllChannels(double);
    18.  
    19. public slots:
    20. void receiveData();
    21.  
    22. private:
    23. QUdpSocket *readSocket;
    24. };
    25.  
    26. #endif // MINTERFACE_H
    To copy to clipboard, switch view to plain text mode 

    readform.h

    Qt Code:
    1. #ifndef READFORM_H
    2. #define READFORM_H
    3.  
    4. #include <QWidget>
    5. #include <minterface.h>
    6. #include <QDebug>
    7.  
    8. namespace Ui {
    9. class ReadForm;
    10. }
    11.  
    12. class ReadForm : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit ReadForm(QWidget *parent = nullptr);
    18. ~ReadForm();
    19.  
    20. private slots:
    21. void setReadData(float);
    22.  
    23. private:
    24. Ui::ReadForm *ui;
    25. mInterface interface;
    26. Form mform2;
    27. };
    28.  
    29. #endif // READFORM_H
    To copy to clipboard, switch view to plain text mode 

    form2.h

    Qt Code:
    1. #ifndef FORM2_H
    2. #define FORM2_H
    3.  
    4. #include <QWidget>
    5. #include <minterface.h>
    6. #include <QDebug>
    7.  
    8. namespace Ui {
    9. class Form2;
    10. }
    11.  
    12. class Form2 : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Form2(QWidget *parent = nullptr);
    18. ~Form2();
    19.  
    20. private slots:
    21. void meinSlot(float);
    22.  
    23. private:
    24. Ui::Form2 *ui;
    25. mInterface form2;
    26. };
    27.  
    28. #endif // FORM2_H
    To copy to clipboard, switch view to plain text mode 







    mainwindow.h


    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QDebug>
    6.  
    7. #include <minterface.h>
    8. #include <readform.h>
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = nullptr);
    20. ~MainWindow();
    21. public slots:
    22. void readAllValue(floatc);
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26. void on_pushStop_clicked();
    27.  
    28. private:
    29. Ui::MainWindow *ui;
    30. mInterface m;
    31. ReadForm readform;
    32.  
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

    As I guessed:

    class ReadForm has a member variable of type mInterface.
    class Form2 has a member variable of type mInterface.
    class MainWindow has a member variable of type mInterface.

    When constructed, these become three completely independent instances of the mInterface class. Only one of them (whichever is constructed first - probably the one in the MainWindow instance constructed in main.cpp) will be successfully be bound to the socket and receive packets. For the other two, the call to bind() will fail because another instance got there first to bind the socket.

    You need to rethink your design and consider how a single instance of a class can be shared among multiple other class instances. It isn't hard - I gave you a hint in my previous answer.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. The following user says thank you to d_stranz for this useful post:

    malone (24th January 2019)

  8. #6
    Join Date
    Jan 2019
    Posts
    11
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Signals- Slots; emit one signal from one Form to multiple forms + mainwindow

    Thank you for your contribution,

    Quote Originally Posted by d_stranz View Post
    I agree with Lesiok. You need to show the -complete code- not just the part you think is important.

    My guess is that in every one of the signal / slot connections you are making, you are using a different instance of your "mInterface" class. I say this because in each of the connect() statements, it looks like you are using the address of a member variable (&interface) instead of a pointer. That tells me you are probably creating that variable on the stack during the construction of your forms and are not passing in the address of the single mInterface instance that is the one receiving the data. Only the first instance created does anything, because after it has been bound to the read socket, the other calls to bind() fail (but you don't bother to check to see if the bind() was successful...).
    I think you are right. When I started the program found that the constructor of the class "mInterface" was called 3 times so 3 different instance of mInterface. once by calling the class Mainwindow + once by calling the class Readform and once by calling the class Form2. When i try with pointer by it is the same. I declared a pointer of mInterface in mainwindow, form2 and readForm for signals/slots-connection but nothing else happening only the same result.
    How can i do that(to declare only one instance of mInterface )? can you give me a example?
    thanks.


    Added after 1 8 minutes:


    Quote Originally Posted by d_stranz View Post
    As I guessed:

    class ReadForm has a member variable of type mInterface.
    class Form2 has a member variable of type mInterface.
    class MainWindow has a member variable of type mInterface.

    When constructed, these become three completely independent instances of the mInterface class. Only one of them (whichever is constructed first - probably the one in the MainWindow instance constructed in main.cpp) will be successfully be bound to the socket and receive packets. For the other two, the call to bind() will fail because another instance got there first to bind the socket.

    You need to rethink your design and consider how a single instance of a class can be shared among multiple other class instances. It isn't hard - I gave you a hint in my previous answer.
    it runs very well. Thank you very much for your help.
    Last edited by malone; 24th January 2019 at 22:39.

Similar Threads

  1. Problem with signals slots and emit
    By slqh in forum Newbie
    Replies: 4
    Last Post: 19th September 2011, 10:20
  2. Replies: 1
    Last Post: 22nd May 2011, 00:53
  3. Replies: 3
    Last Post: 7th April 2011, 12:09
  4. Emit one signal from multiple classes
    By Ishmael in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2010, 00:57
  5. Connecting slots/signals in subclassed form
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2006, 17:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.