简体   繁体   中英

Qt. How to handle double click event

I cannot to handle double click event. I try to do this using following code

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&)
{
    ...
}

OnDc is not calling when double click happens. What did I do wrong?

You should use void QWidget::mouseDoubleClickEvent ( QMouseEvent * event ) [virtual protected]

You can override 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 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM