Results 1 to 8 of 8

Thread: How to select cells diagonally in table view when user drags mouse?

  1. #1
    Join Date
    May 2011
    Posts
    7
    Qt products
    Qt4

    Default How to select cells diagonally in table view when user drags mouse?

    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.

  2. #2
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to select cells diagonally in table view when user drags mouse?

    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

  3. #3
    Join Date
    May 2011
    Posts
    7
    Qt products
    Qt4

    Default Re: How to select cells diagonally in table view when user drags mouse?

    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.

  4. #4
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to select cells diagonally in table view when user drags mouse?

    Ok... you mean selection...
    I think you'll have to reimplement those Protected Functions

    Qt Code:
    1. virtual void mousePressEvent ( QMouseEvent * event )
    2. {
    3. ... memorize the cell the mouse is currently on ...
    4. }
    5. virtual void mouseReleaseEvent ( QMouseEvent * event )
    6. {
    7. ... highlight cells between memorized cell of above and the one you are currently on...
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to select cells diagonally in table view when user drags mouse?


  6. #6
    Join Date
    May 2011
    Posts
    7
    Qt products
    Qt4

    Default Re: How to select cells diagonally in table view when user drags mouse?

    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.

  7. #7
    Join Date
    May 2011
    Posts
    7
    Qt products
    Qt4

    Default Re: How to select cells diagonally in table view when user drags mouse?

    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

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,313
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to select cells diagonally in table view when user drags mouse?

    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:
    1. int rows = model()->rowCount(rootIndex());
    2. int columns = model()->columnCount(rootIndex());
    3. int maxIndex = min( rows, columns ); // in case your table is not square
    4. for ( int index = 0; index < maxIndex; ++index )
    5. {
    6. QModelIndex modelIndex = model()->index( index, index );
    7. QRect r = visualRect( modelIndex );
    8. if ( !r.intersect( rect ).isEmpty() )
    9. itemSelection.append( QItemSelectionRange( modelIndex ) );
    10. }
    To copy to clipboard, switch view to plain text mode 

    Please use "code" tags when posting source code.

Similar Threads

  1. Replies: 5
    Last Post: 3rd April 2010, 04:07
  2. selected cells' table
    By dreamer in forum Qt Programming
    Replies: 5
    Last Post: 19th June 2008, 08:59
  3. Table and validating cells
    By zorro68 in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2007, 23:22
  4. color of table cells
    By rickym in forum Qt Programming
    Replies: 1
    Last Post: 24th January 2007, 10:20
  5. table with combobox cells
    By mgurbuz in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2006, 12:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.