Catch double click in QTableView's unused area

为君一笑 提交于 2020-04-07 06:55:33

问题


My application starts with an empty table, and I want to imlement different methods to add items. One should be by double-clicking the table's unused area (or "background") that is not occupied by any cells. When a cell is double-clicked, I want the default behavior.

I've found way to do this by re-implementing QAbstractScrollArea::mouseDoubleClickEvent() method in my TestTable class:

#include <QMouseEvent>
#include <QTableView>

class TestTable : public QTableView
{
  Q_OBJECT
  signals:
    void backgroundDoubleClickEvent(void);
  protected:
    void mouseDoubleClickEvent (QMouseEvent* e)
    {
      if (indexAt(e->pos()).isValid())
      {
          QTableView::mouseDoubleClickEvent(e);
      }
      else
      {
        e->accept();
        emit backgroundDoubleClickEvent();
      }

    }
};
  • This works, but is there a more elegant way of doing this without subclassing QTableView?
  • I'm not aware of any limitations of my current implementation. Are there obvious caveats?

回答1:


If you don't want to subclass QTableView, try installEventFilter



来源:https://stackoverflow.com/questions/24639407/catch-double-click-in-qtableviews-unused-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!