Using QObjects in namespaces



  • Hi,

    I'm fairly new to C++ (coming from Java) so this question might be more related to that instead of Qt.
    Please bear with me :)

    I want my application to have a globally available method that receives a QString for use with text-to-speech. For this, I need a QProcess which launches espeak with the string as an argument.

    I understand using a namespace is the recommended way.

    So I have;

    voice.h
    namespace voice
    {
    void speak(QString text);
    }

    voice.cpp
    namespace voice
    {
    void speak(QString text)
    {
    // ... use a QProcess
    }
    }

    So from here on, every class which includes voice.h can use the namespace method using voice::speak. Excellent.

    I'm not sure how to define the QProcess here though. In a class, I would define a private QProcess pointer and then create it in the constructor using new.

    So what is the recommended method using a QProcess in this case and how would I define it?
    Ideally, this pointer would be private, and I would also need to connect some of it's signals to private slots.

    Thanks!


  • Moderators

    Global functions in namespaces or not is not what I'd recommend. Why not just a class with a static member?

    @
    class Voice {
    public:
    static void speak(QString text) { QProcess::startDetached(/.../); }
    }
    @


  • Lifetime Qt Champion

    Hi,

    Not directly related to your question (although I would say it depends on the use case) but are you trying to implement some sort of accessibility feature ? If so, Qt provides a module for that that you could use


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.