PDA

View Full Version : How to redirect QMessageBox text to a Textfile



vikuseth
20th November 2012, 13:17
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 .

amleto
20th November 2012, 13:24
make a wrapper for all of the static calls. copy the text to your file before calling the Qt static method



class MyMessageBox
{
public:
static void information(..., QString msg)
{
myfile << msg;
QMessageBox::information(..., msg);
}
};

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



QMessageBox::information(..., msg);
emit MessageForLog(msg);

vikuseth
20th November 2012, 14:14
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 ...

amleto
20th November 2012, 14:51
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

anda_skoa
20th November 2012, 18:16
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,
_

vikuseth
22nd November 2012, 05:07
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 .

anda_skoa
23rd November 2012, 15:17
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,
_

amleto
23rd November 2012, 18:02
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'.