Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to split in DD HH MM SS from hours more then 24 hours



  • Hello everybody,
    with a mysql query I extract the sum of the hours of a work process and I want to display them in the format DD HH: MM: SS
    I am using this code which works fine within 24 hours.
    But for all processing beyond 24 QTime it fails

    QString query = QString("SELECT TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
      QSqlQuery query_time;
      query_time.prepare(query);
    
      if (query_time.exec())
        {
          while (query_time.next())
            {
              sec_work = sec_work + QTime(0, 0, 0).secsTo(query_time.value(0).toTime());
            }
          int dd = ?                            // my days (DD)
          int hh = sec_work/3600;               // my hours (HH)
          int mm = (sec_work%3600)/60;          // my minuts (MM)
          int ss = (sec_work%3600)%60;          // my seconds (SS)
        }
    

    How can I find the days (DD) if the hours exceed 24 ?
    Thank you in advance

    blackout69



  • Thanks for the suggestions.
    I solved it like this:

    // my query:
    QString query = QString("SELECT lastname, date, SUM(time_work) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
    
    // my custom delegate:
    void ColCustomTimeDelegato::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
      int value = index.model()->data(index, Qt::DisplayRole).toInt();
      QStyleOptionViewItem myOption = option;
      myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
      drawDisplay(painter, myOption, myOption.rect, QString("%1:%2:%3").arg(value/3600).arg((value%3600)/60).arg((value%3600)%60));
      drawFocus(painter, myOption, myOption.rect);
    }
    

    It works very well.
    Thanks again.



  • @blackout69 I am not sure if I understood your problem correctly but 24hrs is 86400 seconds. So, to get full days (Assuming no. of seconds is greater than 86400) would lead to int dd = sec_work/86400;



  • @blackout69 said in Hot to split in DD HH MM SS from hours more then 24 hours:

    But for all processing beyond 24 QTime it fails

    QTime represents a time-of-day, 0 to 24 hours. So you will have to deal with your "multiple days" part outside of a QTime.

    For the rest, as @artwaw says.



  • Hi Artur,
    yes of course this is correct. But my query returns a TIME value for example of 35:46:24 in HH: MM: SS and when I pass it to QTime it fails.



  • @blackout69 said in Hot to split in DD HH MM SS from hours more then 24 hours:

    35:46:24 in HH: MM: SS and when I pass it to QTime it fails.

    As I just wrote above....



  • sure, but my query returns a value of type TIME and I cannot do operations with QTime if the value exceeds 24 hours. Maybe I should turn it all into seconds and move on. But I don't know which method to use to do this.


  • Lifetime Qt Champion

    Hi,

    Why not just retrieve the sum in your query rather than making it a TIME type ?



  • Hi,
    for my application, I need to show the processing time on a QTableView.
    This is the code that shows the data I extract from the query .

    //My delegate...
    
    ColTimeDelegate::ColTimeDelegate(QObject *parent) : QItemDelegate(parent)
    {
    
    }
    
    void ColTimeDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
      QTime text = index.model()->data(index, Qt::DisplayRole).toTime();
      QStyleOptionViewItem myOption = option;
      myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
      drawDisplay(painter, myOption, myOption.rect, text.toString("hh:mm:ss"));
      drawFocus(painter, myOption, myOption.rect);
    }
    
    //My application code...
    
    QString query = QString("SELECT lastname, date, TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
    
    model = new QSqlQueryModel(this);
    model->setQuery(query);
    model->setHeaderData(0, Qt::Horizontal, tr("Employee"));
    model->setHeaderData(1, Qt::Horizontal, tr("Date"));
    model->setHeaderData(2, Qt::Horizontal, tr("Time"));   // Fail this field. The time is not shown on this field 
    
    // show
    ui->view->setModel(model);
    ui->view->resizeColumnsToContents();
    ui->view->horizontalHeader()->setStretchLastSection(true);
    ui->view->setItemDelegateForColumn(1, new ColDataDelegate(this));
    ui->view->setItemDelegateForColumn(2, new ColTimeDelegate(this));
    

    blackout69



  • @blackout69 I'd try to supply data as seconds and do the math in the delegate dropping QTime. The only thing to check is if this will be fast enough.



  • Thanks for the suggestions.
    I solved it like this:

    // my query:
    QString query = QString("SELECT lastname, date, SUM(time_work) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
    
    // my custom delegate:
    void ColCustomTimeDelegato::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
      int value = index.model()->data(index, Qt::DisplayRole).toInt();
      QStyleOptionViewItem myOption = option;
      myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
      drawDisplay(painter, myOption, myOption.rect, QString("%1:%2:%3").arg(value/3600).arg((value%3600)/60).arg((value%3600)%60));
      drawFocus(painter, myOption, myOption.rect);
    }
    

    It works very well.
    Thanks again.


Log in to reply