Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Communication between widgets



  • Hi all,

    I have to pass data from mainwidget to second widget.
    The serial port is processed data contionusly in the main widget.
    Data needs to be sent to a secondary screen.

    Can you help to do that?



  • When you send data from one widget (the processing one) to another widget (the one that puts things to the other screen), the default way in Qt is to send a signal from the first to the slot in the second widget...



  • Can you show an example?


  • Lifetime Qt Champion

    Hi,

    Qt's documentation is filed with these kind of examples and demos. You should be able to get them all together and work with the Qt. .



  • Hi,

    So you want to send data from mainwidget class to second widget. class
    Here is the sample code.

    class MainwidgetClass : public QObject{
    Q_OBJECT;
    ...
    public signals:
    void yourSignal();
    };

    ...

    class SecondWidgetClass: public QObject{
    Q_OBJECT;
    ...
    public slots:
    void slotFunc();
    }

    MainwidgetClass *youObject = new MainwidgetClass ;
    connect(MainwidgetClass , SIGNAL(yourSignal()), SecondWidgetClass, SLOT(someSlot()));

    so if you want to send the data from the first MainwidgetClass class then you have use emit call emit yourSignal();

    And establish the connect between the two classes. U will recieve data in slotFunc() of
    SecondWidgetClass Class.


  • Lifetime Qt Champion

    @Pradeep-Kumar
    That's not valid code.
    It should be:

    MainwidgetClass *youObject = new MainwidgetClass ;
    SecondWidgetClass *secondWidget = new SecondWidgetClass();
    connect(youObject , SIGNAL(yourSignal()), secondWidget, SLOT(slotFunc()));
    

    You need pointer to class instances, you cannot pass a class to connect.



  • @jsulm
    Sorry for that how could i miss that.

    Mistake corrected.

    class MainwidgetClass : public QObject{
    Q_OBJECT;
    ...
    public signals:
    void yourSignal();
    };

    ...

    class SecondWidgetClass: public QObject{
    Q_OBJECT;
    ...
    public slots:
    void slotFunc();
    }

    MainwidgetClass *youObject = new MainwidgetClass ;
    SecondWidgetClass *secondWidget = new SecondWidgetClass;

    connect(youObject , SIGNAL(yourSignal()), secondWidget, SLOT(slotFunc()));

    Thanks for correcting me @jsulm


Log in to reply