Results 1 to 2 of 2

Thread: qInstallMsgHandler() error while registering non static function

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qInstallMsgHandler() error while registering non static function

    I am designing Qt log window, So I am using qInstallMsgHandler() function to log all the debug, critical & warning messages on to QTableWidget (in below code I have not implemented It yet). I did as below

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. qInstallMsgHandler(MainWindow::logMessage);
    4. //qInstallMsgHandler(&MainWindow::logMessage); //I tried this also
    5.  
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. qDebug() << "info message";
    9. qWarning() << "warning message";
    10. qCritical() << "critical message";
    11. w.show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
    2. {
    3. ui->setupUi(this);
    4. }
    5.  
    6. MainWindow::~MainWindow()
    7. {
    8. delete ui;
    9. }
    10.  
    11. void MainWindow::logMessage(QtMsgType type, const char *msg)
    12. {
    13. switch (type)
    14. {
    15. case QtDebugMsg:
    16. fprintf(stderr, "Debug: %s\n", msg);
    17. break;
    18. case QtWarningMsg:
    19. fprintf(stderr, "Warning: %s\n", msg);
    20. break;
    21. case QtCriticalMsg:
    22. fprintf(stderr, "Critical: %s\n", msg);
    23. break;
    24. case QtFatalMsg:
    25. fprintf(stderr, "Fatal: %s\n", msg);
    26. abort();
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    When I compile this code, I am getting below error

    Qt Code:
    1. main.cpp:27: error: cannot convert 'void (MainWindow::*)(QtMsgType, const char*)' to 'QtMsgHandler {aka void (*)(QtMsgType, const char*)}' for argument '1' to 'void (* qInstallMsgHandler(QtMsgHandler))(QtMsgType, const char*)'
    2. qInstallMsgHandler(MainWindow::logMessage);
    To copy to clipboard, switch view to plain text mode 

    Note : If I changed void MainWindow::logMessage(QtMsgType type, const char *msg); this function to static function then it is working fine.
    (But if this function is static, I can not create QTableWidgetItem and add them to tableWidget so I want this function to be non static).

    And one more issue i am having here, When I use static function, all the messages are printing after I close my application only.

    Please let me know What is the issue.

    I am using Qt4.8.6 on Windows7

    Thanks In Advance.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: qInstallMsgHandler() error while registering non static function

    The QtMsgHandler function does not take a "this" pointer, which is why you can't use a non-static member function. Either make it a non-member function or a static member function.

    Try adding a fflush(stderr) at the end of your logging function, which will ensure your messages are not buffered and shown immediately after they are written.

  3. The following user says thank you to jefftee for this useful post:

    DURGAPRASAD NEELAM (25th May 2015)

Similar Threads

  1. Replies: 0
    Last Post: 18th October 2012, 06:03
  2. Replies: 11
    Last Post: 5th September 2012, 20:47
  3. Replies: 1
    Last Post: 6th December 2011, 08:39
  4. static building qt, error: declaration of c function conflicts
    By BlueWizard in forum Installation and Deployment
    Replies: 4
    Last Post: 30th July 2010, 12:50
  5. QtHelp Module - registering compressed help file gives error
    By Ankitha Varsha in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2008, 14:14

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.