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

    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 Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by
    #26

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

    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

    This don't made sense to me.

    Qt has 2 syntax to create a connection:

    • the "old" one with SLOT() and SIGNAL() macros
    • the "new" one where you can specify the signal and slots functions.

    With the old syntax, you have to specify all parameter types (not names), because corresponding slot or signal are found per introspection (all QMeta stuff). So there is no way, at compilation time, to know if signal or slot exists.

    With the new syntax, you give full function name. This way it is possible, at compilation, to the check if corresponding signal/slots exists.
    With the new syntax you can also connection a signal to a functor/lambda function. Which is not possible with the old 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.

    For all of them, signal and slot do not have to have the same amount of parameters, but signal must at least have same type of parameters as connected slot.

    Suppose you have follow signals:

    • void statusChanged(int newStatus)
    • void newMessage(const QString &message)
    • void done()

    And following slots:

    • void onStatusChanged(int newStatus)
    • void onStatusMessageChanged(int newStatus, const QString &message)
    • void writeToLog(const QString &message)
    • void triggerTimer()

    with old syntax connection would be:

    connect(this, SIGNAL(statusChanged(int)), this, SLOT(onStatusChanged(int)));
    connect(this, SIGNAL(statusChanged(int)), this, SLOT(triggerTimer()));
    

    same with new syntax:

    connect(this, &MyClass::statusChanged, this, &MyClass::onStatusChanged);
    connect(this, &MyClass::statusChanged, this, &MyClass::triggerTimer);
    

    But connecting statusChanged with writeToLog will not be possible or onStatusMessageChanged, because parameter types are not compatible.

    It seems you are always asking same kind of questions about signals/slots.
    There are so many documentation about this

    • Signals & Slots at doc.qt.io
    • What do I do if a slot is not invoked? at KDAB
    • How Qt Signals and Slots Work at woboq

    I know, reading documentation is boring, but always having same issue is even more boring.
    Don't you think so?

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

    1 Reply Last reply
    4
    • A Anonymous_Banned275

      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.

      S Online
      S Online
      SimonSchroeder
      wrote on last edited by
      #27

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

      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 ?

      When you do the connect() you specify the function signature. The signature consists of the types of the parameters, not the object you like to pass. The slot you want to connect to needs to have the same parameter types in the same order. In addition the slot may have fewer parameters by taking away parameters from the end.

      To take your example from above using mySignal, testSlot and testSlot_PASS_DATA I will try to show how to use them.
      You should have 1 signal in this example:

      signals:
        void mySignal(QListWidgetItem*); // you are allowed to give a parameter name, but it is not necessary
      

      You can have two different kinds of slots to be connected to this signal:

      public slots:
        void testSlot();
        void testSlot_PASS_DATA(QListWidgetItem *item);
      

      You can do a connect from mySignal to testSlot as the slot has fewer parameters as the signal. You can connect mySignal to testSlot_PASS_DATA because the two have the same signature (i.e. the same types of their respective parameters).

      How do you use these?

      emit mySignal(item);
      

      When you emit the signal, you specify which object/value should be handed down. Qt will then take this object and hands it to the slots it calls. This is how you acces the item in testSlot_PASS_DATA. These are the basics of Qt. If you've had read thoroughly through the documentation you would know that already.

      A 1 Reply Last reply
      2
      • S SimonSchroeder

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

        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 ?

        When you do the connect() you specify the function signature. The signature consists of the types of the parameters, not the object you like to pass. The slot you want to connect to needs to have the same parameter types in the same order. In addition the slot may have fewer parameters by taking away parameters from the end.

        To take your example from above using mySignal, testSlot and testSlot_PASS_DATA I will try to show how to use them.
        You should have 1 signal in this example:

        signals:
          void mySignal(QListWidgetItem*); // you are allowed to give a parameter name, but it is not necessary
        

        You can have two different kinds of slots to be connected to this signal:

        public slots:
          void testSlot();
          void testSlot_PASS_DATA(QListWidgetItem *item);
        

        You can do a connect from mySignal to testSlot as the slot has fewer parameters as the signal. You can connect mySignal to testSlot_PASS_DATA because the two have the same signature (i.e. the same types of their respective parameters).

        How do you use these?

        emit mySignal(item);
        

        When you emit the signal, you specify which object/value should be handed down. Qt will then take this object and hands it to the slots it calls. This is how you acces the item in testSlot_PASS_DATA. These are the basics of Qt. If you've had read thoroughly through the documentation you would know that already.

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

        @SimonSchroeder We have beek round and round and I stiil do not see as real code for the SLOT.
        The SIGNAL is pretty much covered, but NOT the SLOT.
        I do noty have an acces to my code riight now so this is just a fake

        // SLOT function declaration
        void testSlot_PASS_DATA(QListWidgetItem *item);
        // function definiton
        void testSlot_PASS_DATA(QListWidgetItem *item)
        {
        // display item
        ui-> add ....(item);

        }
        SIGNAL :
        emit value_changed(item) ;
        // the SLOAT is connected to SIGNAL
        connect (this, SIGNAL( value_chnged);
        this , SLOT( testSlot_PASS_DATA);

        I really appreciate all the help, but could do without constant innuendo about RTFM.
        Give it a rest.
        I''ll be back when I get my OS in order.

        S 1 Reply Last reply
        0
        • SGaistS SGaist

          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.

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

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

          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.

          \
          I am really mixed up with this sample.

          I was under the assumption that "emit" generates the SIGNAL , at least in my case.

          I do not have "value " , one of the results of my code is "item" .
          It is a local variable witch gets assigned (new) , hence not really changed.

          Can you confirm just this part for me ?

          KroMignonK 1 Reply Last reply
          0
          • A Anonymous_Banned275

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

            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.

            \
            I am really mixed up with this sample.

            I was under the assumption that "emit" generates the SIGNAL , at least in my case.

            I do not have "value " , one of the results of my code is "item" .
            It is a local variable witch gets assigned (new) , hence not really changed.

            Can you confirm just this part for me ?

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

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

            I was under the assumption that "emit" generates the SIGNAL , at least in my case.

            Again, emit does nothing. It is just in code to help you, as developer, to understand what's happening.
            As written in documentation, all signals functions are automatically generated during compilation by MOC (Meta Object Compiler).
            Using a signal is nothing else as calling a function.
            This is all the magic you have to understand/admit about signals/slots.

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

            A 1 Reply Last reply
            4
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #31

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

              Again, emit does nothing.

              I really admire your patience and wonder when you give up. I'll give you one more month :D

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • A Anonymous_Banned275

                @SimonSchroeder We have beek round and round and I stiil do not see as real code for the SLOT.
                The SIGNAL is pretty much covered, but NOT the SLOT.
                I do noty have an acces to my code riight now so this is just a fake

                // SLOT function declaration
                void testSlot_PASS_DATA(QListWidgetItem *item);
                // function definiton
                void testSlot_PASS_DATA(QListWidgetItem *item)
                {
                // display item
                ui-> add ....(item);

                }
                SIGNAL :
                emit value_changed(item) ;
                // the SLOAT is connected to SIGNAL
                connect (this, SIGNAL( value_chnged);
                this , SLOT( testSlot_PASS_DATA);

                I really appreciate all the help, but could do without constant innuendo about RTFM.
                Give it a rest.
                I''ll be back when I get my OS in order.

                S Online
                S Online
                SimonSchroeder
                wrote on last edited by
                #32

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

                // SLOT function declaration
                void testSlot_PASS_DATA(QListWidgetItem *item);
                // function definiton
                void testSlot_PASS_DATA(QListWidgetItem *item)
                {
                // display item
                ui-> add ....(item);
                }
                SIGNAL :
                emit value_changed(item) ;
                // the SLOAT is connected to SIGNAL
                connect (this, SIGNAL( value_chnged);
                this , SLOT( testSlot_PASS_DATA);

                I am not sure if the SIGNAL is covered. As far as I see it, you got the SLOT right. In the quoted code how you access item is correct.

                In your connect syntax you are mixing old and new style. It is either
                connect(this, SIGNAL(value_changed(QListWidgetItem*)), this, SLOT(testSlot_PASS_DATA(QListWidgetItem*)));
                or
                connect(this, &DeviceDiscoveryDialog::value_changed, this, &DeviceDiscoveryDialog::testSlot_PASS_DATA);.
                One takes the function signature and the other doesn't.

                It is important that you connect the signal and slot before you emit the signal for the first time.

                Whenever you change the item, immediately after that you do emit value_changed(item);. Exactly in the place you put this emit will the signal trigger the call to the (already) connected slot(s).

                Maybe we are also not on the same page what testSlot_PASS_DATA should be doing. Somewhere in your code you should have

                QListWidgetItem *item = ...;
                ui->list->addItem(item);
                emit value_changed(item);
                

                This should not be in testSlot_PASS_DATA (as you last wrote) as this would be called because of the connect. If testSlot_PASS_DATA is declared as void testSlot_PASS_DATA(QListWidgetItem *item); you can acces the item you have put into your call emit value_changed(item) as the parameter within testSlot_PASS_DATA.

                1 Reply Last reply
                0
                • KroMignonK KroMignon

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

                  I was under the assumption that "emit" generates the SIGNAL , at least in my case.

                  Again, emit does nothing. It is just in code to help you, as developer, to understand what's happening.
                  As written in documentation, all signals functions are automatically generated during compilation by MOC (Meta Object Compiler).
                  Using a signal is nothing else as calling a function.
                  This is all the magic you have to understand/admit about signals/slots.

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

                  @KroMignon I do not think repeating same over and over will convince me how me creating an instance of item creates a SIGNAL without emit.
                  Perhaps different explanation would help.

                  But I have enough info to get by.

                  Let me finish my code , test it, and THEN maybe further discussion would make things clearer for me.
                  In the mean time - stand by.

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

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

                    @KroMignon I do not think repeating same over and over will convince me how me creating an instance of item creates a SIGNAL without emit.
                    Perhaps different explanation would help.

                    There's no "SIGNAL creation".

                    You create an item -> unrelated to the signal.
                    You add this item to your list -> unrelated to the signal.
                    You call value_changed(item) -> still no signal creation.

                    A signal is a function that you declare in your class.
                    It may or may not have parameter(s) as any other function.

                    emit value_changed(item); is a function call
                    value_changed(item); is exactly the same function call

                    As was already explained several times: emit is a macro that is empty. It's 100% aesthetics. It has even a sibling called Q_EMIT which is as empty as emit.

                    Why is it used ? To make the code clear and easy to reason about.
                    Is its use mandatory ? No but it leads to code that is hard to reason about

                    It basically tells the reader: when this function is called, all the slots that are connected to it will be called with the parameter(s) you passed to it.

                    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
                    3
                    • SGaistS SGaist

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

                      @KroMignon I do not think repeating same over and over will convince me how me creating an instance of item creates a SIGNAL without emit.
                      Perhaps different explanation would help.

                      There's no "SIGNAL creation".

                      You create an item -> unrelated to the signal.
                      You add this item to your list -> unrelated to the signal.
                      You call value_changed(item) -> still no signal creation.

                      A signal is a function that you declare in your class.
                      It may or may not have parameter(s) as any other function.

                      emit value_changed(item); is a function call
                      value_changed(item); is exactly the same function call

                      As was already explained several times: emit is a macro that is empty. It's 100% aesthetics. It has even a sibling called Q_EMIT which is as empty as emit.

                      Why is it used ? To make the code clear and easy to reason about.
                      Is its use mandatory ? No but it leads to code that is hard to reason about

                      It basically tells the reader: when this function is called, all the slots that are connected to it will be called with the parameter(s) you passed to it.

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

                      Here is my current working setup , it does "pass" the "item" to be displayed. Better description would be - to be re displayed - for test purposes .
                      At this point I am not sure I would gain more knowledge by elaborating what is or is not a SIGNAL.
                      Nor if new connect style should be of considerable benefit.
                      It has been very confusing discussion which actually started from CODING the "connect " function (?) parameter WITHOUT specifying the desired parameter to pass from SIGNAL to SLOT.

                      I will leave this thread and mark it solved - but also leave its SOLVED mark value to the discretion of the reader.

                          //display local 
                          ui->list->addItem(item);   // event )?)
                      
                         emit pass_item();
                         emit mySignal(item);
                      

                      // signals:
                      // void mySignal(QListWidgetItem * item);
                      // SLOT
                      // int DeviceDiscoveryDialog::testSlot_PASS_DATA (QListWidgetItem * item)
                      // connect local no pass
                      connect(this,
                      &DeviceDiscoveryDialog::mySignal,
                      this ,
                      &DeviceDiscoveryDialog::testSlot);
                      // connect local pass
                      connect(this,
                      &DeviceDiscoveryDialog::mySignal,
                      this ,
                      &DeviceDiscoveryDialog::testSlot_PASS_DATA);

                      KroMignonK 1 Reply Last reply
                      0
                      • A Anonymous_Banned275

                        Here is my current working setup , it does "pass" the "item" to be displayed. Better description would be - to be re displayed - for test purposes .
                        At this point I am not sure I would gain more knowledge by elaborating what is or is not a SIGNAL.
                        Nor if new connect style should be of considerable benefit.
                        It has been very confusing discussion which actually started from CODING the "connect " function (?) parameter WITHOUT specifying the desired parameter to pass from SIGNAL to SLOT.

                        I will leave this thread and mark it solved - but also leave its SOLVED mark value to the discretion of the reader.

                            //display local 
                            ui->list->addItem(item);   // event )?)
                        
                           emit pass_item();
                           emit mySignal(item);
                        

                        // signals:
                        // void mySignal(QListWidgetItem * item);
                        // SLOT
                        // int DeviceDiscoveryDialog::testSlot_PASS_DATA (QListWidgetItem * item)
                        // connect local no pass
                        connect(this,
                        &DeviceDiscoveryDialog::mySignal,
                        this ,
                        &DeviceDiscoveryDialog::testSlot);
                        // connect local pass
                        connect(this,
                        &DeviceDiscoveryDialog::mySignal,
                        this ,
                        &DeviceDiscoveryDialog::testSlot_PASS_DATA);

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

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

                        Here is my current working setup , it does "pass" the "item" to be displayed. Better description would be - to be re displayed - for test purposes .
                        At this point I am not sure I would gain more knowledge by elaborating what is or is not a SIGNAL.

                        This is very sad, because signals/slots is one of the main feature of Qt. For my point of view, it is mandatory to understand how it works.
                        With this mechanism, it is easy to create modular and easy to maintain applications.
                        It also helps to handle multi-threading.

                        Nor if new connect style should be of considerable benefit.
                        It has been very confusing discussion which actually started from CODING the "connect " function (?) parameter WITHOUT specifying the desired parameter to pass from SIGNAL to SLOT.

                        Yes, you are right, but how will you know how coding if you don't want to understand what you are using?

                        The used connect style has also his importance:

                        • with old style, you can connect instance without knowing class
                        • with new style, you can ensure that connect will always work because signal/slots signature are checked during compilation. You can also connect a signal with a lambda function, this way you are not have to create a slot each time.

                        For example, suppose you want to have debug message each time a signal is emitted:

                        connect(this,
                        &DeviceDiscoveryDialog::mySignal,
                        []() { qDebug() << "mySignal emitted"; }
                        

                        I will leave this thread and mark it solved - but also leave its SOLVED mark value to the discretion of the reader.

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

                        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