Помогите разобраться..
Есть таблица заполняемая с базы даных... необходимо в таблице сделать столбик ComboBox с двумя возможными значениями..
вот таблица:
TableEditor::TableEditor(const QString &tableName, QWidget *parent)
: QDialog(parent)
{
model = new QSqlTableModel(this);
model->setTable(tableName);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Keyword"));
model->setHeaderData(2, Qt::Horizontal, tr("Path to file"));
model->setHeaderData(3,Qt::Horizontal,tr("Key_v"));
model->setHeaderData(3,Qt::Horizontal,tr("Path_v"));
model->setHeaderData(3,Qt::Horizontal,tr("Key_e"));
model->setHeaderData(3,Qt::Horizontal,tr("Path_e"));
//!
QTableView *view = new QTableView;
view->setModel(model);
// тут я пытаюсь вставить комбо бокс
YStdDelegate booldelegate(YStdDelegate::TBool);
tableView.setItemDelegateForColumn(0, &booldelegate);
submitButton = new QPushButton(tr("Submit"));
submitButton->setDefault(true);
revertButton = new QPushButton(tr("&Revert"));
quitButton = new QPushButton(tr("Quit"));
buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(submitButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(revertButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
//! [2]
//! [3]
connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
connect(revertButton, SIGNAL(clicked()), model, SLOT(revertAll()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
//! [3]
//! [4]
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(view);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("Cached Table"));
}
void TableEditor::submit()
{
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
} else {
model->database().rollback();
QMessageBox::warning(this, tr("Cached Table"),
tr("The database reported an error: %1")
.arg(model->lastError().text()));
}
}
А вот сама реализация класа делегирования :
YStdDelegate::YStdDelegate(TypeEditor type, QObject *parent)
: QItemDelegate(parent)
{
typeeditor = type;
}
QWidget *YStdDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &,
const QModelIndex &) const
{
QWidget *editor;
switch(typeeditor)
{
case TBool:
{
QComboBox *cmbox = new QComboBox(parent);
cmbox->addItem("0");
cmbox->addItem("1");
return static_cast<QWidget *>(cmbox);
}
}
return editor;
}
void YStdDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QVariant data = index.model()->data(index, Qt::DisplayRole);
switch(typeeditor)
{
case TBool:
{
QComboBox *cmbox = static_cast<QComboBox *>(editor);
if (data.toInt())
{
cmbox->setCurrentIndex(cmbox->findText("1"));
}
else
{
cmbox->setCurrentIndex(cmbox->findText("0"));
}
break;
}
}
}
void YStdDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QVariant data;
switch(typeeditor)
{
case TBool:
QComboBox *cmbox = static_cast<QComboBox *>(editor);
data = cmbox->currentText();
break;
}
model->setData(index, data, Qt::EditRole);
}
void YStdDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}