如何在Qt中的QTableview中找到我的按钮的行号

时间:2018-04-27 20:23:26

标签: c++ qt qt5 qtableview qitemdelegate

我有一个函数可以将QStyleOptionButton添加到QTableview中的某个列, 我希望能够在单击时获取每个按钮的行号,并将值存储在变量中。

这是我的代码delegate.cpp

DELETE tblTestImport.ID
FROM tblTestImport
WHERE tblTestImport.[ID] IN
    (SELECT tblTestImport.ID
    FROM tblTestImport 
    LEFT JOIN [unique] ON tblTestImport.ID = unique.LastOfID
    WHERE unique.LastOfID Is Null
    AND tblTestImport.ID IN (
        SELECT Last(tblTestImport.ID) AS LastOfID
        FROM tblTestImport
        GROUP BY tblTestImport.Url, tblTestImport.Kms, tblTestImport.Price, tblTestImport.Time)
    )

1 个答案:

答案 0 :(得分:4)

您必须使用QModelIndex:

bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    Q_UNUSED(model)
    if( event->type() == QEvent::MouseButtonRelease )
    {
        QMouseEvent * e = (QMouseEvent *)event;
        QPoint p = e->pos();

        QRect r = option.rect;//getting the rect of the cell
        QRect re(r.topRight() - QPoint(30, 0), QSize(30, 30));

        if(re.contains(p)){
            int row = index.row(); // <---- row
            int column = index.column(); // <---- column

            QDialog * d = new QDialog();
            d->setGeometry(0,0,100,100);
            d->show();
         }
    }
    return true;
}