Actually I may have figured it out (after much debugging) although I have no idea if i'm doing it the "right" way. Is there a better way to do this?
Here is what I have done:
In the constructor to my window i create my model and assign it to my combobox. I then connect the dataChanged signal from the model, to a slot that submits the data to the database.
In QtDesigner I have these properties set on the combobox:
editable = true
autoCompletion = true
insertPolicy = QComboBox::InsertAtBottom
modelColumn = true
//Constructor
{
setupUi(this); //i'm using Qt Designer for my GUI design.
myModel->setTable("mydatabasetable");
myModel->select();
myComboBox->setModel(myModel);
myComboBox->setModelColumn(1); //collumn 0 is an auto ID, column 1 is text, which i want displayed
}
//DataChanged Slot
{
myModel->submitAll();
}
//Constructor
MyWindow::MyWindow(QWidget *parent) : QWidget(parent)
{
setupUi(this); //i'm using Qt Designer for my GUI design.
myModel = new QSqlTableModel(this);
myModel->setTable("mydatabasetable");
myModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
myModel->select();
myComboBox->setModel(myModel);
myComboBox->setModelColumn(1); //collumn 0 is an auto ID, column 1 is text, which i want displayed
connect(myModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(myModelDataChanged(const QModelIndex &, const QModelIndex &)));
}
//DataChanged Slot
void MyWindow::myModelDataChanged(const QModelIndex &, const QModelIndex &)
{
myModel->submitAll();
}
To copy to clipboard, switch view to plain text mode
I've tried different edit strategies and it seems that only OnManualSubmit works.
Bookmarks