Signal / Slot feature implemented in 2 classes



  • I want to know how is possible to connect two widgets from different classes. Well basically i declare a slot in first class (method will change label text) , in second class a declare a slot together with a signal , when implementing slot from second class , it will emit a signal (button click). In my app i must connect these widgets and when a signal is emitted , label's text must change . I saw some examples but there are just implementation of signal/slot in one class, and signals are emitted just one time or twice. I need to do it every time when a click button occurs.
    Any ideas ?



  • As long as you have pointers to your two widgets in some containing class (e.g. a dialog or window class) it's no problem to do things like @
    connect (myButton, SIGNAL(clicked()), myLabel, SLOT(changeText()));@



  • [quote author="Chris H" date="1324315458"]As long as you have pointers to your two widgets in some containing class (e.g. a dialog or window class) it's no problem to do things like @
    connect (myButton, SIGNAL(clicked()), myLabel, SLOT(changeText()));@[/quote]

    i must implement my own signal , so your example will not be good for me



  • You either provide a getter for the widget in one of the classes or you friend one of the classes.
    @
    class A
    {
    public:
    QWidget* widgetA() { return _widgetA; }
    private:
    QWidget* _widgetA;
    };

    class B
    {
    friend class C;
    private:
    QWidget* _widgetB;
    };

    class C
    {
    public:
    C()
    {
    connect(_widgetC, SIGNAL(...), a->widgetA(), SLOT(...));
    connect(_widgetC, SIGNAL(...), b->_widgetB, SLOT(...));
    }
    private:
    QWidget* _widgetC;
    };
    @
    Brain to terminal. Not tested. Exemplary.



  • If two objects need to be connected, there there is always a place where you know about both. That is the place to connect the signals.

    I would recommend you stay away from using <code>friend</code> for these purposes. Instead, just give class B the needed signal directly, and connect the signal from C to the corresponding signal in B. That way, you keep the implementation detail that class B uses a class C private as it should be.



  • [quote author="veverita" date="1324315196"]I saw some examples but there are just implementation of signal/slot in one class, and signals are emitted just one time or twice. I need to do it every time when a click button occurs.
    Any ideas ?[/quote]

    Veverita,
    If the signal is emmitted once or twice or a millon times it doesn't make difference (maybe only on performance aspects).

    [quote author="veverita" date="1324315988"][quote author="Chris H" date="1324315458"]As long as you have pointers to your two widgets in some containing class (e.g. a dialog or window class) it's no problem to do things like @connect (myButton, SIGNAL(clicked()), myLabel, SLOT(changeText()));@ [/quote] i must implement my own signal , so your example will not be good for me[/quote]

    Chris's answer points out the difficulty of the problem you are searching to solve. You need the pointers to the two widgets, if you have them you can connect all the types of signals & slots you want. And the example of Lukas completes what Chris said. You just have to define your own signal, like any other function inside your widget:

    @public signal:
    void mySignal(...); @

    And then connect it:
    @connect (myButton, SIGNAL(mySignal(...)), myLabel, SLOT(changeText(...)));@

    Good luck!



  • [quote author="veverita" date="1324315988"]
    i must implement my own signal , so your example will not be good for me[/quote]
    Why not? There is nothing in there that requires the signal be <code>clicked()</code>, I was just using it as an example. It's easy to define your own signal, e.g. @
    class MyClass : public QObject
    {
    ...
    public signal:
    void MyClassDidStuff (QString data);
    }
    @ and @void MyClass::EmitSignal ()
    {
    emit MyClassDidStuff ("Hello, World!");
    } @ Then, @ connect (instanceOfMyClass, SIGNAL (MyClassDidStuff(QString)), myOtherWidget, SLOT (HandleStuff(QString)));@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.