Results 1 to 8 of 8

Thread: How to redirect QMessageBox text to a Textfile

  1. #1
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

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

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to redirect QMessageBox text to a Textfile

    make a wrapper for all of the static calls. copy the text to your file before calling the Qt static method

    Qt Code:
    1. class MyMessageBox
    2. {
    3. public:
    4. static void information(..., QString msg)
    5. {
    6. myfile << msg;
    7. QMessageBox::information(..., msg);
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    alternatively, emit a signal with the string as an argument, and connect to it with some logging class

    Qt Code:
    1. QMessageBox::information(..., msg);
    2. emit MessageForLog(msg);
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    vikuseth (20th November 2012)

  4. #3
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to redirect QMessageBox text to a Textfile

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

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to redirect QMessageBox text to a Textfile

    Is there something like qInstallMsgHandler(..) ? No.

    it's not exactly a lot of effort to write one thin wrapper class and then 'replace all' on QMessageBox
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to redirect QMessageBox text to a Textfile

    You could install a global event filter that is triggered by show events on QMessageBox instances and the retrieve the text from the message box.

    However, it would be way cleaner to introduce a logging facility that can, as one of its output methods, show the text in a message box.
    In other words, make the logger the primary interface for logging errors and let it use QMessageBox when desired.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    vikuseth (22nd November 2012)

  8. #6
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to redirect QMessageBox text to a Textfile

    Thankx for your reply buddy ...really after a long time got a good responSo you are saying i have to create another class which shall be inherited from QMessagebox .Inside which i have to implement showevent() . When i want to show some messagebox i have to call the instance of this newly created class.. Please correct me if i am wrong .

    If i am going to implement this new concept inside my project , then i have to do lots of changes . since i am using around 100 messagebox . can you please give me any shortcut , so that i can implement just one function which will work for the previously existing as well as newly created messagebox .

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to redirect QMessageBox text to a Textfile

    As I said you could use an event filter to avoid that. You would create your event filter class and install it on the QApplication object. It would check for the show event and check if the widget being shown is a QMessageBox. If it is you read the text property of the object and write it to your file.

    See section "Event Filters" here http://doc.qt.digia.com/qt/eventsandfilters.html

    Cheers,
    _

  10. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to redirect QMessageBox text to a Textfile

    Quote Originally Posted by vikuseth View Post
    Thankx for your reply buddy ...really after a long time got a good responSo you are saying i have to create another class which shall be inherited from QMessagebox .Inside which i have to implement showevent() . When i want to show some messagebox i have to call the instance of this newly created class.. Please correct me if i am wrong .

    If i am going to implement this new concept inside my project , then i have to do lots of changes . since i am using around 100 messagebox . can you please give me any shortcut , so that i can implement just one function which will work for the previously existing as well as newly created messagebox .
    I think you are mixing up the two ideas. You can either create another class that inherits QMessagebox OR you can go the event filter route which could see you listen for 'show events'.

    Both methods would need a new class. The former would need you to also do 'replace all' on QMessageBox. The latter would need an extra one or two lines of code to install the filter. In either case you do not have to do 'lots of changes'.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. QMessageBox - Text in the buttons
    By graciano in forum Qt Programming
    Replies: 4
    Last Post: 20th June 2015, 23:51
  2. Select text in QMessageBox
    By mpi in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 08:55
  3. Replies: 1
    Last Post: 10th June 2010, 17:22
  4. SETTING THE TEXT COLOR FOR QMessageBox
    By vinkakarun in forum Newbie
    Replies: 2
    Last Post: 5th November 2009, 16:32
  5. Replies: 52
    Last Post: 10th December 2006, 14: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.