Results 1 to 3 of 3

Thread: Error while calling installing singleton class Qt Message Handler member function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Error while calling installing singleton class Qt Message Handler member function

    From the docs:

    typedef QtMessageHandler

    This is a typedef for a pointer to a function with the following signature:

    void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
    The important words are pointer to a function, not pointer to a MEMBER function. This typically means a stand-alone function, but in your case you might be able to use a static member function of your Log class.

    However, I would turn your design inside-out. Implement the message handler as a stand-alone function, but inside the handler, relay the message to the singleton's handler:

    Qt Code:
    1. class Log : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. //class member declaration
    6.  
    7. static Log& ReturnInstance()
    8. {
    9. static Log Log_obj; //The only Log object that will be used
    10. return Log_obj;
    11. }
    12. void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);
    13.  
    14. private:
    15. //some member variable declartion
    16. QFile m_file;
    17.  
    18. };
    19.  
    20. static void theMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg)
    21. {
    22. Log & log = Log::ReturnInstance();
    23. log.myMessageHandler( type, context, msg );
    24. }
    25.  
    26. #include "mainwindow.h"
    27. #include <QApplication>
    28. #include "logger.h"
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. qInstallMessageHandler( theMessageHandler ); //<------ The former error
    33. QApplication a(argc, argv);
    34. MainWindow w;
    35. w.show();
    36.  
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Sep 2017
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8

    Thumbs up Re: Error while calling installing singleton class Qt Message Handler member function

    Thanks a lot

Similar Threads

  1. How to add class member function in QScriptEngine
    By Sacha_D in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2013, 17:10
  2. Replies: 2
    Last Post: 11th July 2012, 01:18
  3. Calling class functions from another function
    By prophet0 in forum General Programming
    Replies: 2
    Last Post: 2nd March 2012, 22:16
  4. Replies: 7
    Last Post: 2nd September 2010, 20:42
  5. Problems calling C function in C++/Qt class
    By Rayven in forum General Programming
    Replies: 2
    Last Post: 2nd June 2006, 22:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.