Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. What if you emit a FUNCTION (not signals) ....
Forum Updated to NodeBB v4.3 + New Features

What if you emit a FUNCTION (not signals) ....

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 4 Posters 8.0k 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.
  • C Offline
    C Offline
    changsheng230
    wrote on last edited by
    #1

    Seems it will NOT cause any problem though it is bad writing.

    For each signals which defined at class body, moc will generate the following:

    void myClass::signalA()
    {
    QMetaObject::activate(this, &staticMetaObject, 0, 0);
    }

    But for function, moc will not do the same thing above. So when you emit a function by careless. The keyword “emit” just ignored.

    What do you think?

    Chang Sheng
    常升

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

      First of all, your message is a bit hard to read for me, so perhaps I misunderstand what you mean. My question is: why? Why would you want to emit a function? What would that mean?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Emit is not really needed, it defined as nothing

        @
        #define emit
        @
        So emit is not a keyword, it's a define :-)

        You can leave emit out if you want, it's just for better code readability. So if someone sees this:
        @
        void MyClass::foo()
        {
        ... // doing some calculation
        ChangedValue(abc);
        ...
        }
        @

        he does not know, whether ChangedValue is calling someone else, is a sub functions (that's how it looks like) etc.

        Or he sees this

        @
        void MyClass::foo()
        {
        ... // doing some calculation
        emit ChangedValue(abc);
        ...
        }
        @

        which makes it clear: a signal is send to some connected objects.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          changsheng230
          wrote on last edited by
          #4

          Hi, Andre

          Gerolf makes it clearer:

           1)  it is OK to emit the function 
           2)  it is also OK to call signals directly without "emit"
          

          Though both 1) and 2) looks strange.

          About Andre's question" why? Why would you want to emit a function? What would that mean?"

          I once wrote some codes of "emit" the function by my careless, and assume that something serious will happen. Actually , nothing happened.

          Chang Sheng
          常升

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

            Indeed, Gerolfs's explanation that emit is defined to nothing is quite clear. I simply did not understand your question well enough at the first read.

            Yes, you can "emit" a function (though no slot will be called, at least not by Qt's doing) and you can simply call a signal, but as Gerolf showed that results in unclear code. That you can, does not mean that you should.

            Technically, it is all the same, but conceptually it is completely different, in my book. Not emitting signals or emitting functions in my opinion is bad coding practice. You mislead other developers (and yourself), which is not a good thing.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              Hi,

              as I write, emit is used to clearify the code, what you do.
              To changsheng230:

              Please use it in your code for signals to not irritate other people reading your code. And only use it for signals. That makes the code easier to read and maintain.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Franzk
                wrote on last edited by
                #7

                Better yet, handle it as if the behavior of the emit keyword might change in a future version.

                "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                http://www.catb.org/~esr/faqs/smart-questions.html

                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