Hi, I'm trying to write an alternative to qdataFrameModel that has the same speed, but has a more standard treatment of roles. I have the following. Two questions though:<br><br>1) The items get drawn with a checkbox next to them. I make sure the flags() method avoids Qt$Qt$ItemIsUserCheckable, but it is still there. Any idea what I'm doing wrong/missing?<br>
<br>2) In the data() method, the Qt docs say I should return an invalid QVariant instead of 0. I return NA. Is that right? I also wonder why I can't just call super to get the default data for the role from the parent.<br>
<br>Thanks for any help, --John<br><br><br>library(qtbase)<br><br>n <- 10 ## or even 1e6<br>Df <- data.frame(a=1:n, b= rnorm(n))<br><br>qsetClass("DfModel", Qt$QAbstractTableModel, function(parent=NULL) {<br>
super(parent)<br> this$dataframename <- "Df" ## generalize after testing<br>})<br><br>qsetMethod("Df", DfModel, function() {<br> get(this$dataframename, envir=.GlobalEnv)<br>})<br><br>qsetMethod("rowCount", DfModel, function(index) nrow(this$Df()))<br>
qsetMethod("columnCount", DfModel, function(index) ncol(this$Df()))<br><br>qsetMethod("data", DfModel, function(index, role) {<br> d <- this$Df()<br> if(index$isValid()) {<br> if(role == Qt$Qt$DisplayRole)<br>
out <- as.character(d[index$row() + 1, index$column() + 1])<br> else if(role == Qt$Qt$EditRole)<br> out <- as.character(d[index$row() + 1, index$column() + 1])<br> else <br> out <- NA<br> ## super("data", index, role) ## why does this fail?<br>
}<br> return(out)<br>})<br><br>## coerce value to fit into x<br>fitIn <- function(x, value) UseMethod("fitIn")<br>fitIn.default <- function(x, value) value<br>fitIn.numeric <- function(x, value) as.numeric(value)<br>
fitIn.integer <- function(x, value) as.integer(value)<br><br>qsetMethod("setData", DfModel, function(index, value, role) {<br><br> if(index$isValid() && role == Qt$Qt$EditRole) {<br> d <- this$Df()<br>
x <- d[, index$column() + 1]<br> d[index$row() + 1, index$column() + 1] <- fitIn(x,value)<br><br> assign(this$dataframename, d, envir=.GlobalEnv)<br> <br> dataChanged(index, index)<br> return(TRUE)<br>
} else {<br> return(FALSE)<br> }<br>})<br><br>qsetMethod("flags", DfModel, function(index) {<br> if(!index$isValid()) {<br> return(Qt$Qt$ItemIsEnabled)<br> } else {<br> curFlags <- super("flags", index)<br>
if(curFlags & Qt$Qt$ItemIsUserCheckable)<br> curFlags <- curFlags - Qt$Qt$ItemIsUserCheckable # try to remove checkable<br> return(curFlags | Qt$Qt$ItemIsEditable)<br> }<br>})<br><br>## test it<br>model <- DfModel()<br>
<br>view <- Qt$QTableView()<br>view$setModel(model)<br><br>view$setEditTriggers(Qt$QAbstractItemView$AllEditTriggers)<br><br>view$show()<br>view$raise()<br><br clear="all"><br>