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 Best Way to Signal Using an Abstract Argument
Forum Updated to NodeBB v4.3 + New Features

Signal/Slot Best Way to Signal Using an Abstract Argument

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.9k 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.
  • K Offline
    K Offline
    kevins963
    wrote on last edited by
    #1

    I have a question on how to best go about creating a signal/slot using a base class as as the signal parameter.

    So I have the following signal argument

    @
    enum eSignalArgumentType
    {
    SignalTypeOne,
    SignalTypeTwo
    }

    ISignalArgument
    {
    virtual eSignalArgumentType GetSignalType() = 0
    }

    SignalOneArgument : ISignalArgument
    {
    eSignalArgumentType GetSignalType() { return SignalTypeOne; }

    ...Other functions specific to the information it needs to hold...
    }

    EmittingSignalClass
    {
    void EventToSignal( ISignalArgument * )
    }
    @

    Now my question is what is the best way to implement the signal emit EventToSignal(...) I want the argument of the EventToSignal to be the base class ISignalArgument because the derived argument will hold different types of information based on the what information has been processed.

    If I pass the following way then the slot class will be required to handle the deleting which seems a little bit dangerous to me.

    @
    EmittingSignalClass
    {
    ProcessData( char* data)
    {
    ... Parsing Logic ...

    Is of Type DataTypeOne
    SignalOneArgument * arg = new SignalOneArguemnt();
    emit EventToSignal( arg );

    ... Same for other Data Types ...
    }
    }

    SlotClass
    {
    void OnEventSlot( ISignalArgument * arg)
    {
    if(arg->GetType() == SignalTypeOne)
    {
    SignalTypeOne * argument = reinterpret_cast<SignalTypeOne*>(arg>
    HandleSignalTypeOne( argument )

    delete arg
    

    }

    }
    }
    @

    Is there a better way to implement this signal/slot scenario because if the signal is hooked up to multiple slots and the first one deletes the pointer it will be lost to all the rest.

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

      Hi,
      Don't use new, rather send a const reference to your slot.

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Santosh Reddy
        wrote on last edited by
        #3

        Then delete after emiting signal..

        @SignalOneArgument * arg = new SignalOneArguemnt();
        emit EventToSignal( arg );
        delete arg;@

        SS

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

          That will crash if the signal is connected to using a queued connection. And note that the connection method used is outside of the control of the class where the signal is defined.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Santosh Reddy
            wrote on last edited by
            #5

            Yes it will not work for queued connections

            SS

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

              [quote author="Santosh Reddy" date="1361345906"]Yes it will not work for queued connections[/quote]

              It will not only not work, it will cause undefined behaviour and most likely a crash. IMO, that is a very good reason to recommend against this practice. Again: I must stress that as a developer of this particular piece of code, you have no control over how this signal will be connected to.

              If you must pass a pointer, consider passing a QSharedPointer instead:

              @
              QSharedPointer<SignalOneArgument> arg = QSharedPointer<SignalOneArgument>(new SignalOneArgument());
              emit eventToSignal(arg);
              // don't delete manually, it will happen automatically when needed.
              @

              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