本文整理汇总了Python中PyQt4.QtSql.QSqlTableModel.record方法的典型用法代码示例。如果您正苦于以下问题:Python QSqlTableModel.record方法的具体用法?Python QSqlTableModel.record怎么用?Python QSqlTableModel.record使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtSql.QSqlTableModel
的用法示例。
在下文中一共展示了QSqlTableModel.record方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MainForm
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import record [as 别名]
class MainForm(QDialog):
def __init__(self):
super(MainForm, self).__init__()
self.model = QSqlTableModel(self)
self.model.setTable('tbl_symbols')
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch)
addButton = QPushButton("&Add")
deleteButton = QPushButton("&Delete")
buttonLayout = QVBoxLayout()
buttonLayout.addWidget(addButton)
buttonLayout.addWidget(deleteButton)
buttonLayout.addStretch()
lay = QHBoxLayout()
lay.addWidget(self.view)
lay.addLayout(buttonLayout)
self.setLayout(lay)
self.connect(addButton, SIGNAL("clicked()"), self.addRecord)
self.connect(deleteButton, SIGNAL("clicked()"), self.deleteRecord)
def addRecord(self):
row = self.model.rowCount()
self.model.insertRow(row)
index = self.model.index(row, 1)
self.view.setCurrentIndex(index)
self.view.edit(index)
def deleteRecord(self):
index = self.view.currentIndex()
if not index.isValid():
return
#QSqlDatabase.database().transaction()
record = self.model.record(index.row())
self.model.removeRow(index.row())
self.model.submitAll()
示例2: ReferenceDataDlg
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import record [as 别名]
class ReferenceDataDlg(QDialog):
def __init__(self, table, title, parent=None):
super(ReferenceDataDlg, self).__init__(parent)
self.create_widgets(table)
self.layout_widgets()
self.create_connections()
self.setWindowTitle(
"Asset Manager - Edit {0} Reference Data".format(title))
def create_widgets(self, table):
self.model = QSqlTableModel(self)
self.model.setTable(table)
self.model.setSort(NAME, Qt.AscendingOrder)
self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
self.model.setHeaderData(NAME, Qt.Horizontal, QVariant("Name"))
self.model.setHeaderData(DESCRIPTION, Qt.Horizontal,
QVariant("Description"))
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.setSelectionMode(QTableView.SingleSelection)
self.view.setSelectionBehavior(QTableView.SelectRows)
self.view.setColumnHidden(ID, True)
self.view.resizeColumnsToContents()
self.addButton = QPushButton("&Add")
self.deleteButton = QPushButton("&Delete")
self.okButton = QPushButton("&OK")
def layout_widgets(self):
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(self.addButton)
buttonLayout.addWidget(self.deleteButton)
buttonLayout.addStretch()
buttonLayout.addWidget(self.okButton)
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addLayout(buttonLayout)
self.setLayout(layout)
def create_connections(self):
self.addButton.clicked.connect(self.addRecord)
self.deleteButton.clicked.connect(self.deleteRecord)
self.okButton.clicked.connect(self.accept)
def addRecord(self):
row = self.model.rowCount()
self.model.insertRow(row)
index = self.model.index(row, NAME)
self.view.setCurrentIndex(index)
self.view.edit(index)
def deleteRecord(self):
index = self.view.currentIndex()
if not index.isValid():
return
record = self.model.record(index.row())
id = record.value(ID).toInt()[0]
table = self.model.tableName()
query = QSqlQuery()
if table == "deps":
query.exec_(QString("SELECT COUNT(*) FROM employee "
"WHERE deo_id = %1").arg(id))
elif table == "cities":
query.exec_(QString("SELECT COUNT(*) FROM employee "
"WHERE city_id = %1").arg(id))
count = 0
if query.next():
count = query.value(0).toInt()[0]
if count:
QMessageBox.information(self,
QString("Delete %1").arg(table),
(QString("Cannot delete %1<br>"
"from the %2 table because it is used by "
"%3 records")
.arg(record.value(NAME).toString())
.arg(table).arg(count)))
return
self.model.removeRow(index.row())
self.model.submitAll()
示例3: StaffDataDlg
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import record [as 别名]
class StaffDataDlg(QDialog):
def __init__(self, parent=None):
super(StaffDataDlg, self).__init__(parent)
self.create_widgets()
self.layout_widgets()
self.create_connections()
self.setMinimumWidth(850)
self.setWindowTitle(u"Список сотрудников компании")
def create_widgets(self):
self.model = QSqlTableModel(self)
self.model.setTable("employee")
self.model.setSort(ID, Qt.AscendingOrder)
self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
self.model.setHeaderData(CITY, Qt.Horizontal, QVariant("City"))
self.model.setHeaderData(DEPARTMENT, Qt.Horizontal, QVariant("Department"))
self.model.setHeaderData(FIRSTNAME, Qt.Horizontal, QVariant("First Name"))
self.model.setHeaderData(LASTNAME, Qt.Horizontal, QVariant("Last Name"))
self.model.setHeaderData(SEX, Qt.Horizontal, QVariant("sex"))
self.model.setHeaderData(ADDRESS, Qt.Horizontal, QVariant("Address"))
self.model.setHeaderData(PID, Qt.Horizontal, QVariant("PID"))
self.model.setHeaderData(TITLE, Qt.Horizontal, QVariant("Title"))
self.model.setHeaderData(SOLARY, Qt.Horizontal, QVariant("SOLARY"))
self.model.setHeaderData(SHIFT, Qt.Horizontal, QVariant("SHIFT"))
self.model.setHeaderData(HOURS, Qt.Horizontal, QVariant("HOURS"))
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.setSelectionMode(QTableView.SingleSelection)
self.view.setSelectionBehavior(QTableView.SelectRows)
self.view.setColumnHidden(ID, True)
self.view.resizeColumnsToContents()
self.buttonBox = QDialogButtonBox()
self.addButton = self.buttonBox.addButton(u"&Добавить", QDialogButtonBox.ActionRole)
self.deleteButton = self.buttonBox.addButton(u"&Удалить", QDialogButtonBox.ActionRole)
self.sortButton = self.buttonBox.addButton(u"&Сортировать", QDialogButtonBox.ActionRole)
menu = QMenu(self)
self.sortByTitleAction = menu.addAction(u"Сортировка по &Title")
self.sortBySolaryAction = menu.addAction(u"Сортировка по &SOLARY")
self.sortByIDAction = menu.addAction(u"Сортировка по &ID")
self.sortButton.setMenu(menu)
self.closeButton = self.buttonBox.addButton(QDialogButtonBox.Close)
def layout_widgets(self):
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
def create_connections(self):
self.addButton.clicked.connect(self.addRecord)
self.deleteButton.clicked.connect(self.deleteRecord)
self.closeButton.clicked.connect(self.accept)
def addRecord(self):
row = self.model.rowCount()
self.model.insertRow(row)
index = self.model.index(row, TITLE)
self.view.setCurrentIndex(index)
self.view.edit(index)
def deleteRecord(self):
index = self.view.currentIndex()
if not index.isValid():
return
record = self.model.record(index.row())
title = record.value(TITLE).toString()
desc = record.value(SOLARY).toString()
if (
QMessageBox.question(
self,
"Reference Data",
QString("Delete %1 from title %2?").arg(desc).arg(title),
QMessageBox.Yes | QMessageBox.No,
)
== QMessageBox.No
):
return
self.model.removeRow(index.row())
self.model.submitAll()
示例4: MainWindow
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import record [as 别名]
#.........这里部分代码省略.........
if where == MainWindow.FIRST:
row=0
elif where == MainWindow.PREV:
row = 0 if row <= 1 else row - 1
elif where == MainWindow.NEXT:
row += 1
if row >= self.mModel.rowCount():
row = self.mModel.rowCount() -1
elif where == MainWindow.LAST:
row = self.mModel.rowCount()- 1
self.mapper.setCurrentIndex(row)
self.mmUpdate()
def addScaffRecord(self):
if not self.db.isOpen():
self.statusbar.showMessage(
"Database non aperto...",
5000)
return
row = self.mModel.rowCount()
self.mapper.submit()
self.mModel.insertRow(row)
self.mapper.setCurrentIndex(row)
self.scaffLineEdit.setFocus()
self.mmUpdate()
def addDettRecord(self):
if not self.db.isOpen():
self.statusbar.showMessage(
"Database non aperto...",
5000)
return
rowscaff = self.mapper.currentIndex()
record = self.mModel.record(rowscaff)
masterid = record.value(ID).toInt()[0]
if masterid < 1:
self.statusbar.showMessage(
"Scaffale non valido o non confermato...",
5000)
self.scaffLineEdit.setFocus()
return
# aggiunge la nuova riga alla vista
self.sModel.submitAll()
self.sModel.select()
row = self.sModel.rowCount()
self.sModel.insertRow(row)
if row > 1:
precfatt = self.sModel.data(self.sModel.index(row-1, FATT))
else:
precfatt = ''
if row > 1:
lastData = self.sModel.data(self.sModel.index(row-1, DATAINS))
else:
lastData = ''
self.sModel.setData(self.sModel.index(row, MMID),
QVariant(masterid))
self.sModel.setData(self.sModel.index(row, QT),
QVariant(1))
self.sModel.setData(self.sModel.index(row, IMP),
QVariant(0.0))
self.sModel.setData(self.sModel.index(row, FATT),
QVariant(precfatt))
self.editindex = self.sModel.index(row, DATAINS)
self.sTableView.setCurrentIndex(self.editindex)
self.sTableView.edit(self.editindex)