本文整理汇总了Python中PyQt4.QtSql.QSqlTableModel.removeRow方法的典型用法代码示例。如果您正苦于以下问题:Python QSqlTableModel.removeRow方法的具体用法?Python QSqlTableModel.removeRow怎么用?Python QSqlTableModel.removeRow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtSql.QSqlTableModel
的用法示例。
在下文中一共展示了QSqlTableModel.removeRow方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MainForm
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import removeRow [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 removeRow [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 removeRow [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: PlayerList
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import removeRow [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)
示例5: DataDialog
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import removeRow [as 别名]
class DataDialog(QDialog):
def __init__(self, name, table, parent=None):
super(DataDialog, self).__init__(parent)
self.name = name
self.resize(600, 300)
self.model = QSqlTableModel(self)
self.model.setTable(table)
self.model.setSort(ID, Qt.AscendingOrder)
self.model.setHeaderData(ID, Qt.Horizontal, QVariant(_("Id")))
self.model.setHeaderData(EMAIL, Qt.Horizontal, QVariant(_("Email")))
self.model.setHeaderData(WWW, Qt.Horizontal, QVariant(_("WWW")))
self.model.setHeaderData(PACKAGE, Qt.Horizontal, QVariant(_("Package")))
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()
addButton = QPushButton(_("&Add"))
addButton.setIcon(QIcon(":/add.png"))
deleteButton = QPushButton(_("&Delete"))
deleteButton.setIcon(QIcon(":/delete.png"))
okButton = QPushButton(_("&OK"))
okButton.setIcon(QIcon(":/quit.png"))
addButton.setFocusPolicy(Qt.NoFocus)
deleteButton.setFocusPolicy(Qt.NoFocus)
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(addButton)
buttonLayout.addWidget(deleteButton)
buttonLayout.addStretch()
buttonLayout.addWidget(okButton)
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addLayout(buttonLayout)
self.setLayout(layout)
self.connect(addButton, SIGNAL("clicked()"), self.addRecord)
self.connect(deleteButton, SIGNAL("clicked()"), self.deleteRecord)
self.connect(okButton, SIGNAL("clicked()"), self.accept)
self.setWindowTitle(name)
def addRecord(self):
row = self.model.rowCount()
self.model.insertRow(row)
index = self.model.index(row, EMAIL)
self.view.setCurrentIndex(index)
self.view.edit(index)
def deleteRecord(self):
index = self.view.currentIndex()
if not index.isValid():
return
self.model.removeRow(index.row())
self.model.submitAll()
def accept(self):
QDialog.accept(self)
示例6: MainWindow
# 需要导入模块: from PyQt4.QtSql import QSqlTableModel [as 别名]
# 或者: from PyQt4.QtSql.QSqlTableModel import removeRow [as 别名]
#.........这里部分代码省略.........
query.prepare("DELETE FROM magaslave WHERE id = :val")
for i in selrows:
if i.isValid():
query.bindValue(":val", QVariant(i.data().toInt()[0]))
query.exec_()
QSqlDatabase.database().commit()
self.sModel.revertAll()
self.mmUpdate()
def delScaffRecord(self):
if not self.db.isOpen():
self.statusbar.showMessage(
"Database non aperto...",
5000)
return
row = self.mapper.currentIndex()
if row == -1:
self.statusbar.showMessage(
"Nulla da cancellare...",
5000)
return
record = self.mModel.record(row)
id = record.value(ID).toInt()[0]
scaff = record.value(SCAFF).toString()
if(QMessageBox.question(self, "Cancella Scaffale",
"Vuoi cancellare lo scaffale: {0} ?".format(scaff),
QMessageBox.Yes|QMessageBox.No) ==
QMessageBox.No):
self.statusbar.showMessage(
"Cancellazione scaffale annullata...",
5000)
return
# cancella scaffale
self.mModel.removeRow(row)
self.mModel.submitAll()
if row + 1 >= self.mModel.rowCount():
row = self.mModel.rowCount() - 1
self.mapper.setCurrentIndex(row)
if self.mModel.rowCount() == 0:
self.scaffLineEdit.setText(QString(""))
# cancella tutti gli articoli che si riferiscono
# allo scaffale cancellato
self.sModel.setFilter("mmid=%s" % id)
self.sModel.select()
self.sModel.removeRows(0, self.sModel.rowCount())
self.sModel.submitAll()
self.statusbar.showMessage(
"Cancellazione eseguita...",
5000)
self.mmUpdate()
def restoreTablesSettings(self):
settings = QSettings(self)
if self.saveTableGeometryCheckBox.isChecked():
# per la tabella slave
for c in range(1, self.sModel.columnCount()-1):
width = settings.value("Settings/sTableView/%s" % c,
QVariant(60)).toInt()[0]
self.sTableView.setColumnWidth(c,
width if width > 0 else 60)
# per la tabella find
for c in range(1, self.fModel.columnCount()):
width = settings.value("Settings/fTableView/%s" % c,
QVariant(60)).toInt()[0]