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. Changed date is not shown in QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

Changed date is not shown in QStandardItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.6k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I created an date editor in QDelegate CreateEditor like this:

                QDateEdit *deditor = new QDateEdit(parent);
                deditor->setDisplayFormat ("MM/dd/yyyy");
                deditor->setCalendarPopup (true);
                deditor->setStyleSheet ("background-color:rgb(255,217,229)");
                connect(deditor,&QDateEdit::editingFinished ,this,&myDelegate::commitAndCloseEditor );
    

    In setEditorData I have

        QDateEdit *dateEditor = qobject_cast<QDateEdit*>(editor);
        if(dateEditor)
            {
                dateEditor->setDate (index.model ()->data (index, Qt::EditRole).toDate ());
                QVariant d(index.data (Qt::DisplayRole));
                QDate fixdate;
                fixdate = d.toDate ();
                qDebug() << "fixdate: ";
                return;
            }
    

    In setModelData:

                QDateEdit *dateEdit = qobject_cast<QDateEdit*>(editor);
                QModelIndex updateIndex(index.model ()->index(index.row (),0,index.parent ()));
    
                QString fixID;
                QVariant v(updateIndex.data (Qt::DisplayRole));
                fixID = v.toString ();
                qDebug() << "FixID: " << fixID;
    
                model->setData (index,dateEdit->date ().toString ("MM/dd/YYYY"));
    

    I get the following message in output:
    FixID: "3"
    fixdate:
    When I change the date in the editor it changes the month and the day but writes YYYY as a year. What did I miss?
    Thank you.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Is it correct that in setEditorData , you use
      setDate (index.model ()->data (index, Qt::EditRole) <<< Edit role
      but for data, you use
      d(index.data (Qt::DisplayRole)); << Display role
      ?

      Just a thought.

      G 2 Replies Last reply
      1
      • mrjjM mrjj

        Hi
        Is it correct that in setEditorData , you use
        setDate (index.model ()->data (index, Qt::EditRole) <<< Edit role
        but for data, you use
        d(index.data (Qt::DisplayRole)); << Display role
        ?

        Just a thought.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Hi @mrjj
        I changed it to

             dateEditor->setDate (index.model ()->data (index, Qt::EditRole).toDate ());
                    QVariant d(index.data (Qt::EditRole));
        

        but the same thing happens.

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Is it correct that in setEditorData , you use
          setDate (index.model ()->data (index, Qt::EditRole) <<< Edit role
          but for data, you use
          d(index.data (Qt::DisplayRole)); << Display role
          ?

          Just a thought.

          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          @mrjj
          Even the QVariant is missing the year:

          QVariant:  QVariant(QString, "01/01/YYYY")
          
          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You mean its truncated and only the last / is there but no "YYYY" or
            what is missing?
            If you use pure string ( not as date) does it work then?
            Im not sure if save/load data that is wrong og some convert to/from string with datatime.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Your format is wrong: it's yyyy in lower case.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                You should actually store the date as a QDate rather than a string and let displayText() in the delegate to take care to display it in the correct format

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1
                • G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #8

                  I will change it from text to date. Thank you.

                  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