我应该以其他方式使用插槽

问题描述

以其他方式使用插槽是否正常?像这里一样,我在Increment()中使用releaseIncrement()来避免代码重复:

#include "counter1.h"
#include <iostream>

Counter1::Counter1(int startValue)
{
    m_count = startValue;
}

int Counter1::getValue()
{
    return m_count;
}

int Counter1::Increment()
{
    std::cout <<"In class: " <<this <<" method Increment() was called" <<std::endl;
    releaseIncrement(); // I used slot here to avoid code duplicate
    emit wasIncremented();
    return m_count;
}

int Counter1::Decrement()
{
    std::cout <<"In class: " <<this <<" method Increment() was called" <<std::endl;
    releaseDecrement(); // I used slot here to avoid code duplicate
    emit wasDecremented();
    return m_count;
}

void Counter1::releaseIncrement()
{
    m_count++;
    std::cout <<"In class: " <<this << " m_count was incremented by releaseIncrement() methos. Now m_count is: " <<m_count <<std::endl;
}

void Counter1::releaseDecrement()
{
    m_count--;
    std::cout <<"In class: " <<this << " m_count was decremented by releaseDecrement() methos. Now m_count is: " <<m_count <<std::endl;
}

它可以正常工作,但是我觉得我做错了,这是一种不好的做法。你能建议我一些东西吗?

解决方法

如果您问是否可以将插槽功能用作普通功能,答案是肯定的-插槽没有什么特别之处。您可以在任何地方使用它们。