Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

QSQLTableModel: delegate failed to update data

$
0
0
I’m currently using QSQLTableModel and QTableView with custom delegates. One delegate with a combobox and one with a dateedit but i can not successfully transfer changes made in the delegates to the sql table. one delegate: SQLDateDelegate.hpp #ifndef SQLDATE_DELEGATE_HPP__INCLUDED #define SQLDATE_DELEGATE_HPP__INCLUDED   #pragma once   #include <QtWidgets\QStyledItemDelegate>   class SQLDateDelegate : public QStyledItemDelegate {  Q_OBJECT   public:  SQLDateDelegate(QWidget* parent = NULL);    QWidget* createEditor(QWidget* parent, QStyleOptionViewItem const& option, QModelIndex const& index) const;  void setEditorData(QWidget* editor, QModelIndex const& index) const;  void setModelData(QWidget*editor, QAbstractItemModel* model, QModelIndex const& index) const;  void updateEditorGeometry(QWidget* editor, QStyleOptionViewItem const& option, QModelIndex const& index ) const;  void paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const; };   #endif // SQLDATE_DELEGATE_HPP__INCLUDED SQLDateDelegate.cpp #include "SQLDateDelegate.hpp" #include <QtWidgets\QDateTimeEdit> #include <QtWidgets\QApplication>   SQLDateDelegate::SQLDateDelegate(QWidget* parent)  : QStyledItemDelegate(parent) {}     QWidget* SQLDateDelegate::createEditor(QWidget* parent, QStyleOptionViewItem const& option, QModelIndex const& index) const {  QDateEdit* editor(new QDateEdit(parent));  editor->setCalendarPopup(true);  editor->setDisplayFormat("dd.MM.yyyy");  return editor; }   void SQLDateDelegate::setEditorData(QWidget* editor, QModelIndex const& index) const {  QDateEdit* edit = qobject_cast<QDateEdit*>(editor);  if (!edit) return;  edit->setDate(QDate::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate)); }   void SQLDateDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, QModelIndex const& index) const {  QDateEdit* edit = qobject_cast<QDateEdit*>(editor);  if (!edit) return;  // update to latest input  edit->interpretText();  model->setData(index, edit->date().toString(Qt::ISODate)/*, Qt::EditRole*/); }   void SQLDateDelegate::updateEditorGeometry(QWidget* editor, QStyleOptionViewItem const& option, QModelIndex const& index) const {  editor->setGeometry(option.rect); }   void SQLDateDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const {  QStyleOptionViewItemV4 display_option(option);  display_option.text = QDate::fromString(index.data().toString(), Qt::ISODate).toString("dd.MM.yyyy");  QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &display_option, painter); } assigning model to view QTableView& view (*m_userinterface.table); m_model = new QSqlTableModel(&view); m_model->setTable("dataset"); m_model->setEditStrategy(QSqlTableModel::OnFieldChange); m_model->select(); view.setModel(m_model); view.setItemDelegateForColumn(1, new SQLDateDelegate(&view)); Pressing [Enter] or changing row close the QDateEdit but the data is not transfered into the (dataset-)Table. After editing is finishied in addition the row is displayed empty (in all columns) I hope someone knows the solution? :) Sry for my bad english skills :(

Viewing all articles
Browse latest Browse all 13965

Trending Articles