Changing data in an QAbstractListModel

Discussion in 'Python' started by exhuma.twn, Sep 6, 2007.

  1. exhuma.twn

    exhuma.twn Guest

    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()
    #--------------------------------------------------
    exhuma.twn, Sep 6, 2007
    #1
    1. Advertising

  2. On Sep 6, 9:12 am, "exhuma.twn" <> 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?
    Jonathan Gardner, Sep 6, 2007
    #2
    1. Advertising

  3. exhuma.twn

    exhuma.twn Guest

    On Sep 7, 12:42 am, Jonathan Gardner
    <> wrote:
    > On Sep 6, 9:12 am, "exhuma.twn" <> 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?
    exhuma.twn, Sep 7, 2007
    #3
  4. On Sep 7, 1:24 am, "exhuma.twn" <> 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.
    Jonathan Gardner, Sep 10, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert V
    Replies:
    1
    Views:
    1,008
    Jim Rendant
    Jul 19, 2003
  2. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    521
    Alvin Bruney
    Nov 30, 2003
  3. Henry Nelson
    Replies:
    1
    Views:
    435
    Henry Nelson
    May 27, 2004
  4. Aaron Beall
    Replies:
    2
    Views:
    1,797
    Aaron Beall
    Sep 14, 2007
  5. VK
    Replies:
    3
    Views:
    175
    Dr J R Stockton
    Mar 15, 2007
Loading...

Share This Page