问题
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