PDA

View Full Version : How to select cells diagonally in table view when user drags mouse?



Hetal
13th May 2011, 09:19
I need to highlight cells of table diagonally i.e. (0,0), (1,1) and so on, in table view,
when user clicks a cell and then start dragging mouse.

Your suggestions will be helpful.

Thanks.

JoZCaVaLLo
13th May 2011, 10:13
If you already can drag... dragEnterEvent should do the work your asking for...

if you are looking for dragging...
http://doc.qt.nokia.com/latest/model-view-programming.html#using-drag-and-drop-with-item-views

Hetal
13th May 2011, 10:53
I am asking for higlighting the item in view. It don't want to drag the cell of a table.

By default Table view marks all the cell with blue color when we move the mouse keeping left button down.
I need to change this behaviour so that only diagonal cells are highlighted when we move the mouse keeping left button down.

Hope I am able to make it clear now.

Thanks.

JoZCaVaLLo
13th May 2011, 11:27
Ok... you mean selection...
I think you'll have to reimplement those Protected Functions



virtual void mousePressEvent ( QMouseEvent * event )
{
... memorize the cell the mouse is currently on ...
}
virtual void mouseReleaseEvent ( QMouseEvent * event )
{
... highlight cells between memorized cell of above and the one you are currently on...
}

MarekR22
13th May 2011, 11:33
take a look on example http://doc.qt.nokia.com/latest/itemviews-chart.html

Hetal
20th May 2011, 13:29
I tried to override the setSelection method of view to get the diagonal selection, it gives a rect of selection. But How to find out QModelIndex which falls in this rect ?

My model is very big. So scanning each row and column and get its size with visualRect() and then see if it falls in this region may work but as the model is very big it will be a performance problem, its good if I am able to find the items that falls in this rect with minimum scanning of model.

Any ideas?

Thanks.

Hetal
23rd May 2011, 10:35
Hi,
Here is the solution for this:

void MyTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{
QItemSelectionModel* selectionModel = this->selectionModel ();

int rows = model()->rowCount(rootIndex());
int columns = model()->columnCount(rootIndex());
QModelIndexList indexes;

for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
QModelIndex index = model()->index(row, column);
QRect r = visualRect(index);
if(!r.intersect(rect).isEmpty())
indexes.append(index);
}
}

QItemSelection itemSelection;
QModelIndex index;
foreach(index ,indexes)
{
if(index.row() == index.column())
itemSelection.append(QItemSelectionRange(index));
}

selectionModel->select(itemSelection,QItemSelectionModel::Select);
}

Also is there any better way to find the indexes which falls within the selected rectangle, so that I do not need to scan entire model for that.

Thanks,
Hetal

d_stranz
24th May 2011, 04:36
Why do you scan ALL rows and ALL columns? If you are only interested in items where row index == column index, then simply make one loop:



int rows = model()->rowCount(rootIndex());
int columns = model()->columnCount(rootIndex());
int maxIndex = min( rows, columns ); // in case your table is not square
for ( int index = 0; index < maxIndex; ++index )
{
QModelIndex modelIndex = model()->index( index, index );
QRect r = visualRect( modelIndex );
if ( !r.intersect( rect ).isEmpty() )
itemSelection.append( QItemSelectionRange( modelIndex ) );
}


Please use "code" tags when posting source code.