PDA

View Full Version : Get typed time from QTimeEdit



Reiji
29th May 2015, 09:44
Hi,
I am developing an adnroid app which can calculate the time in other time zones.
For displaying the time I am useing the QTimeEdit element.
Now I want to that the user also can type in a time.
But I don't get it to work that it get's the typed time.

I tried it like that:



void WeltZeit::on_ENtimeEdit_timeChanged(const QTime &time)
{
QTime enTime = ui->ENtimeEdit->time();
enTime.addSecs(3600); //adds an hour
ui->DEtimeEdit->setTime(enTime);
}

Lesiok
29th May 2015, 09:47
Line 3 and 4 should be :
QTime enTime = ui->ENtimeEdit->time().addSecs(3600); //adds an hour
addSecs() does not modify the source object only returns new object.

Reiji
29th May 2015, 11:25
Line 3 and 4 should be :
QTime enTime = ui->ENtimeEdit->time().addSecs(3600); //adds an hour
addSecs() does not modify the source object only returns new object.

Thanks it works. :)

Added after 1 21 minutes:

After I did that with all QTimeEdits it doesn't work anymore. :(




void WeltZeit::on_ENtimeEdit_timeChanged(const QTime &time)
{
QTime deTime = ui->ENtimeEdit->time().addSecs(3600); //adds an hour
ui->DEtimeEdit->setTime(deTime);
QTime nyTime = ui->ENtimeEdit->time().addSecs(-18000);
ui->NytimeEdit->setTime(nyTime);
if (DST == 1)
{
QTime jpTime = ui->ENtimeEdit->time().addSecs(28800);
ui->NytimeEdit->setTime(jpTime);
}
else
{
QTime jpTime = ui->ENtimeEdit->time().addSecs(32400);
ui->NytimeEdit->setTime(jpTime);
}

}


void WeltZeit::on_DEtimeEdit_timeChanged(const QTime &time)
{
QTime enTime = ui->DEtimeEdit->time().addSecs(-3600); //adds an hour
ui->ENtimeEdit->setTime(enTime);
QTime nyTime = ui->DEtimeEdit->time().addSecs(-21600);
ui->NytimeEdit->setTime(nyTime);
if (DST == 1)
{
QTime jpTime = ui->DEtimeEdit->time().addSecs(25200);
ui->JPtimeEdit->setTime(jpTime);
}
else
{
QTime jpTime = ui->DEtimeEdit->time().addSecs(28800);
ui->JPtimeEdit->setTime(jpTime);
}
}

void WeltZeit::on_JPtimeEdit_timeChanged(const QTime &time)
{
if (DST == 1)
{
QTime enTime = ui->JPtimeEdit->time().addSecs(-28800);
ui->ENtimeEdit->setTime(enTime);
QTime deTime = ui->JPtimeEdit->time().addSecs(-25200);
ui->DEtimeEdit->setTime(deTime);
QTime nyTime = ui->JPtimeEdit->time().addSecs(-46800);
ui->NytimeEdit->setTime(nyTime);
}
else
{
QTime enTime = ui->JPtimeEdit->time().addSecs(-32400);
ui->ENtimeEdit->setTime(enTime);
QTime deTime = ui->JPtimeEdit->time().addSecs(-28800);
ui->DEtimeEdit->setTime(deTime);
QTime nyTime = ui->JPtimeEdit->time().addSecs(-50400);
ui->NytimeEdit->setTime(nyTime);
}
}

void WeltZeit::on_NytimeEdit_timeChanged(const QTime &time)
{
QTime enTime = ui->NytimeEdit->time().addSecs(18000); //adds an hour
ui->ENtimeEdit->setTime(enTime);
QTime deTime = ui->NytimeEdit->time().addSecs(21600);
ui->DEtimeEdit->setTime(deTime);
if (DST == 1)
{
QTime jpTime = ui->NytimeEdit->time().addSecs(46800);
ui->JPtimeEdit->setTime(jpTime);
}
else
{
QTime jpTime = ui->NytimeEdit->time().addSecs(50400);
ui->JPtimeEdit->setTime(jpTime);
}
}


A output of the application is aloso No such signal QTimeEdit::cursorPositionChanged() but I have set the signal on_NytimeEdit_timeChanged().

Reiji
29th May 2015, 13:17
Now I can't type in any other time by keyboard in a QTimeEdit element. It just resets the time to the old time.

I just recovered an old version of my app. So the problem that I couldn't type a new time by keyboard is vanished. But the problem that it doesn't calculate with the typed time still excists.

ChrisW67
30th May 2015, 06:49
A output of the application is aloso No such signal QTimeEdit::cursorPositionChanged()

The warning is perfectly correct but this has nothing to do with the code you have posted. Somewhere in your program you are trying to connect() a QTimeEdit object with an invalid signal name. If you never called connect() directly then you probably have a function on_someTimeEdit_cursorPositionChanged() somewhere in a Qt Designer form class.

As for the problem "doesn't calculate with the typed time" you will have to explain the problem.
Where do you see the problem?
What input do you enter?
What output do you get?
Why is the wrong?

Reiji
2nd June 2015, 09:42
The warning is perfectly correct but this has nothing to do with the code you have posted. Somewhere in your program you are trying to connect() a QTimeEdit object with an invalid signal name. If you never called connect() directly then you probably have a function on_someTimeEdit_cursorPositionChanged() somewhere in a Qt Designer form class.

As for the problem "doesn't calculate with the typed time" you will have to explain the problem.
Where do you see the problem?
What input do you enter?
What output do you get?
Why is the wrong?

I juste changed the time by keyboard in an QTimeEdit elemet and it automatically calculated the new times for the other QTimeEdit elements and displayed them.
I realized hat created a slot for every QTimeEdit. for example the for the QTImeElement which displays the Time for the UK:


void WeltZeit::on_ENtimeEdit_timeChanged(const QTime &time)
{
QTime deTime = ui->ENtimeEdit->time().addSecs(3600); //adds an hour
ui->DEtimeEdit->setTime(deTime);
QTime nyTime = ui->ENtimeEdit->time().addSecs(-18000);
ui->NytimeEdit->setTime(nyTime);
if (DST == 1)
{
QTime jpTime = ui->ENtimeEdit->time().addSecs(28800);
ui->NytimeEdit->setTime(jpTime);
}
else
{
QTime jpTime = ui->ENtimeEdit->time().addSecs(32400);
ui->NytimeEdit->setTime(jpTime);
}

}

But that doesn't work anymore.
But strangely it works if I change the time via the QTimeEdit for new york(NytimeEdit) and the QTimeEdit for germany(DEtimeEdit).
Now I only get in QT the output that QTimeEdit::cursorPositionChanged() and nothing happens if I typed the new time.
I think maybe there is an problem with slots. Maybe(I can't remember) there is still the slot QTimeEdit::cursorPositionChanged() because I used it to try something. For me it changed to that problem just suddenly and since that it never changed and worked again.

I also set the current time into a QTimeElement of the current Time Zone via the Radio Button.
for example if I choose the radio Button New York the current time will be displayed in the QTimeElement CTtimeEdit and NyTimeEdit. I do that with the following code:

void WeltZeit::on_NewYorkButton_clicked()
{
ui->CTtimeEdit->setTime(ct);
ui->DEtimeEdit->setTime(ct);
....
}

The definiation of ct isn't defined in a void so I can acess it in every void(function).
The definiation of ct is:


QTime ct = QTime::currentTime();


But if I choose the radio button for Japan it's one hour behind and by choosing the button for London even 2 hours behind but I set the current time into the QTimeEdit elemts via the same piece of code(like in the NewYorkButton).