Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Changing data in an QAbstractListModel

Reply
Thread Tools

Changing data in an QAbstractListModel

 
 
exhuma.twn
Guest
Posts: n/a
 
      09-06-2007
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()
#--------------------------------------------------

 
Reply With Quote
 
 
 
 
Jonathan Gardner
Guest
Posts: n/a
 
      09-06-2007
On Sep 6, 9:12 am, "exhuma.twn" <(E-Mail Removed)> wrote:
> 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?

 
Reply With Quote
 
 
 
 
exhuma.twn
Guest
Posts: n/a
 
      09-07-2007
On Sep 7, 12:42 am, Jonathan Gardner
<(E-Mail Removed)> wrote:
> On Sep 6, 9:12 am, "exhuma.twn" <(E-Mail Removed)> wrote:
>
> > 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?


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?

 
Reply With Quote
 
Jonathan Gardner
Guest
Posts: n/a
 
      09-10-2007
On Sep 7, 1:24 am, "exhuma.twn" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing style of the numbers in a CSS <ol> without changing the style of the <li> Aaron Beall HTML 2 09-14-2007 08:07 PM
<FAQENTRY>I'm changing my page but nothing is changing on the screen. Why?</FAQENTRY> VK Javascript 3 03-15-2007 04:59 PM
changing file extension without changing filename jamy Computer Support 4 03-04-2005 01:03 AM
Difficulty in changing the Connection Server control path changing ... Samridhi Kumar Shukla ASP .Net 1 11-30-2003 02:31 AM
Help needed formatted submit results - Changing data Robert V Perl 1 07-19-2003 12:49 AM



Advertisments