Results 1 to 5 of 5

Thread: QSqlTableModel Setting Current Row?

  1. #1
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel Setting Current Row?

    ggI have a dialog which displays a database form for editing (LineEdits and ComboBoxes etc). I have a seperate popup dialog which the user can use to lookup records. The lookup dialog returns a row_id to the editing form which it loads from its own model into a mapper.

    At the moment I'm using a 'quick fix' by filtering the model to the returned id, but this approach breaks the edit dialogs navigation buttons.

    Is there any way to identify the row in the model? I have been considering looping through the records to find the right row, but there potentially could be a *lot* of rows in the table.

    btw Both dialogs are querying the same table, but with different rows in there result set and one will be filtered and the other not - so obtaining the row number from the model or using the same model for both will not work.

    Qt Code:
    1. class ClientEditDlg(QDialog):
    2.  
    3. FIRST, PREV, NEXT, LAST = range(4)
    4.  
    5. def __init__(self, client_id=None, clientModel=None, parent=None):
    6. super(ClientEditDlg,self).__init__(parent)
    7.  
    8. if clientModel == None:
    9. self.model = QSqlRelationalTableModel(self)
    10. self.model.setTable("clients")
    11. self.model.setRelation(db.CURRENCY_ID, QSqlRelation("currency",\
    12. "currency_id", "currency_description"))
    13. self.model.setRelation(db.STATUS_ID, \
    14. QSqlRelation("account_status", "account_status_id", \
    15. "account_status_description"))
    16. self.model.setSort(db.ACCOUNT, Qt.AscendingOrder)
    17. self.model.select()
    18. else:
    19. self.model = clientModel
    20.  
    21. def lookupClient(self):
    22. cp = ClientPopupDlg(self)
    23. if cp.exec_():
    24. client_id = cp.selectedID()
    25. self.model.setFilter("client_id = %i" % client_id)
    26. self.mapper.setCurrentIndex(0)
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class ClientPopupDlg(QDialog):
    2.  
    3. QUERY_BASE = """
    4. SELECT client_id, client_account, client_name
    5. FROM clients"""
    6. QUERY_ORDER = """ ORDER BY client_account"""
    7. def __init__(self, parent=None):
    8. super(ClientPopupDlg, self).__init__(parent)
    9.  
    10. query = QSqlQuery(ClientPopupDlg.QUERY_BASE+ClientPopupDlg.QUERY_ORDER)
    11.  
    12. self.model = QSqlTableModel(self)
    13. self.model.setQuery(query)
    14. self.model.setHeaderData(1, Qt.Horizontal, QVariant("Acc"))
    15. self.model.setHeaderData(2, Qt.Horizontal, QVariant("Name"))
    16. self.model.select()
    17.  
    18. def selectedID(self):
    19. s = self.clientView.selectedIndexes()
    20. idModel = s[1].sibling(s[1].row(), 0)
    21. client_id = idModel.data().toInt()[0]
    22. return client_id
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlTableModel Setting Current Row?

    But what exactly is the problem? The model index identifies each record just fine. If you have a relational model, you can query its relation model if you need it (although I don't know if you do).

  3. #3
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel Setting Current Row?

    Quote Originally Posted by wysota View Post
    But what exactly is the problem? The model index identifies each record just fine. If you have a relational model, you can query its relation model if you need it (although I don't know if you do).
    I need to 'goto' a record within the model which is identified with a field value. The field is an auto-incremented index. I can do this by filtering, but that means navigating by next,prev,first,last does not work as I am now working with a result of 1.

    I need to maintain access to all records in the table but be able to set the cursor position within the table model according the field value.

    The auto incremented field will not necessarily correspond to the model row number as the model may be re-ordered or filtered by the user.

    Is it possible to? :
    • Filter the model data
    • Create a variable pointer to the single filtered record
    • Reset the filter
    • Set the model index to the previously filtered record


    Not really sure if I'm explaining any of this properly!

    I'm not even sure if this is possible using the Qt model/view properly. I might try re-implementing the edit form with sql and populating my widgets from the result set.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlTableModel Setting Current Row?

    Won't QAbstractItemModel::match() allow you to find the proper index? Anyway, wouldn't it be better if the lookup dialog returned a model index instead of the field value? If you operate on the same set of tables, it would be wise to stick with the model interface everywhere possible.

  5. #5
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel Setting Current Row?

    Quote Originally Posted by wysota View Post
    Won't QAbstractItemModel::match() allow you to find the proper index?
    Thanks, I have things working at the moment using functions to find next, prev etc with sql. But I'll have a look into the match method, seems much simpler and exactly what I was looking for.

    Quote Originally Posted by wysota View Post
    Anyway, wouldn't it be better if the lookup dialog returned a model index instead of the field value? If you operate on the same set of tables, it would be wise to stick with the model interface everywhere possible.
    I did consider using the same model across all windows/dialogs. However, I decided against that given an example situation such as;

    1. Main Client List window - user filters the records
    2. Client Detail/Edit window - user creates a new record

    Now, if the new record doesn't fit the filter, then from the users perspective their newly created record 'vanishes'.

Similar Threads

  1. Qt designer plugin errors on OSX Leopard
    By mpotocnik in forum Qt Tools
    Replies: 10
    Last Post: 21st January 2008, 09:45
  2. setting working directory for current process
    By mule in forum Qt Programming
    Replies: 1
    Last Post: 8th October 2007, 13:54
  3. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 18:59
  4. Replies: 15
    Last Post: 21st April 2007, 17:46
  5. Replies: 1
    Last Post: 19th April 2007, 22:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.