Search code examples
c++qtsignals-slots

Clear naming for Signal and Slots in QT


I have difficulty giving clear names for Signals and Slots in one of my class

It is an abstraction level for Controller to control Device: CommunicationAdapter

It does:

  1. sending parameter from Controller to Device (eg. user entering value on UI)
  2. request parameter from Device (get initial data)
  3. informing about the change of parameter in Device (an event occurred on Device)

Now I have something like this:

class CommunicationAdapter
{
    Q_OBJECT

public slots:
    //1: slot for Controller to set parameter
    void parameterReceived(Parameter parameter);

    //2: slot for Controller to request parameter
    void parameterRequested(Parameter parameter);
    //2: slot for Device to reply on parameter request
    void parameterReplied(Parameter parameter);

    //3: slot for Device to inform about change of parameter
    void parameterUpdated(Parameter parameter);

signals:
    //1: signal to Device
    void sendParameter(Parameter parameter);

    //2: signal to Device for sending parameter request
    void requestParameter(Parameter parameter);
    //2: signal to Controller for reply on parameter request
    void replyParameter(Parameter parameter);

    //3: signal to Controller for inform about change of parameter
    void updateParameter(Parameter parameter);
};

I found this naming ugly and not clear

Even I have difficulties to realize what is doing what after weekend

Do you see what I need to put comments in front of every line to explain the meaning of function?

What could be more clear names here?

Edit:

For the moment I solved it like this:

class ICommunicationAdapter : public IThreadObject
{
    Q_OBJECT

public slots:
    void setParameter(Parameter parameter);

    void getParameter(Parameter parameter);

    void subscribeParameter(Parameter parameter);

signals:
    void forwardParameter(Parameter parameter);

    void requestParameter(Parameter parameter);
};

Any feedback/comments are welcome


Solution

  • I would argue, that a slot should always be named after what it does - not how you trigger it. The same goes for signals, they should be named in past tense, stating what happened. This is the common scheme that Qt uses in all its classes so for consistency it makes sense to do the same.

    For an adapter class, it might be a good idea to use the same - or a similar - name as the function that the adapter will call. But that's quite specific and subject to opinion.

    From a logical standpoint, the Adapter shouldn't really care, who calls its functions, because that might change. Stating the effect of calling the function is more interesting to the user.

    So in your case, I would pretty much flip the names of your signals and slots. parameterReceived() should (read: "could") be the signal your controller emits. So an example chain of command will look like this:

    Controller::parameterRecieved() -> Adapter::sendParameter() -emits-> Adapter::parameterReceived() -> Device::sendParameter()

    This way you will keep the usual pattern of Event -> Action.