Results 1 to 3 of 3

Thread: Qt: TableWidget’s ItemAt() acting weirdly

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt: TableWidget’s ItemAt() acting weirdly

    Hi,

    i'm working on a windows application, where in a dialog i query some data from Postgres, and manually show the output in a table widget.

    Qt Code:
    1. m_ui->tableWidget->setRowCount(joinedData.count());
    2. for(int i=0; i<joinedData.count(); i++) //for each row
    3. {
    4. m_ui->tableWidget->setItem(i, 0, new QTableWidgetItem(joinedData[i].bobin.referenceNumber));
    5. m_ui->tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(joinedData[i].bobin.width)));
    6. m_ui->tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getHole())));
    7. m_ui->tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getLessThanZeroFive())));
    8. m_ui->tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroFive_to_zeroSeven())));
    9. m_ui->tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroFive_to_zeroSeven_repetitive())));
    10. m_ui->tableWidget->setItem(i, 6, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroSeven_to_Three())));
    11. m_ui->tableWidget->setItem(i, 7, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getThree_to_five())));
    12. m_ui->tableWidget->setItem(i, 8, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getMoreThanFive())));
    13. }
    To copy to clipboard, switch view to plain text mode 


    Also, based on row and column information, i paint some of these tablewidgetitems to some colors, but i don't think it's relevant.

    I reimplemented the QDialog's contextMenuEvent, to obtain the right clicked tableWidgetItem's row and column coordinates:

    (BobinFlanView is custom class derived from QDialog)
    Qt Code:
    1. void BobinFlanView::contextMenuEvent(QContextMenuEvent *event)
    2. {
    3. QMenu menu(m_ui->tableWidget);
    4. //standard actions
    5. menu.addAction(this->markInactiveAction);
    6. menu.addAction(this->markActiveAction);
    7. menu.addSeparator();
    8. menu.addAction(this->exportAction);
    9. menu.addAction(this->exportAllAction);
    10.  
    11. //obtain the rightClickedItem
    12. QTableWidgetItem* clickedItem = m_ui->tableWidget->itemAt(m_ui->tableWidget->mapFromGlobal(event->globalPos()));
    13.  
    14.  
    15.  
    16. // if it's a colored one, add some more actions
    17. if (clickedItem && clickedItem->column()>1 && clickedItem->row()>0)
    18. {
    19. //this is a property, i'm keeping this for a later use
    20. this->lastRightClickedItem = clickedItem;
    21. //debug purpose:
    22. QMessageBox::information(this, "", QString("clickedItem = %1, %2").arg(clickedItem->row()).arg(clickedItem->column()));
    23. QMessageBox::information(this, "", QString("globalClick = %1, %2\ntransformedPos = %3, %4").arg(event->globalX()).arg(event->globalY())
    24. .arg(m_ui->tableWidget->mapFromGlobal(event->globalPos()).x()).arg(m_ui->tableWidget->mapFromGlobal(event->globalPos()).y()));
    25.  
    26. menu.addSeparator();
    27.  
    28. menu.addAction(this->changeSelectedToleranceToUygun);
    29. menu.addAction(this->changeSelectedToleranceToUyar);
    30. menu.addAction(this->changeSelectedToleranceToDurdurUyar);
    31.  
    32. //... some other irrevelant 'enable/disable' activities
    33.  
    34. menu.exec(event->globalPos());
    35. }
    To copy to clipboard, switch view to plain text mode 


    The problem is, when i right click on the same item i get the same global coordinates, but randomly different row-column information. For instance, the global pos is exactly 600,230 but row-column pair is randomly (5,3) and (4,3). I mean, what?!

    Also, when i click to an item from the last to rows (later than 13, i guess) will never go into condition "if (clickedItem && clickedItem->column()>1 && clickedItem->row()>0)", i think it's mainly because 'clickedItem' is null.

    I'll be more than glad to share any more information, or even the full cpp-h-ui trio in order to get help.

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt: TableWidget’s ItemAt() acting weirdly

    Quote Originally Posted by emredog View Post
    Qt Code:
    1. QTableWidgetItem* clickedItem = m_ui->tableWidget->itemAt(m_ui->tableWidget->mapFromGlobal(event->globalPos()));
    To copy to clipboard, switch view to plain text mode 
    You probably have to add the offset caused by the scrolling of your table widget.

  3. #3
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt: TableWidget’s ItemAt() acting weirdly

    thanks.

    some guy from stackoverflow proposed a better solution, i would like to share it here:

    The problem is that you are trying to map the global position to the table widget position, without considering the scrollable area. To map the global position into something you can pass to itemAt, use
    Qt Code:
    1. tableWidget->viewport()->mapFromGlobal
    To copy to clipboard, switch view to plain text mode 
    .

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.