c++ - 如何在Qt中的QTableview中定位我的按钮的行号

标签 c++ qt qt5 qtableview qitemdelegate

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

这是我的代码delegate.cpp

MyDelegate::MyDelegate(QObject *parent)
     : QItemDelegate(parent)
 {

 }


 void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
     QStyleOptionButton button;
     QRect r = option.rect;//getting the rect of the cell
     int x,y,w,h;
     x = r.left() + r.width() - 30;//the X coordinate
     y = r.top();//the Y coordinate
     w = 30;//button width
     h = 30;//button height
     button.rect = QRect(x,y,w,h);
     button.text = "View log";
     button.state = QStyle::State_Enabled;

     QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);
 }

 bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
 {
     if( event->type() == QEvent::MouseButtonRelease )
     {
         QMouseEvent * e = (QMouseEvent *)event;
         int clickX = e->x();
         int clickY = e->y();

         QRect r = option.rect;//getting the rect of the cell
         int x,y,w,h;
         x = r.left() + r.width() - 30;//the X coordinate
         y = r.top();//the Y coordinate
         w = 30;//button width
         h = 30;//button height

         if( clickX > x && clickX < x + w )
             if( clickY > y && clickY < y + h )
             {
                // int row=tableview->rowAt(pos.y());
                 // int column=tableview->columnAt(pos.x());

                 QDialog * d = new QDialog();
                 d->setGeometry(0,0,100,100);
                // QTextBrowser *txt = new QTextBrowser(row, column);
                 d->show();
             }
     }

     return true;
 }

最佳答案

您必须使用 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;
}

关于c++ - 如何在Qt中的QTableview中定位我的按钮的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50069791/

相关文章:

c++ - 在不先插入标准 map 元素的情况下调用函数

c++ - 如何在模板容器类中实现复制构造函数和赋值运算符?

android - QScroller 动态滚动不流畅

visual-studio - 哪个更好? Qt Creator或Visual Studio IDE

c++ - 将 QAbstractTableModel 实现与自定义类的 QList 一起使用

c++ - QPrinter 在图像周围环绕文本

C++ 位移位

c++ - Adobe 亚当和夏娃 (C++ ASL) : how to bind Eve variable so to get it updated inside C++ application?

qt - QListWidget:更改选择而不触发selectionChanged

c++ - Qt5:toLongLong 失败