我正在使用Windows应用程序,在该对话框中,我在对话框中查询Postgres的一些数据,并在表小部件中手动显示输出。

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())));
 }

另外,基于行和列的信息,我将其中一些tablewidgetitems绘制为某些颜色,但我认为这并不重要。

我重新实现了QDialog的contextMenuEvent,以获得右键单击的tableWidgetItem的行和列坐标:
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's 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());
}

问题是,当我右键单击同一项目时,我会获得相同的全局坐标,但会随机获得不同的行列信息。例如,全局pos正好是600,230,但行列对是随机(5,3)和(4,3)。我的意思是什么?!

另外,当我从最后一行到行(大于13)单击某个项目时,它永远不会进入条件“如果(clickedItem && clickedItem-> column()> 1 && clickedItem-> row()> 0)” ,我认为主要是因为'clickedItem'为null。

为了获得帮助,我将非常高兴分享任何更多信息,甚至是完整的cpp-h-ui三重奏。

非常感谢。

最佳答案

试试这个:

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

问题是您试图将全局位置映射到表窗口小部件位置,而不考虑可滚动区域。要将全局位置映射到可以传递给itemAt的内容中,请使用tableWidget->viewport()->mapFromGlobal

10-05 18:04