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. Losing seconds precision when displaying datetime with postgresql
Forum Updated to NodeBB v4.3 + New Features

Losing seconds precision when displaying datetime with postgresql

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 379 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello!

    I have a database with postgresql. I have a table that contains a timestamp column. When I do a query with pgAdmin, it shows the datetime correctly:

    ba91427e-1a13-48cf-9f3e-fe97bd8eca47-image.png

    But, when I show the result in a QTableView in Qt, I lose the seconds:

    7741cb9e-e167-4ead-a274-38143358c916-image.png

    This is my code:

    QString sROIsQuery = QString("roiID LIKE '\%%1\%'").arg(m_slDataROIs.at(0));
        if (m_slDataROIs.size() > 1) {
            for (int i = 1; i < m_slDataROIs.size(); i++)
                sROIsQuery.append(QString("OR roiID LIKE '\%%1\%'").arg(m_slDataROIs.at(i)));
        }
    
        QString sDataQuery = QString("dataCode='%1'").arg(m_slData.at(0));
        if (m_slData.size() > 1) {
            for (int i = 1; i < m_slData.size(); i++)
                sDataQuery.append(QString("OR dataCode='%1'").arg(m_slData.at(i)));
        }
    
        QString sCamerasQuery = QString("cameraID='%1'").arg(m_slDataCameras.at(0).split(" ")[1]);
        if (m_slDataCameras.size() > 1) {
            for (int i = 1; i< m_slAlarmsCameras.size(); i++)
                sCamerasQuery.append(QString("OR cameraID='%1'").arg(m_slDataCameras.at(i).split(" ")[1]));
        }
    
        QString sPresetsQuery = QString("presetID='%1'").arg(m_slDataPresets.at(0).split(" ").last());
        if (m_slDataPresets.size() > 1) {
            for (int i = 1; i < m_slDataPresets.size(); i++)
                sPresetsQuery.append(QString("OR presetID='%1'").arg(m_slDataPresets.at(i).split(" ").last()));
        }
    
        QString sDateFromQuery = QString("dataDateTime>='%1'").arg(dtDataFrom.toString("yyyy-MM-dd hh:mm:ss"));
        QString sDateToQuery = QString(" AND dataDateTime<='%1'").arg(dtDataTo.toString("yyyy-MM-dd hh:mm:ss"));
        QString sDateQuery = sDateFromQuery + sDateToQuery;
    
        QString sQuery = QString("SELECT dataCode, cameraSN, cameraID, presetID, roiID, dataDateTime, description, data FROM Data "
            "WHERE (%1) AND (%2) AND (%3) AND (%4) AND (%5) ORDER BY dataDateTime DESC;").arg(sROIsQuery).arg(sDataQuery).arg(sCamerasQuery).arg(sPresetsQuery).arg(sDateQuery);
    
    m_pDataQueryModel->setQuery(*query);
    
        ui->tableData->setModel(m_pDataQueryModel);
    //    ui->tableData->horizontalHeader()->setStretchLastSection(true);
        ui->tableData->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(6, QHeaderView::Stretch);
        ui->tableData->horizontalHeader()->setSectionResizeMode(7, QHeaderView::Stretch);
    
    

    What could be happening?

    Thank you very much!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #2

      Create a custom QStyledItemDelegate and reimplement the displayText() method. Check the QVariant::type() of the value parameter and if it's of type QDateTime, do your custom formatting otherwise, call the default implementation of the displayText(). I haven't tested this code but it will be something like this:

      QString CustomDateTimeDelegate::displayText(const QVariant &value, const QLocale &locale){
          if (value.type() == QMetaType::QDateTime){
                 return value.toDateTime().toString("yyyy-MM-dd hh:mm:ss");
          } else{
                 return QStyledItemDelegate::displayText(value, locale);
         }
      }
      ivanicyI 1 Reply Last reply
      7
      • M mchinand

        Create a custom QStyledItemDelegate and reimplement the displayText() method. Check the QVariant::type() of the value parameter and if it's of type QDateTime, do your custom formatting otherwise, call the default implementation of the displayText(). I haven't tested this code but it will be something like this:

        QString CustomDateTimeDelegate::displayText(const QVariant &value, const QLocale &locale){
            if (value.type() == QMetaType::QDateTime){
                   return value.toDateTime().toString("yyyy-MM-dd hh:mm:ss");
            } else{
                   return QStyledItemDelegate::displayText(value, locale);
           }
        }
        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        @mchinand Thank you very much!!

        1 Reply Last reply
        0

        • Login

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