Results 1 to 1 of 1

Thread: How to store combobox value (not currentIndex) in database?

  1. #1
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default How to store combobox value (not currentIndex) in database?

    I have two tables, devices and clients (connected with relation devices.client_id=clients.id, that is client_id is the foreign key) and a form where I browse through the devices and select the client, as seen in the picture:
    Tables_combo.png
    Qt Code:
    1. // Create the model and set the table
    2. model = new QSqlRelationalTableModel(this);
    3. model->setTable("devices");
    4. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5.  
    6. clientIndex = model->fieldIndex("client_id");
    7.  
    8. // set the SQL relation
    9. model->setRelation(clientIndex,
    10. QSqlRelation("clients", "id", "lastname"));
    11.  
    12. // populate the combo
    13. QSqlTableModel *relClientModel = model->relationModel(clientIndex);
    14. ui.client_id_cbo->setModel(relClientModel);
    15. ui.client_id_cbo->setModelColumn(relClientModel->fieldIndex("lastname"));
    16.  
    17. // mapping
    18. mapper = new QDataWidgetMapper(this);
    19. mapper->setModel(model);
    20. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    21. mapper->addMapping(ui.client_id_cbo, clientIndex);
    22.  
    23.  
    24. // On insert new record
    25. model->setData(model->index(row, typeIndex), type_cbo_item_data);
    To copy to clipboard, switch view to plain text mode 
    I want the variable 'type_cbo_item_data', line 25, to have the value of 4.
    But the combo,I think, must hold (somehow) the ids from the table, from the
    Qt Code:
    1. model->setRelation(clientIndex,
    2. QSqlRelation("clients", "id", "lastname"));
    To copy to clipboard, switch view to plain text mode 

    So, how can I do this?
    To store the id of the selected row?
    Last edited by panoss; 8th February 2017 at 11:38.

Similar Threads

  1. Qt database to store
    By ganeshgladish in forum Newbie
    Replies: 3
    Last Post: 6th April 2013, 12:15
  2. How store a unsigned short into a database
    By franco.amato in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2011, 22:17
  3. Need ideas on how to store data in SQL-Database
    By homerun4711 in forum Newbie
    Replies: 3
    Last Post: 5th January 2011, 23:10
  4. Replies: 1
    Last Post: 16th November 2007, 12:06
  5. Is it possible to store pointers in a database ?
    By probine in forum General Programming
    Replies: 8
    Last Post: 5th April 2006, 21:28

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.