Changing data in an QAbstractListModel

E

exhuma.twn

Hi, I want to create a very simple read-only ListView. As the same
data is used on various places in the UI, I decided to create a new
ListView with a new Model instead of using the QListWidget.

So far, the data displays correctly after setting it with "setModel"
on the ListView. But how do I tell the views that the data is updated
in the model?

I defined a simple "update" method in the model which I call on
certain events to fetch the new data in the DB. I tried to "emit" the
"dataChanged()" signal of the Model without success. I don't know
where I should get the two required "index" parameters from.

Any hints?

Here's the model:
#--------------------------------------------------
class UnitListModel(QtCore.QAbstractListModel):

__units = []

def update(self):
c = db_con.cursor()
# SELECT only the most recent entry for each company
c.execute("SELECT DISTINCT ON (unit_id) nom FROM unit ORDER BY
unit_id, year DESC")
for unit in c.fetchall():
self.__units.append( unit[0] )
c.close()

def rowCount(self, parent = QtCore.QModelIndex()):
return len(self.__units)

def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()

if index.row() >= len(self.__units):
return QtCore.QVariant()

if role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.__units[index.row()])
else:
return QtCore.QVariant()
#--------------------------------------------------
 
J

Jonathan Gardner

I defined a simple "update" method in the model which I call on
certain events to fetch the new data in the DB. I tried to "emit" the
"dataChanged()" signal of the Model without success. I don't know
where I should get the two required "index" parameters from.

Any hints?

Shouldn't the index method give you the indexes you need?
 
E

exhuma.twn

Shouldn't the index method give you the indexes you need?

Right... The update method now looks like this:

def update(self):
c = db_con.cursor()
# SELECT only the most recent entry for each company
c.execute("SELECT DISTINCT ON (unit_id) nom FROM unit ORDER BY
unit_id, year DESC")
for unit in c.fetchall():
self.__units.append( unit[0] )
c.close()

si = self.index(0)
se = self.index(len(self.__units)-1)

self.emit(Signal("dataChanged()"), si, se)


Still, nothing is happening when I call this method. Do I still need
to handle the "dataChanged" signal somehow? Or does the ListView take
care of this?
 
J

Jonathan Gardner

Still, nothing is happening when I call this method. Do I still need
to handle the "dataChanged" signal somehow? Or does the ListView take
care of this?

You might have better luck asking these kinds of questions in the Qt
or PyQt forums.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top