Results 1 to 3 of 3

Thread: QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)

    Hi,
    I have an ui->tableView (set up with designer) and subclassed a QStandardItemModel to attach it, along with aQSortFilterProxyModel.

    I want to make the items right-cklickable for a context menu. It nearly works - apart from a very annoying offset in the heigth that amounts to roughly half of a row's height. When I right-click an item in it's bottom half, the item below is given back.

    In the dialog's constructor:
    Qt Code:
    1. this->proxyModel = new QSortFilterProxyModel();
    2. proxyModel->setSourceModel(fotoImport); //foto Import is the model
    3. proxyModel->setDynamicSortFilter(true);
    4. ui->tableView->setModel(proxyModel);
    5. ui->tableView->resizeColumnsToContents();
    6. ui->tableView->resizeRowsToContents();
    7. ui->tableView->verticalHeader()->hide();
    8. ui->tableView->setSortingEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    In the dialog's contextMenuEvent (QContextMenuEvent *e) I try this:
    Qt Code:
    1. QPoint pos;
    2. pos=ui->tableView->mapFrom(this,e->pos());
    3. int row=ui->tableView->rowAt(pos.y());
    4. int column=ui->tableView->columnAt(pos.x());
    5. if (row==-1)return;
    6. if (column==-1)return;
    7. QModelIndex index=ui->tableView->indexAt(pos);
    8. QModelIndex proxyIndex;
    9. proxyIndex=this->proxyModel->mapToSource(index);
    10. qDebug()<<"map from this: "<<fotoImport->itemFromIndex(proxyIndex)->text();
    To copy to clipboard, switch view to plain text mode 

    It furthermore might be important that my model contains small pictures.

    I've set them up this way in the model:
    Qt Code:
    1. item->setData(myPixmap.scaledToHeight(50,Qt::SmoothTransformation),Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    Instead of
    pos=ui->tableView->mapFrom(this,e->pos());
    I've also tried
    pos=ui->tableView->mapFromParent(e->pos());
    which gives equal results and
    pos=ui->tableView->mapFrom(ui->tableView, e->pos());

    as well as all other mapFromXXX and mapToXXX possibilities.

    The columnAt() works fine.

    Any idea what I am doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)

    The position is given relative to the view's viewport(), not to the view itself. Thus the "annoying offset".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    sedi (18th July 2012)

  4. #3
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default SOLVED: Re: QTableView: rowAt() gives wrong row number (aim: right-cklick enabling)

    Smack dab in the middle, wysota - as usual. Thank you!
    Qt Code:
    1. pos=ui->tableView->viewport()->mapFromGlobal(e->globalPos());
    To copy to clipboard, switch view to plain text mode 
    did the trick.

Similar Threads

  1. Replies: 2
    Last Post: 21st June 2012, 13:43
  2. QTableView row number from mouse position
    By lukass in forum Newbie
    Replies: 1
    Last Post: 20th February 2010, 14:04
  3. Enabling scroll bars in disabled QTableView
    By Koas in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2009, 10:39
  4. Count the number of rows in a QTableView
    By grub87 in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2009, 16:31
  5. wrong sort in qtableview
    By toem in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2009, 10:30

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.