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. How to "emit " SIGNAL " after "addItem"?
Forum Updated to NodeBB v4.3 + New Features

How to "emit " SIGNAL " after "addItem"?

Scheduled Pinned Locked Moved Solved General and Desktop
36 Posts 11 Posters 5.8k Views 6 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.
  • A Anonymous_Banned275

    I need to convert
    //display
    ui->list->addItem(item);

    to SIGNAL to pass the item value to another object.

    how?

    (Can I get a sample C++ code ?)

    Qt is not "real time ", so what do you mean with this?
    The SLOT I am processing and displaying the result as "item"
    is triggered by real time as a result of search for nearby bluetooth devices. Roughly 2 to 3 seconds.

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by
    #6

    @AnneRanch

    Emit your own signal, which passes your item to your slot. Do whatever you want and then you can still do ui->list->addItem(item); in your slot to add your item to your list.

    Similar as @KroMignon wrote above:

    signals:    
             void mySignal(QListWidgetItem * item);
    

    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    1 Reply Last reply
    0
    • KroMignonK KroMignon

      @AnneRanch I cannot really understand what problem you try to solve.

      Your explanation are very confusing for me.
      You can add as many signals (or slots) you need to your class. And when signal is emitted is up to you.

      Just for clarification:

      • a SIGNAL is only a function, which is created at compilation during MOC process (you can find it implementation in <class_name>_moc.cpp).
      • emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.
      • Qt is not "real time ", so what do you mean with this?

      The "display " is part of SIGNAL / SLOT and its real time varies .

      A Offline
      A Offline
      Anonymous_Banned275
      wrote on last edited by
      #7

      @KroMignon said in How to "emit " SIGNAL " after "addItem"?:

      @AnneRanch I cannot really understand what problem you try to solve.

      Your explanation are very confusing for me.
      You can add as many signals (or slots) you need to your class. And when signal is emitted is up to you.

      Just for clarification:

      • a SIGNAL is only a function, which is created at compilation during MOC process (you can find it implementation in <class_name>_moc.cpp).
      • emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL funct> - emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.ion.
      • Qt is not "real time ", so what do you mean with this?

      The "display " is part of SIGNAL / SLOT and its real time varies .

      • emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.

      Not according to this

      f87ad132-2dc2-43be-acad-53afe7d93fd3-image.png

      JonBJ 1 Reply Last reply
      0
      • A Anonymous_Banned275

        @KroMignon said in How to "emit " SIGNAL " after "addItem"?:

        @AnneRanch I cannot really understand what problem you try to solve.

        Your explanation are very confusing for me.
        You can add as many signals (or slots) you need to your class. And when signal is emitted is up to you.

        Just for clarification:

        • a SIGNAL is only a function, which is created at compilation during MOC process (you can find it implementation in <class_name>_moc.cpp).
        • emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL funct> - emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.ion.
        • Qt is not "real time ", so what do you mean with this?

        The "display " is part of SIGNAL / SLOT and its real time varies .

        • emit is just a semantical sugar only to help programmer be aware about calling a SIGNAL function.

        Not according to this

        f87ad132-2dc2-43be-acad-53afe7d93fd3-image.png

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #8

        @AnneRanch
        As we have discussed before you see that the definition of emit in Qt header file is:

        #define emit
        

        See https://code.woboq.org/qt5/qtbase/src/corelib/kernel/qobjectdefs.h.html#_M/emit. That's it. So it is "semantical sugar".

        So your clazy message shows it looks for whether you place emit before calling a signal function, and just warns you if you do not. The code still works unaffected without, it's just trying to make your life better, possibly.

        1 Reply Last reply
        4
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #9

          "real time" has a very specific meaning in embedded and controls programming.
          It means a system can satisfy deterministic constraints. This means if the constraint is 100mS then the system will always do certain functions every 100mS. Nit-picky I know, but it can cause confusion in technical discussions.

          By your usage I think you mean "the time it takes" for the operation to complete.

          C++ is a perfectly valid school of magic.

          A 1 Reply Last reply
          2
          • fcarneyF fcarney

            "real time" has a very specific meaning in embedded and controls programming.
            It means a system can satisfy deterministic constraints. This means if the constraint is 100mS then the system will always do certain functions every 100mS. Nit-picky I know, but it can cause confusion in technical discussions.

            By your usage I think you mean "the time it takes" for the operation to complete.

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by Anonymous_Banned275
            #10

            @fcarney Appreciate your clarification.
            I guess typical case "you are dammed if you do or do not ".
            I tried to give my question some background ( some people need ) and see how it got away from the real issue.

            Precise terminology is useful, but the message Is
            "missing ... "and it does NOT imply "warning " to me .....

            JonBJ 1 Reply Last reply
            0
            • A Anonymous_Banned275

              @fcarney Appreciate your clarification.
              I guess typical case "you are dammed if you do or do not ".
              I tried to give my question some background ( some people need ) and see how it got away from the real issue.

              Precise terminology is useful, but the message Is
              "missing ... "and it does NOT imply "warning " to me .....

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #11

              @AnneRanch said in How to "emit " SIGNAL " after "addItem"?:

              "missing ... "and it does NOT imply "warning " to me .....

              "Missing" implies "missing". That has nothing to do with whether something is an error, a warning, a informational message, a hint or a philosophical musing. On the other hand, doesn't the yellow colored triangle and text tell you it's a warning, and red color indicates an error? What is the point of you often stating you do not like the wording of messages? They are what they are.

              If you care, https://www.kdab.com/nailing-13-signal-slot-mistakes-clazy-1-3/ states:

              11. incorrect-emit

              For readability purposes you should always use emit (or Q_EMIT) when calling a signal. Conversely, you should not use those macros when calling something other than a signal.

              Clazy will warn if you forget to use emit (or Q_EMIT) or if you use them when you shouldn’t.

              which I would have thought is the actually important information.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #12

                OK, I am still not getting it.
                My syntax is obviously wrong.

                   emit pass_item();        works 
                   emit mySignal(item); does not work 
                

                // signals:
                // void mySignal(QListWidgetItem * item);

                    connect(this,
                            SIGNAL(mySignal(item)),
                            this ,
                            SLOT(testSlot()));
                

                This works fine

                    connect(this,
                            SIGNAL(pass_item()),
                            this ,
                            SLOT(testSlot_PASS_DATA() ));
                

                And here is my debug / run time error

                TASK : test local SIGNAL / SLOT connect

                File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
                Function : testSlot
                @line : 380
                count : 1
                QObject::connect: No such signal DeviceDiscoveryDialog::mySignal(item) in ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp:148
                QObject::connect: (sender name: 'DeviceDiscovery')
                QObject::connect: (receiver name: 'DeviceDiscovery')

                TASK : test local SIGNAL / SLOT connect

                File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
                Function : testSlot
                @line : 380
                count : 2

                JonBJ KroMignonK 2 Replies Last reply
                0
                • A Anonymous_Banned275

                  OK, I am still not getting it.
                  My syntax is obviously wrong.

                     emit pass_item();        works 
                     emit mySignal(item); does not work 
                  

                  // signals:
                  // void mySignal(QListWidgetItem * item);

                      connect(this,
                              SIGNAL(mySignal(item)),
                              this ,
                              SLOT(testSlot()));
                  

                  This works fine

                      connect(this,
                              SIGNAL(pass_item()),
                              this ,
                              SLOT(testSlot_PASS_DATA() ));
                  

                  And here is my debug / run time error

                  TASK : test local SIGNAL / SLOT connect

                  File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
                  Function : testSlot
                  @line : 380
                  count : 1
                  QObject::connect: No such signal DeviceDiscoveryDialog::mySignal(item) in ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp:148
                  QObject::connect: (sender name: 'DeviceDiscovery')
                  QObject::connect: (receiver name: 'DeviceDiscovery')

                  TASK : test local SIGNAL / SLOT connect

                  File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
                  Function : testSlot
                  @line : 380
                  count : 2

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #13

                  @AnneRanch said in How to "emit " SIGNAL " after "addItem"?:

                  SIGNAL(mySignal(item)),

                  When connecting signals this (old) SIGNAL macro way, you need to specify the type(s) of the parameter(s):

                  SIGNAL(mySignal(QListWidgetItem *))
                  
                  1 Reply Last reply
                  1
                  • A Anonymous_Banned275

                    OK, I am still not getting it.
                    My syntax is obviously wrong.

                       emit pass_item();        works 
                       emit mySignal(item); does not work 
                    

                    // signals:
                    // void mySignal(QListWidgetItem * item);

                        connect(this,
                                SIGNAL(mySignal(item)),
                                this ,
                                SLOT(testSlot()));
                    

                    This works fine

                        connect(this,
                                SIGNAL(pass_item()),
                                this ,
                                SLOT(testSlot_PASS_DATA() ));
                    

                    And here is my debug / run time error

                    TASK : test local SIGNAL / SLOT connect

                    File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
                    Function : testSlot
                    @line : 380
                    count : 1
                    QObject::connect: No such signal DeviceDiscoveryDialog::mySignal(item) in ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp:148
                    QObject::connect: (sender name: 'DeviceDiscovery')
                    QObject::connect: (receiver name: 'DeviceDiscovery')

                    TASK : test local SIGNAL / SLOT connect

                    File : ../../../JUNE 9 WORKCOPY 1/CAT/btscanner/device.cpp
                    Function : testSlot
                    @line : 380
                    count : 2

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #14

                    @AnneRanch You should avoid using old connect syntax and use new one:

                     connect(this,
                                SIGNAL(mySignal(item)),
                                this ,
                                SLOT(testSlot()));
                    

                    becommes:

                     connect(this,
                                &ClassName::mySignal,
                                this ,
                                &ClassName::testSlot);
                    

                    So you will avoid this kind of errors.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    JonBJ 1 Reply Last reply
                    2
                    • KroMignonK KroMignon

                      @AnneRanch You should avoid using old connect syntax and use new one:

                       connect(this,
                                  SIGNAL(mySignal(item)),
                                  this ,
                                  SLOT(testSlot()));
                      

                      becommes:

                       connect(this,
                                  &ClassName::mySignal,
                                  this ,
                                  &ClassName::testSlot);
                      

                      So you will avoid this kind of errors.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #15

                      @KroMignon
                      We have suggested this many, many times.....! :)

                      A 1 Reply Last reply
                      2
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #16

                        It's the exact same problem you reported here. You can apply the same solution

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        A 1 Reply Last reply
                        2
                        • VRoninV VRonin

                          It's the exact same problem you reported here. You can apply the same solution

                          A Offline
                          A Offline
                          Anonymous_Banned275
                          wrote on last edited by
                          #17

                          @VRonin said in How to "emit " SIGNAL " after "addItem"?:

                          It's the exact same problem you reported here. You can apply the same solution

                          Yes, same issue.
                          I think if somebody said "pointer to function" it may have help.

                          Now as far as "passing the data" the syntax looks as standard passing parameter to a function passed as a parameter.
                          Sounds silly but that is what it is , nothing Qt special when implementing "connect".

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @KroMignon
                            We have suggested this many, many times.....! :)

                            A Offline
                            A Offline
                            Anonymous_Banned275
                            wrote on last edited by
                            #18

                            @JonB said in How to "emit " SIGNAL " after "addItem"?:

                            @KroMignon
                            We have suggested this many, many times.....! :)
                            Thanks for keeping track
                            and you may expect more of the same in the future - so ignore it.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              Anonymous_Banned275
                              wrote on last edited by
                              #19

                              OK, few more questions , if I am allowed to push my luck here.

                              The SIGNAL function has parameter "item" , then
                              the SLOT function should have same type of parameter?

                              BUT the new connect syntax does not require the parameters, hence in "connect" neither SIGNAL or SLOT functions are with parameters, and it passes MOC fine.

                              How do I process the undeclared parameter in my SLOT function?

                              BUT If I declare the SLOT with parameter my code breaks.

                              Again if the above questions are bothersome / repetitious just ignore my post.

                                emit pass_item();
                                 emit mySignal(item);
                              

                              // signals:
                              // void mySignal(QListWidgetItem * item);
                              // emit testSlot_PASS_DATA ();

                                  connect(this,
                                              &DeviceDiscoveryDialog::mySignal,
                                              this ,
                                              &DeviceDiscoveryDialog::testSlot);
                              
                                  connect(this,
                                          &DeviceDiscoveryDialog::mySignal,
                                          this ,
                                          &DeviceDiscoveryDialog::testSlot_PASS_DATA);
                              
                              artwawA 1 Reply Last reply
                              0
                              • A Anonymous_Banned275

                                OK, few more questions , if I am allowed to push my luck here.

                                The SIGNAL function has parameter "item" , then
                                the SLOT function should have same type of parameter?

                                BUT the new connect syntax does not require the parameters, hence in "connect" neither SIGNAL or SLOT functions are with parameters, and it passes MOC fine.

                                How do I process the undeclared parameter in my SLOT function?

                                BUT If I declare the SLOT with parameter my code breaks.

                                Again if the above questions are bothersome / repetitious just ignore my post.

                                  emit pass_item();
                                   emit mySignal(item);
                                

                                // signals:
                                // void mySignal(QListWidgetItem * item);
                                // emit testSlot_PASS_DATA ();

                                    connect(this,
                                                &DeviceDiscoveryDialog::mySignal,
                                                this ,
                                                &DeviceDiscoveryDialog::testSlot);
                                
                                    connect(this,
                                            &DeviceDiscoveryDialog::mySignal,
                                            this ,
                                            &DeviceDiscoveryDialog::testSlot_PASS_DATA);
                                
                                artwawA Offline
                                artwawA Offline
                                artwaw
                                wrote on last edited by
                                #20

                                @AnneRanch if your slot doesn't require parameter it's ok. If you need to catch the parameter but can't provide/modify corresponding slot for use with parameter, you can still catch it using lambda (using new connect() syntax. And then do something with that parameter, call helper method or whatever and slot at the beginning/end, however you like.

                                For more information please re-read.

                                Kind Regards,
                                Artur

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  Anonymous_Banned275
                                  wrote on last edited by
                                  #21

                                  Now that is clear as mud.
                                  I have control over the SLOT - that is not the issue.
                                  My question is
                                  if the SIGNAL contains , carries or whatever term is used , the "item" (as a parameter) without specifying "item' in ":connect" as parameter
                                  AND
                                  SLOT connects to such SIGNAL , again without specifying it in "connect"
                                  syntax
                                  HOW DO I ACCESS THE "item" passed (?) by SIGNAL in my SLOT function ?
                                  So far all I have done is to activate the SLOT function - now I need to learn how to use the parameter carried in by SIGNAL.

                                  KroMignonK S 2 Replies Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #22

                                    Hi,

                                    Did you go through the Signals and Slots chapter in Qt's documentation ?
                                    It explains everything and provides simple examples to show how things work.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    A 1 Reply Last reply
                                    2
                                    • SGaistS SGaist

                                      Hi,

                                      Did you go through the Signals and Slots chapter in Qt's documentation ?
                                      It explains everything and provides simple examples to show how things work.

                                      A Offline
                                      A Offline
                                      Anonymous_Banned275
                                      wrote on last edited by Anonymous_Banned275
                                      #23

                                      @SGaist Just scanned thru thru this .

                                      https://wiki.qt.io/New_Signal_Slot_Syntax

                                      Did not find a single example how SIGNAL with parameter is used in SLOT.
                                      It does not explain how such parameter gets thru to the SLOT also.
                                      There is no SLOT function example either.
                                      I'll keep looking , there got to be a pony somewhere...

                                      C 1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #24

                                        The Counter class from the documentation:

                                        #include <QObject>
                                        
                                        class Counter : public QObject
                                        {
                                            Q_OBJECT
                                        
                                        public:
                                            Counter() { m_value = 0; }
                                        
                                            int value() const { return m_value; }
                                        
                                        public slots:
                                            void setValue(int value);
                                        
                                        signals:
                                            void valueChanged(int newValue);
                                        
                                        private:
                                            int m_value;
                                        };
                                        

                                        The setValue slot:

                                        void Counter::setValue(int value)
                                        {
                                            if (value != m_value) {
                                                m_value = value;
                                                emit valueChanged(value);
                                            }
                                        }
                                        

                                        The connection:

                                         Counter a, b;
                                            QObject::connect(&a, &Counter::valueChanged,
                                                             &b, &Counter::setValue);
                                        

                                        The usage:

                                            a.setValue(12);     // a.value() == 12, b.value() == 12
                                            b.setValue(48);     // a.value() == 12, b.value() == 4
                                        

                                        When you call setValue from object a and that this value does not match the current counter, the valueChanged signal will be emitted with the new value as parameter.
                                        Then, the setValue of b will be called with as parameter the new value.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        A 1 Reply Last reply
                                        4
                                        • A Anonymous_Banned275

                                          @SGaist Just scanned thru thru this .

                                          https://wiki.qt.io/New_Signal_Slot_Syntax

                                          Did not find a single example how SIGNAL with parameter is used in SLOT.
                                          It does not explain how such parameter gets thru to the SLOT also.
                                          There is no SLOT function example either.
                                          I'll keep looking , there got to be a pony somewhere...

                                          C Offline
                                          C Offline
                                          ChrisW67
                                          wrote on last edited by
                                          #25

                                          @AnneRanch said in How to "emit " SIGNAL " after "addItem"?:

                                          It does not explain how such parameter gets thru to the SLOT also.

                                          When you are using the new style signal-slot connections then the C++ compiler checks that the sender and receiver have matching signatures (or close enough) at compile time and fails to compile if they do not.
                                          https://wiki.qt.io/New_Signal_Slot_Syntax#Type_mismatch

                                          There's not much to explain here to the user of the signal-slot mechanism. The value passed with the signal function call, e.g. fooSignal("barbaz"), is delivered to any slot function connected to the signal essentially as if you had called fooSlot("barbaz"). (The slot may not be called instantly depending on circumstances.) The precise internals of this mechanism are much more involved, but only of relevance if you intend to develop the Qt library yourself.

                                          1 Reply Last reply
                                          1

                                          • Login

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