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. Saving edited date to db from QStandardItemModel
Forum Update on Monday, May 27th 2025

Saving edited date to db from QStandardItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.4k Views
  • 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 have a QStandardItemModel with QTableView. One of the field is a date field which is originally a QString.
    I use the following code for editing:

    QWidget *myDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        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 );
        return deditor;
    }
    
    void myDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
    
     if(edit)
            {
                if((index.column ()) == 7)//Adoption date
                    {
                        model->setData (index,edit->text ());
                        QVariant fd(index.data (Qt::DisplayRole));
                        QString dateFix;
                        dateFix = fd.toString ();
                        qDebug() << "dateFix: " << dateFix;
                    }
            }
    }
    

    The edited date registers to the model and displayed correctly in the appropriate field. As my db stores the date as 3 strings (month, day, year) I need to break up the date string. The original date is stored as a string in the db and displayed as a string in the tableview. That string is converted into a QDate which is saved back to the model and displayed I guess as a string. When I try to see the value in dafeFix there is nothing displayed. I'm sure it is the date conversion that goes wrong somewhere. Please help me to find where it goes wrong.

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

      QDateEdit *deditor = new QDateEdit(parent);

      QLineEdit *edit = qobject_cast<QLineEdit*>(editor);

      isn't edit always NULL?! did we miss something in between? what I mean is, shouldn't it be QDateEdit* edit = qobject_cast<QDateEdit*>(editor);?

      Also
      this: (index.column ()) == 7 is bad design. the delegate should not depend on the model structure. if you want it to apply only to 1 column us setItemDelegateForColumn in the view

      "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

      G 1 Reply Last reply
      2
      • VRoninV VRonin

        QDateEdit *deditor = new QDateEdit(parent);

        QLineEdit *edit = qobject_cast<QLineEdit*>(editor);

        isn't edit always NULL?! did we miss something in between? what I mean is, shouldn't it be QDateEdit* edit = qobject_cast<QDateEdit*>(editor);?

        Also
        this: (index.column ()) == 7 is bad design. the delegate should not depend on the model structure. if you want it to apply only to 1 column us setItemDelegateForColumn in the view

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

        @VRonin
        Hi, thank you for your notes. I actually need to use 4 editors: lineedit, textedit, qfile and qdateedit. Is it still the best to use the delegate to use the editor with a specific column?

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

          qfile is not a QWidget. If I understand correctly what you need really is a QItemEditorFactory

          "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

          G 2 Replies Last reply
          2
          • VRoninV VRonin

            qfile is not a QWidget. If I understand correctly what you need really is a QItemEditorFactory

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

            @VRonin
            Can you please show me an example how to use it?

            1 Reply Last reply
            0
            • VRoninV VRonin

              qfile is not a QWidget. If I understand correctly what you need really is a QItemEditorFactory

              G Offline
              G Offline
              gabor53
              wrote on last edited by VRonin
              #6

              @VRonin
              I came up with the following structure instead of

               if((index.column ()) == 1)
              
              void myDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
              {
                  QSqlQuery fixquery;
              
                  int colset = index.column ();
              
                  switch(colset)
                      {
                      case 1:
                      {
                          QLineEdit *edit = qobject_cast<QLineEdit*>(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,edit->text ());
                          QVariant f(index.data(Qt::DisplayRole));
                          QString modified;
                          modified = f.toString ();
                          qDebug() << "Modified: " << modified;
              
                          fixquery.prepare("UPDATE Items SET Name = :Name WHERE ID = :fixID");
                          fixquery.bindValue (":Name", modified);
                          fixquery.bindValue (":fixID",fixID);
                          fixquery.exec ();
                      }
                       [[clang::fallthrough]]; case 2:
                      {
                          QLineEdit *edit = qobject_cast<QLineEdit*>(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,edit->text ());
                          QVariant w(index.data (Qt::DisplayRole));
                          QString fixedWhat;
                          fixedWhat = w.toString ();
                          fixquery.prepare ("UPDATE Items SET What = :What WHERE ID = :fixID ");
                          fixquery.bindValue (":What",fixedWhat);
                          fixquery.bindValue (":fixID",fixID);
                          fixquery.exec ();
                      }
                      }
              

              Is it better? It seems to be working.

              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