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. QTableView makes a mistake on Qtime
Forum Updated to NodeBB v4.3 + New Features

QTableView makes a mistake on Qtime

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.2k 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.
  • paesP Offline
    paesP Offline
    paes
    wrote on last edited by paes
    #1

    Hi.
    I have a problem when I try to show The total time in a field of QTableView.
    total time is 08:24
    but

    //your code here
    ```modelRecord->setData(modelRecord->index(c,1),entry.toString("hh:mm")); // is 08:25
    and
    if(ui->lineEdit->text().isEmpty()) ui->lineEdit->setText(entry.toString("hh:mm"));// is 08:24
    

    I am using the native classes of Qt

    1 Reply Last reply
    0
    • devDawgD Offline
      devDawgD Offline
      devDawg
      wrote on last edited by
      #2

      Hello,

      I'm afraid you've got to show some more context here. It's very difficult to even have an idea of what you're going for here.

      Think about what you want, think about what is stopping you, & post the related code.

      Cheers

      Anything worthwhile is never achieved easily.

      1 Reply Last reply
      0
      • paesP Offline
        paesP Offline
        paes
        wrote on last edited by paes
        #3

        I am using QTableView to show a DataBase table via model and one of the table columns has a timestamp. But that column doesn't show the same data as it was stored in The DataBase:
        for example
        my column timestamp in The database stored is 2018-06-01 08:24:06.35
        but the table column of QTableView is 2018-06-01T08:25:05

        //my code here
        ```modelRecord = new QStandardItemModel;
        modelRecord->setHorizontalHeaderLabels(headers);
        ui->treeViewAttedance->setModel(modelRecord);
        
        modelRecord->insertRow(c);
        modelRecord->setData(modelRecord->index(c,1),recordAtt.value("_date").toDateTime().toString(Qt::ISODate));
        modelRecord->submit();
        JonBJ 1 Reply Last reply
        0
        • paesP paes

          I am using QTableView to show a DataBase table via model and one of the table columns has a timestamp. But that column doesn't show the same data as it was stored in The DataBase:
          for example
          my column timestamp in The database stored is 2018-06-01 08:24:06.35
          but the table column of QTableView is 2018-06-01T08:25:05

          //my code here
          ```modelRecord = new QStandardItemModel;
          modelRecord->setHorizontalHeaderLabels(headers);
          ui->treeViewAttedance->setModel(modelRecord);
          
          modelRecord->insertRow(c);
          modelRecord->setData(modelRecord->index(c,1),recordAtt.value("_date").toDateTime().toString(Qt::ISODate));
          modelRecord->submit();
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @paes
          QTableView just displays what is in your data model. You are storing a string, so QTableView is hardly going to be changing the value to some other second, so something else is going on here, e.g. the database is not storing the value you think it is.

          Try something like (you may have to adjust for the QVariant returned by modelRecord->data(), I don't know):

          CString ds = recordAtt.value("_date").toDateTime().toString(Qt::ISODate));
          qDebug() << ds;
          modelRecord->setData(modelRecord->index(c,1), ds);
          ds = modelRecord->data(modelRecord->index(c,1));
          qDebug() << ds;
          modelRecord->submit();
          // You *may* need to refresh the model from the database here after `submit()`
          // You say your data goes to a "database"
          // but you are using `QStandardItemModel` so I don't know how you're doing that
          // Assuming your `submit()` does update the database, you could also look there
          // to see what it has *actually* stored for the value
          ds = modelRecord->data(modelRecord->index(c,1));
          qDebug() << ds;
          
          1 Reply Last reply
          3
          • paesP Offline
            paesP Offline
            paes
            wrote on last edited by paes
            #5

            @JonB @devDawg
            I am not sending data to the database, but I am sending data to TableView via QStandardItemModel and QSqlRelationalTableModel; however, I am adding some columns that my model doesn't have:

            modelAttendace = new QSqlRelationalTableModel(this);   modelAttendace->setRelation(modelAttendace->fieldIndex("id_pers"),QSqlRelation("peopleac","id","lastname"));
             modelAttendace->setTable("attendance");
            ui->treeViewAttedance->setModel(modelRecord);
            
            headers << "Hr. Timein"<< "Hr. Timeout" << "Hrs. Worked" << "Hrs. Delay" << "Day" << "Date"<< "Event";
                modelRecord = new QStandardItemModel;
                modelRecord->setHorizontalHeaderLabels(headers);
            
             modelAttendace->setFilter(QString("_date >='%1' and _date <= '%2' and id_pers = %3 order by id_pers, _date, _inside")
                                              .arg(begin.toString(Qt::ISODate))
                                              .arg(aux.toString(Qt::ISODate))
                                              .arg(id));
            modelAttendace->select();
            while(true){
            QSqlRecord recordAtt = modelAttendace->record(i);
            modelRecord->insertRow(c);
            QVariant ds = recordAtt.value("_date");
               qDebug()<< "after : "<< ds; //after: QVariant(QDateTime, QDateTime(2018-06-01 08:24:06.351
             modelRecord->setData(modelRecord->index(c,1),ds);
            ds = modelRecord->data(modelRecord->index(c,1));
              qDebug()<< "before : "<< ds;//before: QVariant(QDateTime, QDateTime(2018-06-01 08:24:06.351
            modelRecord->submit();
            ds = modelRecord->data(modelRecord->index(c,1));//2018-06-01T08:24:06
              qDebug()<<"after submit "<< ds; //after submit  QVariant(QDateTime, QDateTime(2018-06-01 08:24:06.351
            }
            

            Sorry for what I explained, the mistake that I pointed out is not on QStandardItemModel so that it's on TableView gui!

            I tried QTreeView and QTableView
            ----------------------------------------------------
            "Hrs. Delay"  - "Day" -             "Date"                        
               ----          ----      01/06/2018 08:25 // A. M.
            
            JonBJ 1 Reply Last reply
            0
            • paesP paes

              @JonB @devDawg
              I am not sending data to the database, but I am sending data to TableView via QStandardItemModel and QSqlRelationalTableModel; however, I am adding some columns that my model doesn't have:

              modelAttendace = new QSqlRelationalTableModel(this);   modelAttendace->setRelation(modelAttendace->fieldIndex("id_pers"),QSqlRelation("peopleac","id","lastname"));
               modelAttendace->setTable("attendance");
              ui->treeViewAttedance->setModel(modelRecord);
              
              headers << "Hr. Timein"<< "Hr. Timeout" << "Hrs. Worked" << "Hrs. Delay" << "Day" << "Date"<< "Event";
                  modelRecord = new QStandardItemModel;
                  modelRecord->setHorizontalHeaderLabels(headers);
              
               modelAttendace->setFilter(QString("_date >='%1' and _date <= '%2' and id_pers = %3 order by id_pers, _date, _inside")
                                                .arg(begin.toString(Qt::ISODate))
                                                .arg(aux.toString(Qt::ISODate))
                                                .arg(id));
              modelAttendace->select();
              while(true){
              QSqlRecord recordAtt = modelAttendace->record(i);
              modelRecord->insertRow(c);
              QVariant ds = recordAtt.value("_date");
                 qDebug()<< "after : "<< ds; //after: QVariant(QDateTime, QDateTime(2018-06-01 08:24:06.351
               modelRecord->setData(modelRecord->index(c,1),ds);
              ds = modelRecord->data(modelRecord->index(c,1));
                qDebug()<< "before : "<< ds;//before: QVariant(QDateTime, QDateTime(2018-06-01 08:24:06.351
              modelRecord->submit();
              ds = modelRecord->data(modelRecord->index(c,1));//2018-06-01T08:24:06
                qDebug()<<"after submit "<< ds; //after submit  QVariant(QDateTime, QDateTime(2018-06-01 08:24:06.351
              }
              

              Sorry for what I explained, the mistake that I pointed out is not on QStandardItemModel so that it's on TableView gui!

              I tried QTreeView and QTableView
              ----------------------------------------------------
              "Hrs. Delay"  - "Day" -             "Date"                        
                 ----          ----      01/06/2018 08:25 // A. M.
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @paes
              Rather different from the original code you presented... In particular, you're not passing around strings, which your original code showed you were...

              Purely at a guess, it's a minute-rounding issue? You could have told us this: is the displayed value always the next minute rounded up from the value you actually store? If so, you need to look at the default way a QVariant(QDateTime)) is converted to a string during QTableView, it probably just does some kind of standard "toString()" on the result of modelRecord->data(index, Qt::DisplayRole), and you may have to override that to return the string representation of the datetime which you actually desire. E.g. https://stackoverflow.com/questions/40494687/format-date-time-value-shown-by-a-qtableview gives you some thoughts. Or you can go down the QStyledItemDelegate route.

              1 Reply Last reply
              2
              • paesP Offline
                paesP Offline
                paes
                wrote on last edited by
                #7

                @JonB
                Yes, it's a minute-rounding issue!.

                QString ds = recordAtt.value("_date").toDateTime().toString(Qt::ISODate));
                qDebug() << ds; // 2018-06-01 08:24:06.351
                
                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