[PyQt] Help with QTableView
Catriona Johnson
catriona.johnson at gmail.com
Tue Jan 29 22:22:21 GMT 2008
Thanks Brian
I tried that and the checkboxes are showing the right value but I am
unable to edit the checkbox only the datarole. What am I doing wrong??
Code sample below.
from PyQt4.QtGui import *
from PyQt4.QtSql import *
from PyQt4.QtCore import *
import sys
class MyModel(QSqlRelationalTableModel, QStandardItemModel):
def data(self, index, role=Qt.DisplayRole):
qv = QSqlRelationalTableModel.data(self, index, role)
if not index.isValid():
return QVariant()
elif index.column() in [1]:
if role == Qt.CheckStateRole:
it = QSqlRelationalTableModel.data(self, index, 0)
if it.toInt()[0] == 1:
return QVariant(Qt.Checked)
else:
return QVariant(Qt.Unchecked)
else:
return QVariant()
elif role == Qt.DisplayRole:
return qv
else:
return QVariant()
def flags(self, index):
if index.column() in [1]:
return Qt.ItemIsEnabled|Qt.ItemIsUserCheckable|Qt.ItemIsSelectable|Qt.ItemIsEditable
else:
return Qt.ItemIsEnabled|Qt.ItemIsEditable|Qt.ItemIsSelectable
def setData(self, index, value, role):
if index.isValid():
record = self.record(index.row())
if role == Qt.EditRole:
if value == Qt.Checked:
record.setValue(index.column(), QVariant(1))
else:
record.setValue(index.column(), QVariant(0))
self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index, index)
return True
return False
class Form(QDialog):
def __init__(self, parent=None):
super(Form,self).__init__(parent)
self.timeSeriesModel = MyModel(self) # links data to data aware widget
self.timeSeriesModel.setTable("TimeSeries")
self.timeSeriesModel.setRelation(2, QSqlRelation("AnimalMovement",
"AnimalMovementId", "AnimalMovement"))
self.timeSeriesModel.setData
self.timeSeriesModel.select()
self.tvTest = QTableView(self)
self.tvTest.setGeometry(QRect(10,0,1400,200))
self.tvTest.setModel(self.timeSeriesModel)
self.tvTest.setSelectionMode(QTableView.SingleSelection)
self.tvTest.setSelectionBehavior(QTableView.SelectRows)
self.tvTest.setItemDelegate(QSqlRelationalDelegate(self.tvTest))
def createConnection():
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(":memory:")
if not db.open():
return False
query = QSqlQuery()
query.exec_("CREATE TABLE AnimalMovement("
"AnimalMovementId INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"AnimalMovement VARCHAR(20) NOT NULL"
")")
query.prepare("INSERT INTO AnimalMovement(AnimalMovement) VALUES (?)")
animalMovementData = ['Swim to', 'Swim away', 'Swim parallel']
for animalMovement in animalMovementData:
query.addBindValue(QVariant(QString(animalMovement)))
query.exec_()
query.exec_("CREATE TABLE TimeSeries("
"TimeSeriesId INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"First INTEGER NULL, "
"AnimalMovementId INTEGER NULL CONSTRAINT fk_animal_movement
REFERENCES AnimalMovement(AnimalMovementId) "
")")
query.prepare("INSERT INTO TimeSeries(First, AnimalMovementId) VALUES (?, ?)")
timeSeriesData = [[1, 1], [0, 2], [1, 2]]
for first, animalMovementId in timeSeriesData:
query.addBindValue(QVariant(first))
query.addBindValue(QVariant(animalMovementId))
query.exec_()
return True
if __name__ == "__main__":
app = QApplication(sys.argv)
if not createConnection():
QMessageBox.warning(None, "Database", QString("Database error:
%l").arg(db.lastError().text()))
sys.exit(1)
form = Form()
form.show()
sys.exit(app.exec_())
On 26/01/2008, Brian Kelley <kelley at eyesopen.com> wrote:
> in the model's data method:
>
> def data(index, role):
> ...
> if role == QtCore.Qt.CheckStateRole:
> return QtCore.Qt.Checked # or UnChecked
>
> Note you can have a checkbox along with the datarole as well.
>
> On Jan 18, 2008, at 12:10 AM, Catriona Johnson wrote:
>
> > Hello
> >
> > How do I implement a checkbox in a QTableView column that
> > retreives/stores values in a database?
> >
> > Also, I have one model with columns say abc and I have two QTableViews
> > of this model that show the columns in different orders - bca and cab.
> > How do I change the column order in the QTableView?
> >
> > Thanks
> > _______________________________________________
> > PyQt mailing list PyQt at riverbankcomputing.com
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
>
More information about the PyQt
mailing list