Python Forum
QSqlRelationalTableModel: how do I save combobox data?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QSqlRelationalTableModel: how do I save combobox data?
#1
If you run this code, a database is created, and a window opens, where you can select (on the right) a client from a list (combobox) of clients.
#!/usr/bin/env python

from PyQt4 import QtCore, QtGui, QtSql


def initializeModel(model):
   model.setTable('devices')

   model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
   model.setRelation(2, QtSql.QSqlRelation('clients', 'id', 'lastname'))

   model.setHeaderData(0, QtCore.Qt.Horizontal, "ID")
   model.setHeaderData(1, QtCore.Qt.Horizontal, "Brand")
   model.setHeaderData(2, QtCore.Qt.Horizontal, "Client")

   model.select()


def createView(title, model):
   view = QtGui.QTableView()
   view.setModel(model)
   view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
   view.setWindowTitle(title)

   return view


def createDB():
   db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
   db.setDatabaseName('rd.db')

   if not db.open():
       QtGui.QMessageBox.critical(None, QtGui.qApp.tr("Cannot open database"),
                                  QtGui.qApp.tr("Unable to establish a database connection.\n"
                                                "This example needs SQLite support. Please read "
                                                "the Qt SQL driver documentation for information "
                                                "how to build it.\n\n" "Click Cancel to exit."),
                                  QtGui.QMessageBox.Cancel)

       return False

   query = QtSql.QSqlQuery()

   query.exec_("create table devices(id int primary key, "
               "brand varchar(20), client_id int)")



   query.exec_("insert into devices values(101, 'Toshiba', 17)")
   query.exec_("insert into devices values(102, 'LG', 14)")
   query.exec_("insert into devices values(103, 'Sony', 16)")

   query.exec_("create table clients(id int primary key, "
               "firstname varchar(20), lastname varchar(20))")

   query.exec_("insert into clients values(14, 'John', 'Padd')")
   query.exec_("insert into clients values(15, 'Bob', 'New')")
   query.exec_("insert into clients values(16, 'Sean', 'Wheeler')")
   query.exec_("insert into clients values(17, 'Diana', 'Rash')")
   query.exec_("insert into clients values(18, 'George', 'Nett')")
   return True


if __name__ == '__main__':

   import sys

   app = QtGui.QApplication(sys.argv)

   createDB()

   model = QtSql.QSqlRelationalTableModel()

   initializeModel(model)

   view = createView("Relational Table Model", model)

   view.show()

   sys.exit(app.exec_())
My question is, ok I have selected one of the clients in the list of the combobox.
How do I save it in the database?

(it has something to do with model.setData or something.)
Reply


Messages In This Thread
QSqlRelationalTableModel: how do I save combobox data? - by panoss - Feb-08-2017, 03:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 564 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Save data frame to .csv df.to.csv() mcva 1 1,547 Feb-03-2022, 07:05 PM
Last Post: mcva
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,183 Jul-02-2021, 02:19 PM
Last Post: xtc14
  How to save json data in a dataframe shantanu97 1 2,169 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Binding Complex data to Combobox gcfernando 2 2,087 Sep-14-2020, 03:24 PM
Last Post: gcfernando
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,458 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  How to save Python Requests data sent to server? RedLeonard 5 4,999 Jul-05-2020, 10:33 AM
Last Post: RedLeonard
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,117 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Save Arduino data in mysql server via raspberrypi rithikvg 1 3,425 Jun-24-2020, 10:59 PM
Last Post: Larz60+
  Selection and display of data by combobox LagratteCchouette 10 7,557 Mar-02-2020, 06:34 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020