Hola pego el siguiente código para ver si alguien con mas experiencia me
pudiera ayudar.
tengo un tableview que se llena de una base de datos sqlite3.
agrego dos botones para poder seleccionar un registro y con el botón arriba
y abajo poder mover el registro seleccionado.
este es el código que estoy tratando de adecuar para dicha función.
from PyQt5 import QtGui, QtWidgets, QtSql, QtCore , uic
from functools import partial
class Dialog(QtWidgets.QDialog):
DOWN = 1
UP = -1
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.resize(800,600)
self.table = QtWidgets.QTableView(self)
self.table.setSelectionBehavior(self.table.SelectRows)
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("BASE.db")
db.open()
self.model = QtSql.QSqlQueryModel()
self.model.setQuery("select * from Tabla ", db)
self.table.setModel(self.model)
self.table.resizeColumnsToContents()
self.table.setSelectionMode(self.table.SingleSelection)
self.table.setSelectionBehavior(self.table.SelectRows)
db.close()
self.table.setModel(self.model)
self.upBtn = QtWidgets.QPushButton('Up', self)
self.downBtn = QtWidgets.QPushButton('Down', self)
self.mainLayout = QtWidgets.QVBoxLayout(self)
self.mainLayout.addWidget(self.table)
self.buttonLayout = QtWidgets.QHBoxLayout()
self.buttonLayout.addWidget(self.upBtn)
self.buttonLayout.addWidget(self.downBtn)
self.mainLayout.addLayout(self.buttonLayout)
self.upBtn.clicked.connect(partial(self.moveCurrentRow, self.UP))
self.downBtn.clicked.connect(partial(self.moveCurrentRow,
self.DOWN))
def moveCurrentRow(self, direction=DOWN):
if direction not in (self.DOWN, self.UP):
return
model = self.model
selModel = self.table.selectionModel()
selected = selModel.selectedRows()
if not selected:
return
items = []
indexes = sorted(selected, key=lambda x: x.row(),
reverse=(direction==self.DOWN))
for idx in indexes:
#items.append(model.itemFromIndex(idx))
items.append(model.indexInQuery(idx))#cambio
rowNum = idx.row()
newRow = rowNum+direction
if not (0 <= newRow < model.rowCount()):
continue
#rowItems = model.takeRow(rowNum)
rowItems = model.record(rowNum)
model.insertRow(newRow, rowItems)
selModel.clear()
for item in items:
selModel.select(item.index(), selModel.Select|selModel.Rows)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
d = Dialog()
d.show()
d.raise_()
app.exec_()
_______________________________________________
Python-es mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-es
FAQ: http://python-es-faq.wikidot.com/