[Qt-interest] QTableView Selection

Kenneth Beck nekkceb at comcast.net
Mon Mar 23 06:13:43 CET 2009


Kenneth Beck wrote:
> I have a very simple data model, amenable to showing in a table, I 
> think, but the behavior of table view is not acting consistently, or at 
> least not the way I expect. First, I set Behavior and Mode to only allow 
> the user to select individual complete rows:
>       periodsList->setSelectionBehavior(QTableView::SelectRows);
>       periodsList->setSelectionMode(QTableView::SingleSelection);
> Now, when the table appears, the only way to select whole rows is to 
> mouse click on a tiny rectangle that appears to the left of the row. 
> clicking on any one cell does NOT select the row (the behavior I want).
> What am I doing wrong here?
> 
> Second, I need to operate on the selected row. I have tried both of the 
> approaches below, they both return 0 for size of the returned list, when 
> there is clearly an entire row selected:
> 
> QModelIndexList milst0=periodsList->selectionModel()->selectedRows(1);
> QModelIndexList 
> milst0=periodsList->selectionModel()->selection().indexes();
> 
> I have also tried
> QModelIndex ndx=periodsList->selectionModel()->currentIndex();
> which does return a valid index, but it may not be in my selected row, 
> it may be in another cell that the user just clicked!
> 
> Anything obvious wrong here? Should I be using a TreeView?
> 
> Qt 4.5.0, Windows XP, Visuall C++ 2003.
Found the problem: I had re-defined the flags() method, and in there I 
did not add Qt::ItemIsEnabled!!!
I also switched to TreeView, it seems to behave much better, as I do not 
need the vertical header.



More information about the Qt-interest-old mailing list