Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal / Slot feature implemented in 2 classes
Forum Updated to NodeBB v4.3 + New Features

Signal / Slot feature implemented in 2 classes

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris H
      wrote on last edited by
      #2

      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()));@

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        [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

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Seba84
              wrote on last edited by
              #6

              [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!

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris H
                wrote on last edited by
                #7

                [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)));@

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved