вот нужная часть :
C++ (Qt)
model1 = new QSqlTableModel;
proxyModel = new QSortFilterProxyModel(this);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("windows-1251"));
model1->setTable("peoples5");
model1->select();
ui->mytable->setModel(model1);
ui->mytable->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
row = model1->rowCount();
qDebug()<<row;
createContextMenu();
proxyModel->setSourceModel(model1);
ui->mytable->setModel(proxyModel);
QTextCodec::setCodecForTr(QTextCodec::codecForName("windows-1251"));
model1->setHeaderData(6, Qt::Horizontal,
tr("Женат/замужем"));
model1->setHeaderData(8, Qt::Horizontal,
tr("Дата рождения"));
view.setModel(model1);
QSqlQuery temp;
}
else qDebug()<<"Error in opening"<<db.lastError();
}
void MainWindow::createContextMenu()
{
ui->mytable->addAction(insertrow);
ui->mytable->addAction(deleterow);
}
MainWindow::~MainWindow()
{
delete ui;
}
//combobox delegate
ImageDelegate::ImageDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *ImageDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & /* option */,
const QModelIndex &index) const
{
QComboBox *comboBox = new QComboBox(parent);
if (index.column() == 1) {
comboBox->addItem(tr("Да"));
comboBox->addItem(tr("Нет"));
}
qDebug()<<index.column();
connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));
return comboBox;
}
void ImageDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
if (!comboBox)
return;
int pos = comboBox->findText(index.model()->data(index).toString(),
Qt::MatchExactly);
comboBox->setCurrentIndex(pos);
}
void ImageDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
if (!comboBox)
return;
model->setData(index, comboBox->currentText());
}
void ImageDelegate::emitCommitData()
{
emit commitData(qobject_cast<QWidget *>(sender()));
}
//end of delegate