Qt-interest Archive, June 2007
editable QComboBox using a QSqlQueryModel?
Message 1 in thread
Hi,
I have manually populated an editable QComboBox using the data from a
database. Then manually inserted into the db when a new item was added. Is it
possible to setup an editable QComboBox with a custom model derived from
QSqlQueryModel? When the user adds an item to the combo box, how do I insert
a new record into the db using the model?
Thanks,
Tom
--
[ signature omitted ]
Message 2 in thread
On Thursday 14 June 2007 11:19, Tom Brown wrote:
> Hi,
>
> I have manually populated an editable QComboBox using the data from a
> database. Then manually inserted into the db when a new item was added. Is
> it possible to setup an editable QComboBox with a custom model derived from
> QSqlQueryModel? When the user adds an item to the combo box, how do I
> insert a new record into the db using the model?
Ok, so I figured out how to create an editable QComboBox using a custom model
derived from QSqlQueryModel. I had to implement the insertRows() method and
just return True so that my setData() method would be called.
Now I need the value of the first column, which is the id for the record. How
do I go about getting the value of the first column?
My custom model is given below. It is written in Python.
Thanks,
Tom
class ECBQueryModel(QSqlQueryModel):
def __init__(self, sql, db):
QSqlQueryModel.__init__(self)
self.sql = sql
self.db = db
self.refresh()
def insertRecord(self, value):
sql = 'insert into model (model) values (%r)' % value
query = QSqlQuery(self.db)
return query.exec_(sql)
def insertRows(self, row, count, parent):
return True
def refresh(self):
self.setQuery(self.sql, self.db)
def setData(self, index, value, role):
value = str(value.toString())
ok = self.insertRecord(value)
if ok:
self.clear()
self.refresh()
return ok
--
[ signature omitted ]
Message 3 in thread
On Thursday 14 June 2007 17:52, Tom Brown wrote:
> On Thursday 14 June 2007 11:19, Tom Brown wrote:
> > Hi,
> >
> > I have manually populated an editable QComboBox using the data from a
> > database. Then manually inserted into the db when a new item was added.
> > Is it possible to setup an editable QComboBox with a custom model derived
> > from QSqlQueryModel? When the user adds an item to the combo box, how do
> > I insert a new record into the db using the model?
>
> Ok, so I figured out how to create an editable QComboBox using a custom
> model derived from QSqlQueryModel. I had to implement the insertRows()
> method and just return True so that my setData() method would be called.
>
> Now I need the value of the first column, which is the id for the record.
> How do I go about getting the value of the first column?
I figured out how to get the id through the model. My updated custom model is
below. Note the getID() method. Any other approaches would be welcome.
Thanks,
Tom
class ECBQueryModel(QSqlQueryModel):
def __init__(self, sql, db):
QSqlQueryModel.__init__(self)
self.sql = sql
self.db = db
self.refresh()
def getID(self, row):
record = self.record(row)
field = record.field(0)
value = field.value()
val, ok = value.toInt()
return val
def insertRecord(self, value):
sql = 'insert into model (model) values (%r)' % value
query = QSqlQuery(self.db)
return query.exec_(sql)
def insertRows(self, row, count, parent):
return True
def refresh(self):
self.setQuery(self.sql, self.db)
def setData(self, index, value, role):
value = str(value.toString())
ok = self.insertRecord(value)
if ok:
self.clear()
self.refresh()
return ok
--
[ signature omitted ]