Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [PyQt] How can I access QTableModel by primary key index
QtWS25 Last Chance

[PyQt] How can I access QTableModel by primary key index

Scheduled Pinned Locked Moved Language Bindings
2 Posts 1 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ithrayen
    wrote on last edited by
    #1

    Hello,
    In the following code:

    @row = 3
    col = 4
    value = "Hi"
    self.myQTableModel.setData(self.myQTableModel.index(row, col), value)@

    the row parameter of the index() method is not related to the primary key of my table.

    What I want, is access to the row where pimaryKeyIndex = 3

    is there a way to have instead:
    @self.myQTableModel.setData(self.myQTableModel.index(primaryKeyIndex, col), value) @

    Thanks

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Ithrayen
      wrote on last edited by
      #2

      Many thanks to David Cortesi hwo sent me the following Email:

      is there a way to have instead:
      self.myQTableModel.setData(
      self.myQTableModel.index(primaryKeyIndex, col), value)

      QAbstractTableModel is extremely abstract! It offers no relational functions, only index() and parent().

      It is up to you to make the table model real by adding code. The documentation says you must implement rowCount, columnCount, and data(), and also setData if you want it to be modifiable.

      But you may implement other functions. In your situation I would implement a method pkRow(keyvalue) which returns the row number that corresponds to a given primary key value. Then your statement becomes,

      self.myModel.setData( self.myModel.index( self.myModel.pkRow(keyvalue), col ), value_for_col)
      

      To save keystrokes, you could write a pkIndex(keyvalue, col) method (built on top of pkRow). Then:

      self.myModel.setData( self.myModel.pkIndex(keyvalue,col), value_for_col)
      

      You pkRow method could perhaps cache the recent keyvalues it has seen, to save time.

      Note there is also a class QSqlRelationalTableModel, but it does not have any key-dependent methods either. But if your real data is simple SQL table perhaps you should use that.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved