python - QDataWidgetMapper-find index for sqlite database row in pyqt -


i want use qdatawidgetmapper link widget data in sqlite table. methods find specifying data's row ones seem work if iterating through rows sequentially 1 @ time: setcurrentindex, tofirst, tolast, tonext, toprevious. able jump directly row containing data needed, , retrieve handle row pass mapper.

the primary key of table integer autoincrements, since rows can deleted there no guarantee index guessed using key.

------------------------- |     item_info         | |-----------------------| |item_id  |  item_desc  | |---------|-------------| |1        |  item 1     | |2        |  item 2     | |7        |  blue item  | |15       |  pink item  | #ordered index 3 ------------------------- 

i briefly hoped able use rowid return ordered index of table rows, looks returns primary key , isn't related notion of row's position in table.

i use counter go through each row until correct 1 found, , use resulting counter value argument setcurrentindex, seems terrible solution. here's example of that, displaying item_desc row item_id 15:

import sys  pyqt5 import qt,qtwidgets,qtsql  class form(qtwidgets.qwidget):     def __init__(self,parent=none):         qtwidgets.qwidget.__init__(self,parent)         self.line_edit=qtwidgets.qlineedit()         layout=qtwidgets.qvboxlayout()         layout.addwidget(self.line_edit)         self.setlayout(layout)                  db=qtsql.qsqldatabase.adddatabase("qsqlite")         db.setdatabasename("populated_db.db")         db.open()          self.item_info_model = qtsql.qsqltablemodel(self)         self.item_info_model.settable("item_info")         self.item_info_model.select()          self.item_info_mapper = qt.qdatawidgetmapper(self)         self.item_info_mapper.setsubmitpolicy(qt.qdatawidgetmapper.manualsubmit)         self.item_info_mapper.setmodel(self.item_info_model)          self.item_info_mapper.addmapping(self.line_edit, 1) #establishes mapping between line edit , second column of data           self.show_description(15)      def show_description(self,desired_item_id):         ###### here ugly technique search 1 one until desired row discovered ######         i=0 #start counter         success=false          query=qtsql.qsqlquery()         query.exec("select item_id item_info")         query.first()          while not success:             if query.value(0)==desired_item_id:                 success=true                 self.item_info_mapper.setcurrentindex(i)             else:                 i+=1                 query.next()  a=qtwidgets.qapplication(sys.argv) form=form() form.show() a.exec_() 

this accomplishes task, seems there should better way uses database efficiently, direct way set mapping rather numeric index. i've been unable find far if case though.

you can use record() method of model:

def show_description(self,desired_item_id):     in range(self.item_info_model.rowcount()):         if self.item_info_model.record(i).value(0) == desired_item_id:  # field can defined position or name              self.item_info_mapper.setcurrentindex(i)             break 

alternativaly can set filter model:

def show_description(self,desired_item_id):     self.item_info_model.setfilter('item_id = {}'.format(desired_item_id))     self.item_info_mapper.tofirst() 

your model populated (by self.item_info_model.select() in class form(), reselected automatically see documentation


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -