[Qt-jambi-interest] Manual Selection in QTableView does not select and does not repaint
Curt Nowak
cnowak at bwl.uni-hildesheim.de
Wed Aug 27 14:04:37 CEST 2008
Hi Gunnar,
Thanks a lot for your help. It does work now.
My second problem was that when debugging, "this.currentIndex()" would sometimes return "null". But I could fix that myself. If anyone's interested, I attached the working sample. However, it's a minor pitty that now, when the user clicks on the table the "on_selectionChanged()"-method is invoked twice. If anybody knows a more elegant way to do this, please let me know.
Curt
import java.util.Arrays;
import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QItemSelection;
import com.trolltech.qt.gui.QMouseEvent;
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), SelectionFlag.Current);
}
this.selectionModel().currentChanged.connect(this, "on_selectionChanged()");
}
@Override
protected void mouseReleaseEvent(QMouseEvent event)
{
super.mouseReleaseEvent(event);
on_selectionChanged();
}
}
________________________________
Von: Gunnar Sletta [mailto:gunnar at trolltech.com]
Gesendet: Mi 27.08.2008 12:15
An: Curt Nowak
Cc: qt-jambi-interest at trolltech.com
Betreff: Re: [Qt-jambi-interest] Manual Selection in QTableView does not select and does not repaint
Curt Nowak wrote:
> 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.)
Hi Curt,
The problem is that QTableView does a reset of the selection in the
mouseReleaseEvent() based on the current mouse position in the view. You
need to reimplement mouseReleaseEvent() to keep on selecting what you want.
> [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.
I don't get what you mean here. The index is always valid when you have
the mouse inside the view.
best regards,
Gunnar
More information about the Qt-jambi-interest
mailing list