#include "comboboxdelegate.h"
#include <QMessageBox>
comboBoxDelegate
::comboBoxDelegate(QObject *parent
) : QStyledItemDelegate(parent)
{
m_db->setHostName("localhost");
m_db->setDatabaseName("gestionstockDB.db");
m_db->setPassword("");
m_db->setUserName("");
if(m_db->open())
m_query
= new QSqlQuery("select designation from Produit") ;
}
{ if(!index.isValid())
return QStyledItemDelegate::createEditor(parent,option,index);
if(index.column() == 1)
{
if(m_db->open())
{
editor->setEditable(true);
while(m_query->next())
editor->addItem(m_query->value(0).toString());
return editor;
}else{
QMessageBox::critical(parent,
"Erreur Connexion ",
"Erreur, lors de la connexion au base de donnée");
return QStyledItemDelegate::createEditor(parent,option,index);
}
}else
return QStyledItemDelegate::createEditor(parent,option,index);
}
{
QVariant value
= index.
model()->data
(index,Qt
::EditRole) ;
QComboBox *comboBox
= qobject_cast<QComboBox
*>
(editor
);
comboBox->setCurrentIndex(comboBox->findText(value.toString()));
}
{
QComboBox *comboBox
= qobject_cast<QComboBox
*>
(editor
);
model->setData(index,comboBox->currentText(),Qt::EditRole);
}
{
editor->setGeometry(option.rect);
}
#include "comboboxdelegate.h"
#include <QMessageBox>
comboBoxDelegate::comboBoxDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
m_db = new QSqlDatabase;
*m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db->setHostName("localhost");
m_db->setDatabaseName("gestionstockDB.db");
m_db->setPassword("");
m_db->setUserName("");
if(m_db->open())
m_query = new QSqlQuery("select designation from Produit") ;
}
QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ if(!index.isValid())
return QStyledItemDelegate::createEditor(parent,option,index);
if(index.column() == 1)
{
if(m_db->open())
{
QComboBox *editor = new QComboBox(parent);
editor->setEditable(true);
while(m_query->next())
editor->addItem(m_query->value(0).toString());
return editor;
}else{
QMessageBox::critical(parent,"Erreur Connexion ","Erreur, lors de la connexion au base de donnée");
return QStyledItemDelegate::createEditor(parent,option,index);
}
}else
return QStyledItemDelegate::createEditor(parent,option,index);
}
void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QVariant value = index.model()->data(index,Qt::EditRole) ;
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
comboBox->setCurrentIndex(comboBox->findText(value.toString()));
}
void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
model->setData(index,comboBox->currentText(),Qt::EditRole);
}
void comboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
To copy to clipboard, switch view to plain text mode
Bookmarks