self.model.setTable('main')
self.
model.
setRelation(1,
QSqlRelation("Names",
"nameOfCm",
"nameOfCm"))self.
model.
setRelation(2,
QSqlRelation("Types",
"type_IDs",
"typeOfCm"))self.
model.
setRelation(8,
QSqlRelation("Places",
"placeName",
"placeName"))self.model.select()
self.table.setModel(self.model)
self.table = QTableView()
self.model = QSqlRelationalTableModel(self)
self.model.setTable('main')
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.setRelation(1, QSqlRelation("Names", "nameOfCm", "nameOfCm"))
self.model.setRelation(2, QSqlRelation("Types", "type_IDs", "typeOfCm"))
self.model.setRelation(8, QSqlRelation("Places", "placeName", "placeName"))
self.model.select()
self.table.setModel(self.model)
self.table.setItemDelegate(QSqlRelationalDelegate(self.table))
To copy to clipboard, switch view to plain text mode
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
connectDB.setDatabaseName('test.db')
connectDB.open()
createNameTable='CREATE TABLE IF NOT EXISTS "Names" ("nameOfCm" TEXT, PRIMARY KEY("nameOfCm"))'
createTypeTable='CREATE TABLE IF NOT EXISTS "Types" ("type_IDs" TEXT, "typeOfCm" TEXT,PRIMARY KEY("type_IDs"))'
createMainTable='CREATE TABLE IF NOT EXISTS "main" ("partnumber" TEXT,"name" TEXT,"type" TEXT,PRIMARY KEY("partnumber"))'
query.exec(createNameTable)
query.exec(createTypeTable)
query.exec(createMainTable)
query.exec("INSERT INTO Names (nameOfCm) VALUEs ('IC')")
query.exec("INSERT INTO Names (nameOfCm) VALUEs ('Transistor')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('Transistor1','NPN')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('Transistor2','PNP')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('IC1','Op_AMP')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('IC2','Micro')")
def __init__(self):
super(main,self).__init__()
self.setWindowTitle('My Electronic Inventory v: 0.1')
self.setGeometry(10, 10, 400 , 500)
self.UI()
self.show()
self.databaseIsConect()
def UI(self):
self.setCentralWidget(self.mainCentralWidget)
self.centralWidget().setLayout(self.mainLayout)
self.newComponentButton.clicked.connect(self.newComponentButtonClicked)
self.model.setTable('main')
self.
model.
setRelation(1,
QSqlRelation("Names",
"nameOfCm",
"nameOfCm")) self.
model.
setRelation(2,
QSqlRelation("Types",
"type_IDs",
"typeOfCm")) self.model.select()
self.table.setModel(self.model)
self.
table.
setEditTriggers(QAbstractItemView.
DoubleClicked|QAbstractItemView.
AnyKeyPressed) self.model.setHeaderData(0,Qt.Horizontal,('Part Number'))
self.model.setHeaderData(1,Qt.Horizontal,('Name'))
self.model.setHeaderData(2,Qt.Horizontal,('Type'))
self.mainLayout.addWidget(self.newComponentButton)
self.mainLayout.addWidget(self.table)
def databaseIsConect(self):
if connectDB.isOpen():
print("Databsae Is Connected")
else:
print("Erorr!. Databsae Is Not Connected")
def newComponentButtonClicked(self):
row = self.model.rowCount()
self.model.insertRow(row)
if __name__ == '__main__':
ex = main()
sys.exit(app.exec_())
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
connectDB = QSqlDatabase.addDatabase("QSQLITE")
connectDB.setDatabaseName('test.db')
connectDB.open()
query = QSqlQuery()
createNameTable='CREATE TABLE IF NOT EXISTS "Names" ("nameOfCm" TEXT, PRIMARY KEY("nameOfCm"))'
createTypeTable='CREATE TABLE IF NOT EXISTS "Types" ("type_IDs" TEXT, "typeOfCm" TEXT,PRIMARY KEY("type_IDs"))'
createMainTable='CREATE TABLE IF NOT EXISTS "main" ("partnumber" TEXT,"name" TEXT,"type" TEXT,PRIMARY KEY("partnumber"))'
query.exec(createNameTable)
query.exec(createTypeTable)
query.exec(createMainTable)
query.exec("INSERT INTO Names (nameOfCm) VALUEs ('IC')")
query.exec("INSERT INTO Names (nameOfCm) VALUEs ('Transistor')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('Transistor1','NPN')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('Transistor2','PNP')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('IC1','Op_AMP')")
query.exec("INSERT INTO Types (type_IDs,typeOfCm) VALUEs ('IC2','Micro')")
class main (QMainWindow):
def __init__(self):
super(main,self).__init__()
self.setWindowTitle('My Electronic Inventory v: 0.1')
self.setGeometry(10, 10, 400 , 500)
self.UI()
self.show()
self.databaseIsConect()
def UI(self):
self.mainCentralWidget = QWidget()
self.mainLayout = QVBoxLayout()
self.setCentralWidget(self.mainCentralWidget)
self.centralWidget().setLayout(self.mainLayout)
self.newComponentButton= QPushButton("Add component")
self.newComponentButton.clicked.connect(self.newComponentButtonClicked)
self.table= QTableView()
self.model = QSqlRelationalTableModel(self)
self.model.setTable('main')
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.setRelation(1, QSqlRelation("Names", "nameOfCm", "nameOfCm"))
self.model.setRelation(2, QSqlRelation("Types", "type_IDs", "typeOfCm"))
self.model.select()
self.table.setModel(self.model)
self.table.setItemDelegate(QSqlRelationalDelegate(self.table))
self.table.setEditTriggers(QAbstractItemView.DoubleClicked|QAbstractItemView.AnyKeyPressed)
self.model.setHeaderData(0,Qt.Horizontal,('Part Number'))
self.model.setHeaderData(1,Qt.Horizontal,('Name'))
self.model.setHeaderData(2,Qt.Horizontal,('Type'))
self.mainLayout.addWidget(self.newComponentButton)
self.mainLayout.addWidget(self.table)
def databaseIsConect(self):
if connectDB.isOpen():
print("Databsae Is Connected")
else:
print("Erorr!. Databsae Is Not Connected")
def newComponentButtonClicked(self):
row = self.model.rowCount()
self.model.insertRow(row)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = main()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
def __init__(self, parent):
super().__init__()
def setData(self, item, value, role):
super().setData(item,value, role)
a= item.siblingAtColumn(1).data()
self.relationModel(2).setFilter("type_IDs like '"+a+"%'")
return True
class mysqlmodel(QSqlRelationalTableModel):
def __init__(self, parent):
super().__init__()
def setData(self, item, value, role):
super().setData(item,value, role)
a= item.siblingAtColumn(1).data()
self.relationModel(2).setFilter("type_IDs like '"+a+"%'")
return True
To copy to clipboard, switch view to plain text mode
This works for my purpose, but there are issues:
Bookmarks