Ikky
Ikky

Reputation: 2856

Qt: How to set a date with time, to a QDateTimeEdit?

I'm getting dates formatted like this: "2011-04-14 15:00:00", and want to set them to a QDateTimeEdit object.

This is what i do:

void MainWindow::setUIDateAndTime(QString &date)
{
    QDateTime dateTime;
    dateTime.fromString(date, "yyyy-MM-dd hh:mm:ss");

    QString sDatetime = dateTime.toString("yyyy-MM-dd hh:mm:ss"); // For debug testing

    // Create the datetime picker
    QDateTimeEdit *dateTimePicker = new QDateTimeEdit(dateTime);
    dateTimePicker->setObjectName("dateTimePicker");

    ui->frameCommentHolderLayout->addWidget(dateTimePicker);
}

But it never get's set. The debug variable "sDateTime" is always set to "".

Does Anybody see what i do wrong? Thanks in advance.

Upvotes: 0

Views: 12210

Answers (1)

beduin
beduin

Reputation: 8253

fromString function is static in QDateTime class. That means it doesn't modify state of your dateTime object. Try this:

QDateTime dateTime(QDateTime::fromString(date, "yyyy-MM-dd hh:mm:ss"));

Upvotes: 4

Related Questions