[Qt-jambi-interest] Manual Selection in QTableView does not select and does not repaint
Curt Nowak
cnowak at bwl.uni-hildesheim.de
Tue Aug 26 18:20:24 CEST 2008
Hi all,
I'm trying to implement a QTableView in which two consecutive rows are always marked as selected. For example: If the user clicks on row 1 then row 1 and row 2 should both be marked.
I created a small example that shows where I'm stuck.
There are two problems that I am having:
[1] The blue marks on the table change only when it re-gains the window focus. (Click on a different window and then back on the application to see what I mean.)
[2] I realize that in "on_selectionChanged" I always get a null-Index. The commented line in the same method aims to correct that but I can't figure out the correct settings.
Any hint would be appreciated.
Curt
Here's my example code:
import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QItemSelection;
import com.trolltech.qt.gui.QStandardItem;
import com.trolltech.qt.gui.QStandardItemModel;
import com.trolltech.qt.gui.QTableView;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QItemSelectionModel.SelectionFlag;
public class QTableViewExample extends QTableView{
public static void main(String[] args) {
QApplication.initialize(args);
QTableViewExample example = new QTableViewExample(null);
example.show();
QApplication.exec();
}
public QTableViewExample(QWidget parent)
{
super(parent);
setSelectionMode(SelectionMode.ContiguousSelection);
setSelectionBehavior(SelectionBehavior.SelectRows);
QStandardItemModel model = new QStandardItemModel();
String[] header = { "A", "B", "C"};
model.setHorizontalHeaderLabels(Arrays.asList(header));
for(int i=0; i<5; i++){
for(int j=0; j<3; j++){
String text = String.valueOf(i) + ", " + String.valueOf(j);
QStandardItem item = new QStandardItem(text);
model.setItem(i, j, item);
}
}
setModel(model);
this.selectionModel().currentChanged.connect(this, "on_selectionChanged()");
}
@SuppressWarnings("unused")
private void on_selectionChanged()
{
QModelIndex currentIndex = this.currentIndex();
int row;
if(currentIndex == null){
row = 0;
}
else{
// never actually mark the last row
row = Math.min(currentIndex.row(), model().rowCount()-2);
}
// avoid multiple loops
this.selectionModel().currentChanged.disconnect(this, "on_selectionChanged()");
{
this.selectionModel().clear();
QModelIndex indexTopLeft = this.model().index(row, 0);
QModelIndex indexBottomRight = this.model().index(row+1, 2);
QItemSelection selection = new QItemSelection(indexTopLeft, indexBottomRight);
this.selectionModel().select(selection, SelectionFlag.ClearAndSelect);
// this.selectionModel().setCurrentIndex(this.model().index(row, 0), whatValue??);
}
this.selectionModel().currentChanged.connect(this, "on_selectionChanged()");
}
}
More information about the Qt-jambi-interest
mailing list