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. QT Singleton short acces question

QT Singleton short acces question

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

    Hello,

    I have a singleton.h file which I use so I can acces my text to speech object in all my classes without making everytime a new object. Now I was wondering if there is a way to short out this acces.

    First of all the two .h files:

    File:  singleton.h
    template <class T>
    class Singleton
    {
    public:
      static T* Instance()
      {
         if(!m_Instance) m_Instance =  new T;
         return m_Instance;
      }
    protected:
      Singleton();
      ~Singleton();
    private:
      Singleton(Singleton const&);
      Singleton& operator=(Singleton const&);
      static T* m_Instance;
    };
    template <class T> T* Singleton<T>::m_Instance=NULL;
    
    File: texttospeech.h
    #include <QObject>
    #include "singleton.h"
    class TextToSpeech : public QObject
    {
      Q_OBJECT
    public:
      explicit TextToSpeech(QObject *parent = 0);
      ~TextToSpeech();
    
      void init();
      void speak(const QString &text);
    };
    typedef Singleton<TextToSpeech> tts;
    

    Every time I need to acces something of the texttospeech class I must do this:
    tts::Instance()->speak("Hello Word!");

    Is there a way that I can shorten the tts::Instance() ? I though as idea that I could add #define TTS tts::Instance() after the typedef in the texttospeech.h but I would like to hear other feedbacks.

    Thanks in advanced..

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

      Hi,

      You can take inspiration from the qApp macro sources

      Hope it helps

      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
      • P Offline
        P Offline
        ploef
        wrote on last edited by
        #3

        HI,

        Thanks for the help!

        I know have the following define:
        #define TTS tts:Instance()

        in my texttospeech.h file and it works.

        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