Hi to all,
I'm using a QDialog to edit data from QSqlTableModel through QDataWidgetMapper. Now I've a record field integer that can take only five values but I need to code in text those values and use a QComboBox to do it. So I've thought to implement a QItemDelegate following the example demos/spreadsheet/main.cpp that came with QAssistant but it doesn't work 
The attachment is a Snapshot about the Qdialog.
The following code to attempt an explanation :
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:
private slots:
void revert();
void submit();
private:
Ui::frmCategorie_AME ui;
int id;
ComboBoxDelegate boxEU;
};
#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);
private slots:
void revert();
void submit();
private:
Ui::frmCategorie_AME ui;
QDataWidgetMapper *mapper;
QSqlTableModel *modelAME;
int id;
ComboBoxDelegate boxEU;
};
#endif // FRMCATEGORIE_AME_H
To copy to clipboard, switch view to plain text mode
frmCategorie_AME.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;
}
mapper->setModel(modelAME);
mapper->addMapping(ui.lneDescrizione, 1);
mapper->addMapping(ui.cboText, 2);
mapper->setCurrentIndex(id);
ui.cboText->setItemDelegate(&boxEU);
connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
qDebug() << "frmCategorie_AME Loaded!";
}
/*----------------------------------------------------------------------------*/
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;
}
mapper = new QDataWidgetMapper(this);
mapper->setModel(modelAME);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->addMapping(ui.lneDescrizione, 1);
mapper->addMapping(ui.cboText, 2);
mapper->setCurrentIndex(id);
ui.cboText->setItemDelegate(&boxEU);
connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
qDebug() << "frmCategorie_AME Loaded!";
}
/*----------------------------------------------------------------------------*/
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 <QtGui>
{
public:
ComboBoxDelegate
(QObject *parent
= 0);
private slots:
void commitAndCloseEditor();
};
#endif /* COMBOBOXDELEGATE_H */
#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
#include <QtGui>
class ComboBoxDelegate: public QItemDelegate
{
public:
ComboBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
private slots:
void commitAndCloseEditor();
};
#endif /* COMBOBOXDELEGATE_H */
To copy to clipboard, switch view to plain text mode
ComboBoxDelegate.cpp
#include "ComboBoxDelegate.h"
/*----------------------------------------------------------------------------*/
ComboBoxDelegate
::ComboBoxDelegate(QObject *parent
){
qDebug() << "ComboBoxDelegate Costruttore";
}
/*----------------------------------------------------------------------------*/
const QStyleOptionViewItem
&,
const QModelIndex &index
) const {
editor->addItem("Green", 0);
editor->addItem("Red", 1);
editor->addItem("Yellow", 2);
editor->addItem("Blue", 3);
editor->addItem("Magenta", 4);
editor->setCurrentIndex(0);
return editor;
}
/*----------------------------------------------------------------------------*/
void ComboBoxDelegate::commitAndCloseEditor()
{
QComboBox *editor
= qobject_cast<QComboBox
*>
(sender
());
emit commitData(editor);
emit closeEditor(editor);
}
/*----------------------------------------------------------------------------*/
{
QComboBox *edit
= qobject_cast<QComboBox
*>
(editor
);
int value = index.model()->data(index, Qt::EditRole).toInt();
edit->setCurrentIndex(value);
}
/*----------------------------------------------------------------------------*/
{
QComboBox *edit
= qobject_cast<QComboBox
*>
(editor
);
model->setData(index, edit->itemData(edit->currentIndex()));
}
#include "ComboBoxDelegate.h"
/*----------------------------------------------------------------------------*/
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
qDebug() << "ComboBoxDelegate Costruttore";
}
/*----------------------------------------------------------------------------*/
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem&, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("Green", 0);
editor->addItem("Red", 1);
editor->addItem("Yellow", 2);
editor->addItem("Blue", 3);
editor->addItem("Magenta", 4);
editor->setCurrentIndex(0);
return editor;
}
/*----------------------------------------------------------------------------*/
void ComboBoxDelegate::commitAndCloseEditor()
{
QComboBox *editor = qobject_cast<QComboBox *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
/*----------------------------------------------------------------------------*/
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox *edit = qobject_cast<QComboBox *>(editor);
int value = index.model()->data(index, Qt::EditRole).toInt();
edit->setCurrentIndex(value);
}
/*----------------------------------------------------------------------------*/
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *edit = qobject_cast<QComboBox *>(editor);
model->setData(index, edit->itemData(edit->currentIndex()));
}
To copy to clipboard, switch view to plain text mode
Thanks in advance!
Bookmarks