Unsolved Saving edited date to db from QStandardItemModel
-
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.
-
QDateEdit *deditor = new QDateEdit(parent);
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
isn't
edit
alwaysNULL
?! did we miss something in between? what I mean is, shouldn't it beQDateEdit* 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 ussetItemDelegateForColumn
in the view -
@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? -
qfile is not a QWidget. If I understand correctly what you need really is a QItemEditorFactory
-
@VRonin
Can you please show me an example how to use it? -
@VRonin
I came up with the following structure instead ofif((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.