QSqlTableModel * mod2 = new QSqlTableModel(this,dbSql);mod2->setTable("Animals");mod2->select();ui->tableView_2->setModel(mod2);
mod2->insertRow(mod2->rowCount());
mod2->select(); if (mod2->submit()== false) { QMessageBox::critical(this,tr("Error"), tr(" Erorr ").append(tabM->lastError().text())); return; } mod2->insertRow(mod2->rowCount()); return;
mod2->submit();
(QSqlTableModel::OnFieldChange)
QWidget *LineDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &, const QModelIndex &index) const{ QDoubleValidator * dV = new QDoubleValidator(-999999999,99999999,2,parent); QWidget *editor; switch(typeeditor) { case TLine: { QLineEdit *lin = new QLineEdit(parent); lin->setReadOnly(true); return static_cast<QWidget *>(lin); } case TCombo: { QComboBox *cmbox = new QComboBox(parent); cmbox->addItem("true"); cmbox->addItem("false"); return static_cast<QWidget *>(cmbox); } case TLine_add: { QLineEdit *line = new QLineEdit(parent); line->setReadOnly(false); return static_cast<QWidget *>(line); } case TLine_html: { QLineEdit *line = new QLineEdit(parent); line->setReadOnly(false); return static_cast<QWidget *>(line); } case TLineInt: { QLineEdit *line = new QLineEdit(parent); line->setValidator(dV); line->setReadOnly(false); return static_cast<QWidget *>(line); } case TLine_regInt: { QLineEdit *line = new QLineEdit(parent); line->setValidator(dV); //line->setValidator(new QDoubleValidator(-99999999.0,999999999.0, 2,editor)); line->setReadOnly(false); return static_cast<QWidget *>(line); } case TLabel: { QLabel *line= new QLabel(parent); return static_cast<QWidget *>(line); } case ComboCl: { QVariant data = index.model()->data(index, Qt::DisplayRole); QComboBox *cbox = new QComboBox(parent); for (int i = 0 ; i < lC.size(); i++) { cbox->addItem(lC.at(i)); } cbox->addItem(data.toString()); cbox->view()->setCornerWidget(new QSizeGrip(cbox)); cbox->setAutoCompletion(true); qint32 width = 0; QFontMetrics fm(cbox->font()); for(int i = 0; i < cbox->count(); ++i) width = qMax(fm.width(cbox->itemText(i)), width); cbox->view()->setMinimumWidth(width + 10); return static_cast<QWidget *>(cbox); } case ComboCl2: { QVariant data = index.model()->data(index, Qt::DisplayRole); QComboBox *cbox = new QComboBox(parent); cbox->setEditable(true); for (int i = 0 ; i < lC.size(); i++) { cbox->addItem(lC.at(i)); } cbox->addItem(data.toString()); return static_cast<QWidget *>(cbox); } case Calendar: { QDateEdit *editor = new QDateEdit(parent); editor->setDisplayFormat("dd.MM.yyyy"); editor->setCalendarPopup(true); editor->setDate(QDate::currentDate()); return editor; } } return editor;}
void LineDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ QVariant data = index.model()->data(index, Qt::DisplayRole); switch(typeeditor) { case TLine: { QLineEdit *lin = static_cast<QLineEdit *>(editor); lin->setText(data.toString()); break; } case TCombo: { QComboBox *cmbox = static_cast<QComboBox *>(editor); if (data.toString()=="true") { cmbox->setCurrentIndex(cmbox->findText("true")); } else { cmbox->setCurrentIndex(cmbox->findText("false")); } break; } case TLine_add: { QLineEdit *line = static_cast<QLineEdit *>(editor); line->setText(data.toString()); break; } case TLine_html: { QLineEdit *line = static_cast<QLineEdit *>(editor); line->setText(data.toString()); break; } case TLineInt: { QLineEdit *line = static_cast<QLineEdit *>(editor); line->setText(data.toString()); break; } case TLine_regInt: { QLineEdit *line = static_cast<QLineEdit *>(editor); line->setText(data.toString()); break; } case TLabel: { QLabel *line= static_cast<QLabel *>(editor); line->setText(data.toString()); break; } case ComboCl: { QComboBox * cbox = static_cast < QComboBox *> (editor); QString text = data.toString(); int ig =0; for (int i = 0 ; i < cbox->count() ; i++) { QStringList op = cbox->itemText(i).split(" "); if (op.contains(text)) { ig = i; break; } } cbox->setCurrentIndex(ig); break; } case ComboCl2: { QComboBox * cbox = static_cast < QComboBox *> (editor); QString text = data.toString(); int t = cbox->findText(text); cbox->setCurrentIndex(t); break; } case Calendar: { QDateTimeEdit * date = static_cast < QDateTimeEdit *> (editor); date->setDate(QDate::fromString( index.model()->data(index, Qt::EditRole).toString(), "dd.MM.yyyy")); break; } }}
void LineDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QVariant data; switch(typeeditor) { case TLine: { QLineEdit *lin = static_cast<QLineEdit *>(editor); data = lin->text(); break; } case TCombo: { QComboBox *cmbox = static_cast<QComboBox *>(editor); if (cmbox->currentText()=="true") { cmbox->setCurrentIndex(cmbox->findText("true")); data = "true"; } else { cmbox->setCurrentIndex(cmbox->findText("false")); data = "false"; } break; } case TLine_add: { QLineEdit *line = static_cast<QLineEdit *>(editor); //line->setText(re); data = line->text(); break; } case TLine_html: { QLineEdit *line = static_cast<QLineEdit *>(editor); QString ret = line->text(); if(line->text().isEmpty())model->revert(); else { data = line->text(); break; } break; } case TLineInt: { QLineEdit *line = static_cast<QLineEdit *>(editor); //line->setText(re); data = line->text(); break; } case TLine_regInt: { QLineEdit *line = static_cast<QLineEdit *>(editor); QString ret = line->text(); if(line->text().isEmpty())model->revert(); else { data = line->text(); break; } break; } case TLabel: { QLabel *line= static_cast<QLabel *>(editor); data = line->text(); break; } case ComboCl: { QComboBox * cbox = static_cast < QComboBox *> (editor); int t = cbox->currentIndex(); QString str = cbox->itemText(t); QStringList ret = str.split(" "); str = ret.first(); if (str.isEmpty() == true) { break; } data = ret.first(); break; } case ComboCl2: { QComboBox * cbox = static_cast < QComboBox *> (editor); int t = cbox->currentIndex(); QString str = cbox->itemText(t); if (str.isEmpty() == true) { break; } data = str; break; } case Calendar: { QDateTimeEdit * date = static_cast < QDateTimeEdit *> (editor); QStringList ret = data.toString().split("."); data = date->date().toString("dd.MM.yyyy"); break; } } model->setData(index, data, Qt::EditRole);}
setEditStrategy(QSqlTableModel::OnFieldChange);