Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]QSqlTableModel converting time to unixtime and QDateTime
Forum Updated to NodeBB v4.3 + New Features

[solved]QSqlTableModel converting time to unixtime and QDateTime

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ras123
    wrote on 25 Jun 2012, 14:32 last edited by
    #1

    Hi,
    time is stored in the databse as unixtime, and it should be displayed as QDateTime with a format of dd/MM/yyyy. So the following function is used

    @QVariant MyModel::data(const QModelIndex &index, int role) const
    {
    QVariant value = QSqlRelationalTableModel::data(index, role);
    if (value.isValid() && role == Qt::DisplayRole)
    {
    if(index.column() == 1)
    {
    return QDateTime::fromTime_t(value.toInt());
    }
    }
    return value;
    }@

    it converts to QDateTime format, but when edited in the table how to convert it back to unixtime?
    The following delgate is used
    @WDelegate::WDelegate(QObject *parent)
    : QItemDelegate(parent)
    {
    }

    QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    if(index.column() == 1)
    {
    QDateTimeEdit *editor = new QDateTimeEdit(parent);
    editor->setDisplayFormat("dd/MM/yyyy");
    editor->setCalendarPopup(true);
    return editor;
    }
    }

    void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
    QLineEdit edit = qobject_cast<QLineEdit>(editor);
    if (edit)
    {
    edit->setText(index.model()->data(index, Qt::EditRole).toString());
    }
    else
    {
    QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
    if (dateEditor)
    {
    dateEditor->setDate(QDate::fromString(
    index.model()->data(index, Qt::EditRole).toString(),
    "dd/MM/yyyy"));
    }//if (dateEditor)
    }
    }

    void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
    if (edit)
    {
    model->setData(index, edit->text());
    }
    else
    {
    QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
    if (dateEditor)
    {
    model->setData(index, dateEditor->date().toString("dd/MM/yyyy"));
    }
    }
    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on 25 Jun 2012, 14:43 last edited by
      #2

      After you've got a new QDateTime, then you can use toTime_t() to get it back to unix time.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0

      1/2

      25 Jun 2012, 14:32

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved