Qt的.如何处理双击事件

Ufx*_*Ufx 8 c++ qt

我无法处理双击事件.我尝试使用以下代码执行此操作

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected slots:
    void OnDc(const QModelIndex&);

private:
    Ui::MainWindow *ui;
};


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(this, SIGNAL(doubleClicked(const QModelIndex& )), this, SLOT(OnDc(const QModelIndex&)));
}

void MainWindow::OnDc(const QModelIndex&)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

双击发生时OnDc没有调用.我做错了什么?

Ash*_*hot 17

你应该使用void QWidget :: mouseDoubleClickEvent(QMouseEvent*event)[虚拟保护]

你可以覆盖 QMainWindow::mouseDoubleClickEvent

void MainWindow::mouseDoubleClickEvent( QMouseEvent * e )
{
    if ( e->button() == Qt::LeftButton )
    {
        ...
    }

    // You may have to call the parent's method for other cases
    QMainWindow::mouseDoubleClickEvent( e );
}
Run Code Online (Sandbox Code Playgroud)