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.
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.
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...ith-item-views
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.
Ok... you mean selection...
I think you'll have to reimplement those Protected Functions
Qt Code:
{ ... memorize the cell the mouse is currently on ... } { ... highlight cells between memorized cell of above and the one you are currently on... }To copy to clipboard, switch view to plain text mode
take a look on example http://doc.qt.nokia.com/latest/itemviews-chart.html
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.
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
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:
Qt Code:
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 ) { if ( !r.intersect( rect ).isEmpty() ) }To copy to clipboard, switch view to plain text mode
Please use "code" tags when posting source code.
Bookmarks