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. How to redirect QMessageBox text to a Textfile

How to redirect QMessageBox text to a Textfile

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.0k Views
  • 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.
  • B Offline
    B Offline
    BibekKumar
    wrote on last edited by
    #1

    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 .

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BibekKumar
        wrote on last edited by
        #3

        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 ...

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BibekKumar
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

            [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

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BibekKumar
              wrote on last edited by
              #6

              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..

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                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...

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  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