PDA

View Full Version : Change date not working



sousadaniel7
7th May 2012, 13:26
Hi I'm trying to reschedule a QDate, but do not know why that is not working.

I have the following code:


qDebug() << ano2;
qDebug() << mes2;
qDebug() << dia2;

bool b = ui->data_inicio->date().setDate(ano2, mes2, dia2);
qDebug () << b;



In qDebug appears fine. Until b returns true.

Why does not the date and amended?




regards,
Daniel Sousa

amleto
7th May 2012, 16:55
Not enough info. read sig.

Most likely date() returns a temporary and not the actual date object used in data_inicio. This is where reading the docs comes in handy.

wysota
7th May 2012, 17:27
date() returns a copy of the object. Calling setDate() on that will change the copy and not the original date object.

sousadaniel7
7th May 2012, 17:33
Not enough info. read sig.

Most likely date() returns a temporary and not the actual date object used in data_inicio. This is where reading the docs comes in handy.

But I've been reading and I do not understand the problem.

I do not know and explain how I change the date of a QDate through the code?

amleto
7th May 2012, 18:39
you've been reading? So you saw this http://qt-project.org/doc/qt-4.8/qdatetime.html#setDate ?

sousadaniel7
8th May 2012, 11:31
you've been reading? So you saw this http://qt-project.org/doc/qt-4.8/qdatetime.html#setDate ?

And that's what I did. The following code should work right?

bool b = ui->data_inicio->date().setDate(2012, 10, 5);

d_stranz
8th May 2012, 18:33
ui->data_inicio->date()

And what does this method return? Does it return QDate? Or QDate & ? If it returns QDate, then this is a temporary, a copy of the QDate stored in data_incio. Changing the copy has no effect at all on on the original QDate stored in data_incio.

Of course the setDate() function call will return true, because even if the QDate is a temporary, setting it to a new date will work just fine. It just has no effect, because at the end of that line of code, the copy is destroyed and whatever changes you made go with it.

sousadaniel7
8th May 2012, 19:13
And what does this method return? Does it return QDate? Or QDate & ? If it returns QDate, then this is a temporary, a copy of the QDate stored in data_incio. Changing the copy has no effect at all on on the original QDate stored in data_incio.

Of course the setDate() function call will return true, because even if the QDate is a temporary, setting it to a new date will work just fine. It just has no effect, because at the end of that line of code, the copy is destroyed and whatever changes you made go with it.

But then how can I do this?

amleto
8th May 2012, 20:01
And that's what I did. The following code should work right?

bool b = ui->data_inicio->date().setDate(2012, 10, 5);

no, that's not what you did, and no, it shouldn't work.

Look carefully!
http://qt-project.org/doc/qt-4.8/qdatetime.html#setDate
http://qt-project.org/doc/qt-4.8/qdate.html#setDate

and try again...

wysota
8th May 2012, 21:07
But then how can I do this?

Maybe you should call setDate() on the ui->data_inicio object?

d_stranz
8th May 2012, 21:34
Maybe you should call setDate() on the ui->data_inicio object?

Or maybe, since Daniel won't tell us what "data_inico" is, we can just keep guessing at answers until one of us guesses correctly.

sousadaniel7
17th May 2012, 22:55
Or maybe, since Daniel won't tell us what "data_inico" is, we can just keep guessing at answers until one of us guesses correctly.

ui->data_inicio is a QDateEdit

I've tried many ways and I can not change its value ...

amleto
18th May 2012, 00:30
try reading the thread

try reading the docs http://qt-project.org/doc/qt-4.8/qdatetimeedit.html#public-functions

sousadaniel7
18th May 2012, 00:34
I managed to solve the problem.

Now I can not and the pm or am a QTimeEdit. I've had to do the builders and they do not allow for it ... How do I solve this?

amleto
18th May 2012, 00:41
http://qt-project.org/doc/qt-4.8/qdatetimeedit.html#displayFormat-prop

sousadaniel7
18th May 2012, 00:45
http://qt-project.org/doc/qt-4.8/qdatetimeedit.html#displayFormat-prop

I have the following code.


QStringList list_hora_inicio = fg.separarHora(query.value(8).toString());
QString horaInicio = list_hora_inicio[0];
QString minutosInicio = list_hora_inicio[1];
QString amanha_tarde = list_hora_inicio[2];
ui->hora_incio->setTime(QTime(horaInicio.toInt(&ok, 10), minutosInicio.toInt(&ok, 10)));

Where do I put the AM or PM?

amleto
18th May 2012, 01:28
is there a brick wall I can borrow? ...

spoon feeding ...
qt-project.org/doc/qt-4.8/qdatetime.html#fromString

ChrisW67
18th May 2012, 01:43
Where do I put the AM or PM?
You don't. You read the documentation... it is obvious. I'll even highlight the bit that answers your question


QTime::QTime ( int h, int m, int s = 0, int ms = 0 )

Constructs a time with hour h, minute m, seconds s and milliseconds ms.
h must be in the range 0 to 23, m and s must be in the range 0 to 59, and ms must be in the range 0 to 999.

What is 3 PM on a 24-hour clock?

d_stranz
18th May 2012, 02:56
@amleto: is there a brick wall I can borrow? ...

Sorry, I'm using that wall right now. If you can answer the question in my post (http://www.qtcentre.org/threads/49021-QGraphicsItem-setCacheMode-how-does-it-work), I will let you have the wall until I need it again. :(