Qt: TableWidget ItemAt () acts weird

I am working on a Windows application where in a dialog box I request some data from Postgres and manually show the output as a table widget.

m_ui->tableWidget->setRowCount(joinedData.count());
for(int i=0; i<joinedData.count(); i++) //for each row
{
        m_ui->tableWidget->setItem(i, 0, new QTableWidgetItem(joinedData[i].bobin.referenceNumber));
        m_ui->tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(joinedData[i].bobin.width)));
        m_ui->tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getHole())));
        m_ui->tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getLessThanZeroFive())));                      m_ui->tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroFive_to_zeroSeven())));
        m_ui->tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroFive_to_zeroSeven_repetitive())));
        m_ui->tableWidget->setItem(i, 6, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getZeroSeven_to_Three())));
        m_ui->tableWidget->setItem(i, 7, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getThree_to_five())));
        m_ui->tableWidget->setItem(i, 8, new QTableWidgetItem(QString::number(joinedData[i].tolerance.getMoreThanFive())));
 }

Also, based on the row and column information, I draw some of these tablewidgetitems on some colors, but I don’t think it matters.

I overridden QDialog contextMenuEvent to get the row and column coordinates of the tableWidgetItem with the right mouse button:

void BobinFlanView::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(m_ui->tableWidget);
    //standard actions
    menu.addAction(this->markInactiveAction);
    menu.addAction(this->markActiveAction);
    menu.addSeparator();
    menu.addAction(this->exportAction);
    menu.addAction(this->exportAllAction);

    //obtain the rightClickedItem
    QTableWidgetItem* clickedItem = m_ui->tableWidget->itemAt(m_ui->tableWidget->mapFromGlobal(event->globalPos()));



    // if it a colored one, add some more actions
    if (clickedItem && clickedItem->column()>1 && clickedItem->row()>0)
    {
        //this is a property, i'm keeping this for a later use
        this->lastRightClickedItem = clickedItem; 
        //debug purpose:
        QMessageBox::information(this, "", QString("clickedItem = %1, %2").arg(clickedItem->row()).arg(clickedItem->column()));
        QMessageBox::information(this, "", QString("globalClick = %1, %2\ntransformedPos = %3, %4").arg(event->globalX()).arg(event->globalY())
                                 .arg(m_ui->tableWidget->mapFromGlobal(event->globalPos()).x()).arg(m_ui->tableWidget->mapFromGlobal(event->globalPos()).y()));

        menu.addSeparator();

        menu.addAction(this->changeSelectedToleranceToUygun);
        menu.addAction(this->changeSelectedToleranceToUyar);
        menu.addAction(this->changeSelectedToleranceToDurdurUyar);

        //... some other irrevelant 'enable/disable' activities

    menu.exec(event->globalPos());
}

The problem is that when I right-click on the same element, I get the same global coordinates, but accidentally distinguish information about the columns of the row. For example, the global pos is exactly 600,230, and a pair of row columns is randomly assigned to (5.3) and (4.3). I mean that?!

, (, 13, ) " (clickedItem & & click clickItem- > column() > 1 && clickedItem → row() > 0)", , , 'clickedItem' null.

cpp-h-ui, .

.

+3
2

:

QTableWidgetItem* clickedItem = m_ui->tableWidget->itemAt(event->pos());

, , . -, itemAt, tableWidget->viewport()->mapFromGlobal.

+3

, , .

itemAt() QDropEvent:: pos() , 1.

, , , , , , . QTableWidget viewport() → mapFromParent ([drop pos]), QTableWidget.

, A LOT!

0

Source: https://habr.com/ru/post/1720282/


All Articles