PDA

View Full Version : QDateTime application level format string



davidovv
9th March 2013, 01:53
I am trying to change default datetime format for my entire application. What i was expecting is QDateTime static method that sets default string format. With no luck of course.
I also tried QLocale::setDefault, but there i am only able to choose country/language. (or i am missing something). Code refactoring and replacing QDateTime to string functions doesnt seam like a good idea.
What is the proper way to set custom global datetime format for application?

wysota
9th March 2013, 20:33
What is the proper way to set custom global datetime format for application?

Write and use a function that does that.

davidovv
9th March 2013, 22:16
Write and use a function that does that.
wysota, that is a teasing comment

Is there a single place where i can set datetime format string so that it afects every QDateTime::toString() function, not only for datetime objects that i create in code, but also for datetime widgets (like QDateTimeEdit and model based views that display datetime fields from database)
Qlocale seamed like the place, but there i could set default language and country, not the custom format.

If there is no such place, how do fellow programmers usualy "write" function that does that.

wysota
9th March 2013, 23:44
If there is no such place, how do fellow programmers usualy "write" function that does that.
They don't because fellow programmers don't feel a need for such functions. If fellow programmers want to change default behavior of something, they reimplement virtual methods of objects using them, e.g. they implement custom delegates for views if they want to show custom content in them. If fellow programmers want to shoot themselves into their feet, they take a gun and pull the trigger.

And seriously if you try to change the default format of the date output, you break all the functionality already in Qt depending on that format being exactly how it is now.

davidovv
10th March 2013, 00:06
wysota, now you criticize your own comment,
first you say "write fucntion that does that"
and after "fellow programmers don't feel a need for such functions"

anyway, What i am trying right now is to reimplement QLocale::dateFormat and QLocale::dateTimeFormat and to call QLocale::setDefault(myLocale)
since those are not virtual methods and i am getting strange compilation errors as soon as i include derived class files, is this what i am doing even possible?

wysota
10th March 2013, 00:11
wysota, now you criticize your own comment,
No, I don't.


first you say "write fucntion that does that"
and after "fellow programmers don't feel a need for such functions"
Yes. We don't need that. If you want it, implement it yourself. If you don't want to implement it yourself because you'd want it to be already implemented for you then wait until someone else does it which is very doubtful because of my second sentence of this paragraph.


anyway, What i am trying right now is to reimplement QLocale::dateFormat and QLocale::dateTimeFormat and to call QLocale::setDefault(myLocale)
Bad idea since non of these are virtual. And I really have no idea why you'd want that in the first place.


is this what i am doing even possible?
I have no idea what you are trying to do. I mean I know you're trying to make something default not default but why you are doing that is a riddle to me.

davidovv
10th March 2013, 01:20
No, I don't.Yes you do, you say write function and then We don't need that


If fellow programmers want to shoot themselves into their feet, they take a gun and pull the trigger.What is the point of this edit??


And I really have no idea why you'd want that in the first place.I want to set datetime format once, in one place, so that every form, widget, edit... that displays datetime uses that format, I dont want to go to every file in the project and search for datetime usage and change functions, i dont want to go to every model based view i created long time ago, and implement custom datetime delegate. QLocale "almost" does what i need, i create

int main(int argc, char *argv[]){
QLocale locale(QLocale::Polish, QLocale::Poland);
QLocale::setDefault(locale);
...
and it afects every datetime view, but my country format is wrong, and even if it was correct, i would like to tune it.

If you can't or won't help, please dont comment at all


Write and use a function that does that. comment like this isn't helpfull.

ChrisW67
10th March 2013, 05:04
No amount of to-and-fro will make a function that does not exist suddenly spring into existence. If you want a date format that differs from the locale implied one then you need to code for it. There's no mechanism that can intercept all argument-free calls to QDateTime::toString() and QLocale equivalent. Even if there were, the default item delegate for item views formats the date explicitly in QLocale::ShortFormat so you would still have to replace the delegates relying on default format.

If you think such an ability is globally important then by all means code it, submit for inclusion in Qt, and fight for it. Since you will be proposing breaking compatibility with all existing Qt code you will need to wait a while to get it in.

wysota
10th March 2013, 09:08
Yes you do, you say write function and then We don't need that
Yes. I don't need that, Chris doesn't need that. Thus we won't write that function. If you need that then by all means write such a function, rebuild Qt and be happy in your life.


What is the point of this edit??
The point is to show you that you are trying to shoot yourself in your foot by trying to break something that works. I understand that you want some behaviour that is not default, but trying to make it default is not a good solution. I'm sorry that you have to go through your code and reimplement 10, 20, 100 or 1000 views to set a new delegate for them (if you know how to use search and replace, you can do that in 10 seconds for all your views). If you have thought about it upfront when you were coding your views or whatever you have there, you would have saved yourself time now. Since you haven't then you have to do that additional work. You can take a shortcut and just change Qt in a couple of places specifically for your program but I strongly doubt it will not backfire on you sooner or later.

So again, if you want a function that will convert a date to a custom string format, write such a function and use it wherever it needs to be used. Just like I wrote in my first reply in this thread.

davidovv
10th March 2013, 13:24
If i was doing everything right, i wouldn't be here. Also, i am not lazy, and if proper way is to reimplement my views, i will do it. I allready spent more time on this thread than it would take me to refactor my code. First I wanted to check here if anyone knows a better, more centralised way to do it. When i was coding my views i did think upfront, and used default toString() behavieour (even if default didn't satisfy me), thinking that i could change this default behaviour with qlocale for example.

Pardon me if i still think that if there should be a way to set global default behaviour manualy, not just by choosing country and language, or implementing custom delegates on every view.

and another thing, you constantly repeat "we dont need that", i supose you dont go from thread to thread and if someone neads something you brag that "you dont need that"
Maybe something like "it is a bad idea because of...." or "you should do this instead..." would be more appropriate

wysota
10th March 2013, 13:58
Pardon me if i still think that if there should be a way to set global default behaviour manualy, not just by choosing country and language, or implementing custom delegates on every view.
A public function (any public function) that is part of a library (any library) can be called by any code -- your code or code of the library itself. If you set a "global" behaviour then you influence not only your code but also code of the library itself that can depend on receiving this and not other result from a function it knows to work in a particular way. For instance, there might be a function somewhere that extracts the textual description of something using the string and you globally change the format of the string, that code will simply fail and you'll be spending hours and hours trying to find the problem or complaining that the library is buggy because "it used to work yesterday and now it doesn't".


and another thing, you constantly repeat "we dont need that", i supose you dont go from thread to thread and if someone neads something you brag that "you dont need that"
My response was a direct response to your statement about fellow programmers. Fellow programmers don't (let me quote your post) "usualy »write« function that does that" because fellow programmers don't need a function that does that. If you want to abuse a function then go ahead -- do it at your own responsibility but don't expect that there is (or should be) a built-in way to do that.

Read my posts again and think whether they are "bragging" anything or merely answering questions you ask. If you ask wrong questions, don't expect to receive answers to right questions.

davidovv
10th March 2013, 15:34
Fellow programmers don't (let me quote your post) "usualy »write« function that does that"
so you quote my post, where i quoted and repeted your words from first post, and reproach me for wanting something you sugested??


Read my posts again
you edited your post after my reply, my reply was to you original post, if the post was complete maybe i wouldnt ask no more questions. Since that moment this thread had more of quoting quotes, than talking about specific problem

to summarize, i was simply asking how do you customize datetime in my application, asked if it is good idea to tune QLocale, and you answer is there is no easy way that wouldn't create other problems, and that i have to implement custom delegates, and refactor my code.

Dont get me wrong, you are helpful and you helped few times in the past and now, but you are dificult man to talk to.

Added after 4 minutes:

and another thing, there is a proverb

There are no wrong questions, only unasked ones

wysota
10th March 2013, 15:53
Added after 4 minutes:

and another thing, there is a proverb

There are no wrong questions, only unasked ones

Look, this is really simple -- If you ask a wrong question, we can either bluntly say it's wrong and try to determine what is the real question you should have asked or we can simply ignore your wrong question. I usually tend to do the first because I have an impression that this lets us still solve the real problem (despite maybe worsened mood of the person asking) in contrast to just ignoring your question which lets me save my mood and time (and what the netiquette suggests anyway) but makes you stay in exactly the same spot you have been prior to asking that question.

In other words, if you ask a wrong question, it can very likely remain without an answer, paraphrasing your proverb to "there are no wrong questions, only unanswered ones".