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. Using QObjects in namespaces
Forum Updated to NodeBB v4.3 + New Features

Using QObjects in namespaces

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.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.
  • S Offline
    S Offline
    SQEW
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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(/.../); }
      }
      @

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

        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

        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

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved