PDA

View Full Version : QTableView, QSqlQueryModel and delegates



estanisgeyer
29th January 2008, 11:47
Good day friends!

I'm trying to customize a QTableView through QItemDelegate. Trying debug noticed that my class MyDelegate is not being processed. I ask: Is it possible to delegate with QSqlQueryModel? What may be occurring's wrong? Follow my code:



void CadComissoes::getUsuarios()
{
OpenDB();
QSqlQuery qry;
QString SQL;

model = new QSqlQueryModel();

SQL = "SELECT COD_USER, NAME, COD_GROUP ";
SQL+= "FROM USERS";

qry.prepare(SQL);
qry.exec();

model->setQuery(qry);
ui.tableView_ComissaoUsuarios->setModel(model);

MyDelegate *iDelegate = new MyDelegate(this);
ui.tableView_ComissaoUsuarios->setItemDelegate(iDelegate);
CloseDB();
}

MyDelegate::MyDelegate(QObject *parent)
:QItemDelegate(parent)
{

}

QWidget *MyDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (!index.isValid())
{
qDebug() << "Not Valid index";
}
if (index.column() == 2)
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
}

void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::DisplayRole).toInt();

QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}

void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();

model->setData(index, value, Qt::EditRole);
}


Thanks,

Marcelo E. Geyer

estanisgeyer
29th January 2008, 12:04
I found the problem, I gave to that QSqlQueryModel is only for reading. So QSqlTableModel used. Thanks.