Ok, now I think to be closer to a solution...
I've read this article http://doc.trolltech.com/qq/qq21-datawidgetmapper.html and the chapter "Using Delegates to Offer Choices" maybe will be useful!
According to the previous article I changed the code as following:
frmCategorie_AME.h
#ifndef FRMCATEGORIE_AME_H
#define FRMCATEGORIE_AME_H
#include <QtGui>
#include <QtSql>
#include "ui_frmCategorie_AME.h"
#include "ComboBoxDelegate.h"
class frmCategorie_AME
: public QDialog{
Q_OBJECT
public:
void setLabel
(const QString &text
);
private slots:
void revert();
void submit();
private:
Ui::frmCategorie_AME ui;
int id;
};
#endif // FRMCATEGORIE_AME_H
#ifndef FRMCATEGORIE_AME_H
#define FRMCATEGORIE_AME_H
#include <QtGui>
#include <QtSql>
#include "ui_frmCategorie_AME.h"
#include "ComboBoxDelegate.h"
class frmCategorie_AME : public QDialog
{
Q_OBJECT
public:
frmCategorie_AME(QSqlTableModel *model, const int &isItNew, QWidget *parent=0);
void setLabel(const QString &text);
private slots:
void revert();
void submit();
private:
Ui::frmCategorie_AME ui;
QDataWidgetMapper *mapper;
QSqlTableModel *modelAME;
int id;
};
#endif // FRMCATEGORIE_AME_H
To copy to clipboard, switch view to plain text mode
frmCategorie.cpp
#include "frmCategorie_AME.h"
/*----------------------------------------------------------------------------*/
frmCategorie_AME
::frmCategorie_AME(QSqlTableModel *model,
const int &isItNew,
{
ui.setupUi(this);
this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
this->setAttribute(Qt::WA_DeleteOnClose);
modelAME = model;
modelAME->database().transaction();
if (isItNew < 0)
{
id = modelAME->rowCount();
modelAME->insertRow(id);
}
else
{
id = isItNew;
}
items << tr("Green") << tr("Red");
ui.cboText->setModel(typeModel);
mapper->setModel(modelAME);
mapper->setItemDelegate(new ComboBoxDelegate(this));
mapper->addMapping(ui.lneDescrizione, 1);
mapper->addMapping(ui.cboText, 2);
mapper->setCurrentIndex(id);
connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
qDebug() << "frmCategorie_AME Loaded!";
}
/*----------------------------------------------------------------------------*/
void frmCategorie_AME
::setLabel(const QString &text
) {
qDebug() << "frmAME->setLabel";
ui.lblValore->setText(text.toUtf8());
}
/*----------------------------------------------------------------------------*/
void frmCategorie_AME::revert()
{
mapper->revert();
qDebug() << "->frmCategorie_AME (Aggiunta Rifiutata!";
modelAME->revertAll();
modelAME->database().rollback();
qDebug() << "frmAME closed!";
}
/*----------------------------------------------------------------------------*/
void frmCategorie_AME::submit()
{
if (mapper->submit())
{
mapper->setCurrentIndex(id);
qDebug() << "Ok submit!";
if (modelAME->submitAll())
{
modelAME->database().commit();
qDebug() << "Valore eliminato!";
}
else
{
modelAME->revertAll();
modelAME->database().rollback();
tr("Il database ha riportato un errore: %1")
.arg(modelAME->lastError().text()));
}
}
else
{
qDebug() << "No submit!";
tr("Il database ha riportato un errore: %1")
.arg(modelAME->lastError().text()));
}
}
#include "frmCategorie_AME.h"
/*----------------------------------------------------------------------------*/
frmCategorie_AME::frmCategorie_AME(QSqlTableModel *model, const int &isItNew,
QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);
this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
this->setAttribute(Qt::WA_DeleteOnClose);
modelAME = new QSqlTableModel(this);
modelAME = model;
modelAME->database().transaction();
if (isItNew < 0)
{
id = modelAME->rowCount();
modelAME->insertRow(id);
}
else
{
id = isItNew;
}
QStringList items;
QStringListModel *typeModel;
items << tr("Green") << tr("Red");
typeModel = new QStringListModel(items, this);
ui.cboText->setModel(typeModel);
mapper = new QDataWidgetMapper(this);
mapper->setModel(modelAME);
mapper->setItemDelegate(new ComboBoxDelegate(this));
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->addMapping(ui.lneDescrizione, 1);
mapper->addMapping(ui.cboText, 2);
mapper->setCurrentIndex(id);
connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
qDebug() << "frmCategorie_AME Loaded!";
}
/*----------------------------------------------------------------------------*/
void frmCategorie_AME::setLabel(const QString &text)
{
qDebug() << "frmAME->setLabel";
ui.lblValore->setText(text.toUtf8());
}
/*----------------------------------------------------------------------------*/
void frmCategorie_AME::revert()
{
mapper->revert();
qDebug() << "->frmCategorie_AME (Aggiunta Rifiutata!";
modelAME->revertAll();
modelAME->database().rollback();
qDebug() << "frmAME closed!";
}
/*----------------------------------------------------------------------------*/
void frmCategorie_AME::submit()
{
if (mapper->submit())
{
mapper->setCurrentIndex(id);
qDebug() << "Ok submit!";
if (modelAME->submitAll())
{
modelAME->database().commit();
qDebug() << "Valore eliminato!";
}
else
{
modelAME->revertAll();
modelAME->database().rollback();
QMessageBox::warning(this, tr("Attenzione!"),
tr("Il database ha riportato un errore: %1")
.arg(modelAME->lastError().text()));
}
}
else
{
qDebug() << "No submit!";
QMessageBox::warning(this, tr("Attenzione!"),
tr("Il database ha riportato un errore: %1")
.arg(modelAME->lastError().text()));
}
}
To copy to clipboard, switch view to plain text mode
ComboBoxDelegate.h
#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
#include <QItemDelegate>
{
Q_OBJECT
public:
ComboBoxDelegate
(QObject *parent
= 0);
};
#endif /* COMBOBOXDELEGATE_H */
#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
#include <QItemDelegate>
class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate(QObject *parent = 0);
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
};
#endif /* COMBOBOXDELEGATE_H */
To copy to clipboard, switch view to plain text mode
ComboBoxDelegate.cpp
#include <QtGui>
#include "ComboBoxDelegate.h"
/*----------------------------------------------------------------------------*/
{
}
/*----------------------------------------------------------------------------*/
{
if (!editor->metaObject()->userProperty().isValid())
{
if (editor->property("currentIndex").isValid())
{
editor->setProperty("currentIndex", index.data());
return;
}
}
}
/*----------------------------------------------------------------------------*/
{
if (!editor->metaObject()->userProperty().isValid())
{
QVariant value
= editor
->property
("currentIndex");
if (value.isValid())
{
model->setData(index, value);
return;
}
}
}
#include <QtGui>
#include "ComboBoxDelegate.h"
/*----------------------------------------------------------------------------*/
ComboBoxDelegate::ComboBoxDelegate(QObject *parent) : QItemDelegate(parent)
{
}
/*----------------------------------------------------------------------------*/
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (!editor->metaObject()->userProperty().isValid())
{
if (editor->property("currentIndex").isValid())
{
editor->setProperty("currentIndex", index.data());
return;
}
}
QItemDelegate::setEditorData(editor, index);
}
/*----------------------------------------------------------------------------*/
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if (!editor->metaObject()->userProperty().isValid())
{
QVariant value = editor->property("currentIndex");
if (value.isValid())
{
model->setData(index, value);
return;
}
}
QItemDelegate::setModelData(editor, model, index);
}
To copy to clipboard, switch view to plain text mode
but I'm havinga strange compiler error:
./debug\ComboBoxDelegate.o: In function `ZN16ComboBoxDelegateC2EP7QObject':
D:/DEV_QT/GESCON_STD/project/src/ComboBoxDelegate.cpp:21: undefined reference to `vtable for ComboBoxDelegate'
./debug\ComboBoxDelegate.o: In function `ZN16ComboBoxDelegateC1EP7QObject':
D:/DEV_QT/GESCON_STD/project/src/ComboBoxDelegate.cpp:21: undefined reference to `vtable for ComboBoxDelegate'
Unbeliveble!!!
Bookmarks