How to redirect QMessageBox text to a Textfile



  • In my project lots of time i am using QMessageBox. which is for warning / information . For e.g. If i am having some validation then at that time i am using some QMessageBox::information(...) , or if i want to restrict the user not to move out of a particular page then i am using QMessageBox::critical(...) .
    I know "How to redirect qDebug output to a file".I want to do the same thing for the QMessagebox too. so that i will maintain a text file in my project folder . whenever the application run if the user encounters some message through QMessagebox , then that shall be redirected to that text file .So that after closing the application the user have a small list of messagedetails ,whatever he has encountered during running the application .

    Please help me in writting this code ..

    Thankxx in advance .



  • How about you create a logger class , so whereever you are using QMessageBox you can log the messages that you encounter n have a function like dumpToConsole() or dumpToFile() that will actually flush/write to a file which is created on the same level where your project is running.

    You can also search for QxtLogger.



  • Is there something like qInstallMsgHandler(..) ?
    So that we dont have to implement a new class . since lots of times i have used this QMessagebox() in my project .So if i follow the above concept then i have to write emit statement for each of that QMesagebox .
    Can you please give some alternative solution ..
    Thankx a lot for your reply ...



  • i did not find anything about QxtLogger..
    can you please share something on it ?
    [quote author="Sam" date="1353417994"]How about you create a logger class , so whereever you are using QMessageBox you can log the messages that you encounter n have a function like dumpToConsole() or dumpToFile() that will actually flush/write to a file which is created on the same level where your project is running.

    You can also search for QxtLogger.[/quote]



  • [quote author="Vikuseth" date="1353420859"]Is there something like qInstallMsgHandler(..) ?
    So that we dont have to implement a new class . since lots of times i have used this QMessagebox() in my project .So if i follow the above concept then i have to write emit statement for each of that QMesagebox .
    Can you please give some alternative solution ..
    Thankx a lot for your reply ...[/quote]

    Why not use qInstallMsgHandler() as-is?

    You install your own message handler function, in which you can show the message in a QMessageBox, dump the text to a file or do whatever you like with the text. Then you simply use qDebug() and friends instead of creating a QMessageBox directly and let the message handler function take care of the rest...

    @ void myMessageOutput(QtMsgType type, const char *msg)
    {
    switch (type) {
    case QtDebugMsg:
    QMessageBox::information(QApplication::activeWindow(), tr("Message"), QString::fromLocal8Bit(msg));
    fprintf(textfile, "Debug: %s\n", msg);
    break;
    case QtWarningMsg:
    QMessageBox::warning(QApplication::activeWindow(), tr("Message"), QString::fromLocal8Bit(msg));
    fprintf(textfile, "Warning: %s\n", msg);
    break;
    case QtCriticalMsg:
    QMessageBox::critical(QApplication::activeWindow(), tr("Message"), QString::fromLocal8Bit(msg));
    fprintf(textfile, "Critical: %s\n", msg);
    break;
    case QtFatalMsg:
    QMessageBox::critical(QApplication::activeWindow(), tr("Message"), QString::fromLocal8Bit(msg));
    fprintf(textfile, "Fatal: %s\n", msg);
    abort();
    }
    }

    int main(int argc, char **argv)
    {
    qInstallMsgHandler(myMessageOutput);
    QApplication app(argc, argv);
    ...
    return app.exec();
    }@

    http://doc.qt.digia.com/qt/qtglobal.html#qInstallMsgHandler



  • Hello MuldeR.. Thanks for replying ..Actually i am not asking for popping a mesagebox for QtDebugMsg , QtWarningMsg etc... What i am asking is whenever there is some QMessagebox encounters in the programme , the message inside it shall be written inside a text file present inside the same project folder .Just Like qInstallMsgHandler(..) do for the debug, warning .. messages .Insted of doing it for the above mentioned message i want to do it for QMessagebox..Is there any way to do that except the above mentioned method .

    Simply i want to follow the concept of qInstallMsgHandler() for QMessagebox..



  • I don't think the existing QMessageBox supports that functionality. Thus, you can do it like I suggested above. Then you'd simply call qDebug() and friends where you usually would call QMessageBox::information() and friends - and let the message handler function take care of everything. If that approch isn't good for you, for some reason, you will have to create your own class that inherits from QMessageBox and adds the desired functionality and/or overwrites the existing functionality as needed...



  • Ok, I have not tried this myself, but I would try this:

    Create an object that you install as an event filter on your QApplication. Listen for ShowEvents, and see if the object it was aimed at is a QMessageBox subclass. If so, you can read the text from it and log that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.