本文整理汇总了Python中PyQt4.QtSql.QSqlTableModel.setFilter方法的典型用法代码示例。如果您正苦于以下问题:Python QSqlTableModel.setFilter方法的具体用法?Python QSqlTableModel.setFilter怎么用?Python QSqlTableModel.setFilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtSql.QSqlTableModel
的用法示例。
在下文中一共展示了QSqlTableModel.setFilter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_model_to_table
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import setFilter [as 别名]
def set_model_to_table(self, widget, table_name, filter_):
''' Set a model with selected filter.
Attach that model to selected table '''
# Set model
model = QSqlTableModel();
model.setTable(table_name)
model.setEditStrategy(QSqlTableModel.OnManualSubmit)
model.setFilter(filter_)
model.select()
# Check for errors
if model.lastError().isValid():
self.controller.show_warning(model.lastError().text())
# Attach model to table view
widget.setModel(model)
示例2: QuestionDlg
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import setFilter [as 别名]
#.........这里部分代码省略.........
self.StudentModel.setData(self.StudentModel.index(row, 5), 0)
def removeStudent(self):
index = self.StudentView.currentIndex()
row = index.row()
if QMessageBox.question(self, "删除确认", "是否要删除当前选中记录?", "确定", "取消") == 0:
self.StudentModel.removeRows(row, 1)
self.StudentModel.submitAll()
self.StudentModel.database().commit()
def revertStudent(self):
self.StudentModel.revertAll()
self.StudentModel.database().rollback()
def saveStudent(self):
self.StudentModel.database().transaction()
if self.StudentModel.submitAll():
self.StudentModel.database().commit()
# print("save success! ->commit")
else:
self.StudentModel.revertAll()
self.StudentModel.database().rollback()
#######======= classModel ============###############
def newClass(self):
row = self.ClassnameModel.rowCount()
self.ClassnameModel.insertRow(row)
def removeClass(self):
index = self.ClassnameView.currentIndex()
row = index.row()
curClassname = index.sibling(index.row(),0).data()
strwhere = "classname like '" + curClassname + "'"
self.StudentModel.setFilter(strwhere)
self.StudentModel.select()
# print(self.StudentModel.rowCount(), "----", )
if QMessageBox.question(self, "删除确认", "删除班级意味着会删除本班所有人员信息。是否要删除当前选中记录?", "确定", "取消") == 0:
self.ClassnameModel.removeRows(row, 1)
self.ClassnameModel.submitAll()
self.ClassnameModel.database().commit()
self.StudentModel.removeRows(0, self.StudentModel.rowCount())
self.StudentModel.submitAll()
self.StudentModel.database().commit()
def revertClass(self):
self.ClassnameModel.revertAll()
self.ClassnameModel.database().rollback()
def saveClass(self):
query = QSqlQuery(self.db)
# record the old class name
lstOldClassName = {}
lstOldClassid = []
query.exec_("select rowid, classname from classtable" )
while(query.next()):
lstOldClassName[query.value(0)] = query.value(1)
lstOldClassid.append(query.value(0))
# print(lstOldClassName)
# Update the class Table
self.ClassnameModel.database().transaction()
if self.ClassnameModel.submitAll():
self.ClassnameModel.database().commit()
# print("save success! ->commit")
示例3: PlayerList
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import setFilter [as 别名]
class PlayerList(QDialog):
"""QtSQL Model view of the players"""
def __init__(self, parent):
QDialog.__init__(self)
self.parent = parent
self.model = QSqlTableModel(self, DBHandle.default)
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.model.setTable("player")
self.model.setSort(1, 0)
self.model.setHeaderData(1, Qt.Horizontal, QVariant(m18nc("Player", "Name")))
self.model.setFilter('name not like "ROBOT %" and name not like "Robot %"')
self.view = MJTableView(self)
self.view.verticalHeader().show()
self.view.setModel(self.model)
self.view.hideColumn(0)
self.buttonBox = QDialogButtonBox()
self.buttonBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.newButton = self.buttonBox.addButton(m18nc('define a new player', "&New"), QDialogButtonBox.ActionRole)
self.newButton.setIcon(KIcon("document-new"))
self.newButton.clicked.connect(self.slotInsert)
self.deleteButton = self.buttonBox.addButton(m18n("&Delete"), QDialogButtonBox.ActionRole)
self.deleteButton.setIcon(KIcon("edit-delete"))
self.deleteButton.clicked.connect(self.delete)
cmdLayout = QHBoxLayout()
cmdLayout.addWidget(self.buttonBox)
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addLayout(cmdLayout)
self.setLayout(layout)
self.setWindowTitle(m18n("Players") + ' - Kajongg')
self.setObjectName('Players')
def showEvent(self, dummyEvent):
"""adapt view to content"""
if not self.model.select():
logError("PlayerList: select failed")
sys.exit(1)
self.view.initView()
StateSaver(self, self.view.horizontalHeader())
if not self.view.isColumnHidden(2):
# we loaded a kajonggrc written by an older kajongg version where this table
# still had more columns. This should happen only once.
self.view.hideColumn(2)
self.view.hideColumn(3)
def accept(self):
"""commit all modifications"""
self.view.selectRow(0) # if ALT-O is entered while editing a new row, this is one way
# to end editing and to pass the new value to the model
if not self.model.submitAll():
Sorry(m18n('Cannot save this. Possibly the name already exists. <br><br>' \
'Message from database:<br><br><message>%1</message>',
self.model.lastError().text()))
return
QDialog.accept(self)
def slotInsert(self):
"""insert a record"""
self.model.insertRow(self.model.rowCount())
self.view.selectRow(self.model.rowCount()-1)
def delete(self):
"""delete selected entries"""
sel = self.view.selectionModel()
maxDel = self.view.currentIndex().row() - 1
for idx in sel.selectedIndexes():
if idx.column() != 1:
continue
# sqlite3 does not enforce referential integrity.
# we could add a trigger to sqlite3 but if it raises an exception
# it will be thrown away silently.
# if anybody knows how to propagate sqlite3 exceptions via QtSql
# into python please tell me (wrohdewald)
player = self.model.createIndex(idx.row(), 0).data().toInt()[0]
# no query preparation, we don't expect lots of data
if Query("select 1 from game where p0==%d or p1==%d or p2==%d or p3==%d" % \
(player, player, player, player)).records:
Sorry(m18n('This player cannot be deleted. There are games associated with %1.',
idx.data().toString()))
else:
self.model.removeRow(idx.row())
maxDel = max(maxDel, idx.row())
self.view.selectRow(maxDel+1)
def keyPressEvent(self, event):
"""use insert/delete keys for insert/delete"""
key = event.key()
if key == Qt.Key_Insert:
self.slotInsert()
return
QDialog.keyPressEvent(self, event)
示例4: MainWindow
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import setFilter [as 别名]
#.........这里部分代码省略.........
self.cModel.setHeaderData(CEMAIL, Qt.Horizontal, QVariant("Email"))
self.cModel.setHeaderData(CNOTE, Qt.Horizontal, QVariant("Note"))
self.cModel.select()
def setupTables(self):
"""
Initialize all the application tablesview
"""
self.cTableView.setModel(self.cModel)
self.cTableView.setItemDelegate(MyQSqlRelationalDelegate(self))
self.cTableView.setColumnHidden(CID, True)
self.cTableView.setWordWrap(True)
self.cTableView.resizeRowsToContents()
self.cTableView.setAlternatingRowColors(True)
self.cItmSelModel = QItemSelectionModel(self.cModel)
self.cTableView.setSelectionModel(self.cItmSelModel)
self.cTableView.setSelectionBehavior(QTableView.SelectRows)
self.cTableView.setSortingEnabled(True)
def updateFilter(self):
self.cModel.select()
self.cTableView.setColumnHidden(CID, True)
def applyFilter(self):
if not self.db.isOpen():
self.statusbar.showMessage(
"Database non aperto...",
5000)
return
filter = ( "ragsoc LIKE '%s' OR "
"indirizzo LIKE '%s' OR "
"note LIKE '%s'" %
((self.filterLineEdit.text(),)*3))
self.cModel.setFilter(filter)
self.filtered = True
self.updateFilter()
def resetFilter(self):
if not self.db.isOpen():
self.statusbar.showMessage(
"Database non aperto...",
5000)
return
self.filtered = False
self.filterLineEdit.setText("")
self.cModel.setFilter("")
self.updateFilter()
def addCliRecord(self):
if not self.db.isOpen():
self.statusbar.showMessage(
"Database non aperto...",
5000)
return
customerIndex = self.cTableView.currentIndex()
if self.filtered:
self.resetFilter()
self.cModel.submitAll()
self.cModel.select()
row = self.cModel.rowCount()
self.cModel.insertRow(row)
self.editindex = self.cModel.index(row, CRAGSOC)
self.cTableView.setCurrentIndex(self.editindex)
self.cTableView.edit(self.editindex)
def delCliRecord(self):