QT's QDate::toString() function, without parameters, converts a QDate to a QString with a default format of "ddd MMM d yyyy". Our application is international and this fixed format does not reflect locale and regional settings. I don't want to use LongFormat because it takes too much space; the default no-parameter is a more optimal length. I have obtained the LongFormat from system QLocale massaged the format string to give us a QString similar to the default format but also reflects international settings.
Is there any way I can tell QT to use my new formatting string whenever toString() is called so that I don't have to find all existing toString() calls and insert the formatting string as a parameter?
According to the Qt documentation of
QDate, you can specify the format you desire inQDate::toString().Now, to avoid the repetitions that are bothering you, you can specify somewhere a
staticvariable that contains the application formatting. Then you give it as parameter everytime you callQDate::toString(). This way, you will have to use always the same variable/format.But if you really want to not give any parameters, the solution is to subclass
QDateand redefine thetoString()method by changing the default format by the one you want.For example:
.h:
.cpp:
And you can you it as follows (example):
Here I have named the method
toFormattedString()in order to make the code more understandable. Feel free to adapt it as you want.I hope it will help.