Pythonfrom PyQt4 import QtGui, QtCore, QtSqlimport os.path class mainform(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.resize(600, 700) self.con = QtSql.QSqlDatabase.addDatabase("QSQLITE", "Base") self.con.setDatabaseName("./SQLiteBase/PyQt4DB.s3db") self.OpenOrCreateDB() self.mainLayout = QtGui.QVBoxLayout() self.setLayout(self.mainLayout) self.btnFill = QtGui.QPushButton("Заполнить БД некоторыми данными") self.mainLayout.addWidget(self.btnFill) self.btnFill.clicked.connect(self.Fill_View_With_Some_Values) self.model = QtSql.QSqlRelationalTableModel(None, self.con) self.model.setTable('thirst') self.typeindex = self.model.fieldIndex("type") self.model.setRelation(self.typeindex, QtSql.QSqlRelation("second", "type", "text")) self.model.select() self.view1 = QtGui.QTableView() self.mainLayout.addWidget(self.view1) self.view1.setModel(self.model) self.view1.setItemDelegate(QtSql.QSqlRelationalDelegate(self.view1)) self.view2 = QtGui.QTableView() self.mainLayout.addWidget(self.view2) self.view2.setModel(self.model) self.txtBoxTovar = QtGui.QLineEdit() self.mainLayout.addWidget(self.txtBoxTovar) self.comboBoxType = QtGui.QComboBox() self.mainLayout.addWidget(self.comboBoxType) self.btnNew = QtGui.QPushButton("Новая запись") self.mainLayout.addWidget(self.btnNew) self.mapper = QtGui.QDataWidgetMapper() self.mapper.setModel(self.model) self.mapper.setItemDelegate(QtSql.QSqlRelationalDelegate()) self.mapper.addMapping(self.txtBoxTovar, 1) self.comboBoxType.setModel(self.model) self.comboBoxType.setModelColumn(self.model.fieldIndex("text")) self.mapper.addMapping(self.comboBoxType, 2, 'text') self.mapper.toFirst() def Fill_View_With_Some_Values(self): query = QtSql.QSqlQuery(self.con) query.exec("INSERT INTO thirst (tovar, type) VALUES ('Potato', 1)") query.exec("INSERT INTO thirst (tovar, type) VALUES ('Apple', 2)") query.exec("INSERT INTO thirst (tovar, type) VALUES ('Juice', 3)") query.exec("INSERT INTO thirst (tovar, type) VALUES ('Nuts', 4)") self.model.select() def OpenOrCreateDB(self): ThisIsNewBase = False if not os.path.exists("./SQLiteBase/PyQt4DB.s3db"): ThisIsNewBase = True if not self.con.open(): print ("БД не открылась!") else: print ("БД открыта") if ThisIsNewBase == True: self.Create_Structure_of_DB() def Create_Structure_of_DB(self): query = QtSql.QSqlQuery(self.con) query.exec("CREATE TABLE thirst (" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "tovar VARCHAR(20) NOT NULL, " "type INTEGER NOT NULL" ")") query.exec("CREATE TABLE second (" "type INTEGER, " "text VARCHAR(50)" ")") query.exec("INSERT INTO second VALUES(1, 'Бяка')") query.exec("INSERT INTO second VALUES(2, 'Кака')") query.exec("INSERT INTO second VALUES(3, 'Добро')") query.exec("INSERT INTO second VALUES(4, 'Благо')") if __name__=="__main__": import sys app = QtGui.QApplication(sys.argv) form1 = mainform() form1.setWindowTitle("Работа с БД при помощи PyQt4") form1.show() sys.exit(app.exec_())
Python self.comboBoxType.setModel(self.model) self.comboBoxType.setModelColumn(self.model.fieldIndex("text"))