Results 1 to 3 of 3

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

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

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

    I am trying to direct the Qt debugging messages into a text log file. while trying to install the message handler function i have written, the following error occurred:
    Qt Code:
    1. main.cpp: error: C3867: 'Log::myMessageHandler': function call missing argument list; use '&Log::myMessageHandler' to create a pointer to member
    To copy to clipboard, switch view to plain text mode 
    please note that :

    1. I have declared the message handler function myMessageHandler as a member function in the singleton Logger class.

    2. myMessageHandler function was accessed using the[B] static method ReturnInstance().
    3. The function myMessageHandler was installed in the main function of my application using qInstallMessageHanler() function.

    The code is as follows:
    Qt Code:
    1. /****************************Log.h***********************/
    2. class Log : public QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. //class member declaration
    7.  
    8. static Log& ReturnInstance()
    9. {
    10. static Log Log_obj; //The only Log object that will be used
    11. return Log_obj;
    12. }
    13. void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);
    14. private:
    15. //some member variable declartion
    16. QFile m_file;
    17.  
    18. };
    19. /****************************Log.cpp***********************/
    20. //constructor definition
    21. //destructor definition
    22. void Log::myMessageHandler(QtMsgType type, QMessageLogContext &context, const QString &msg)
    23. {
    24.  
    25. QTextStream ostream(&m_file);
    26.  
    27. switch (type) {
    28. case QtDebugMsg:
    29. ostream<<("Debugging Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
    30. break;
    31. case QtInfoMsg:
    32. ostream<<("Information Message: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function);
    33. break;
    34. case QtWarningMsg:
    35. ostream<<("Warnning Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
    36. break;
    37. case QtFatalMsg:
    38. ostream<<("Fatal Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
    39. break;
    40. case QtCriticalMsg:
    41. ostream<<("Critical Message: %s (%s:%u, %s)\n" , msg, context.file, context.line, context.function);
    42. break;
    43. default:
    44. break;
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 
    And here is the installation of the message handler function:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include "logger.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. Log::ReturnInstance();
    8. qInstallMessageHandler(Log::ReturnInstance().myMessageHandler); //<------ The error
    9. QApplication a(argc, argv);
    10. MainWindow w;
    11. w.show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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.

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.