Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt. How to handle double click event

Tags:

c++

qt

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?

like image 575
Ufx Avatar asked Jul 01 '14 19:07

Ufx


1 Answers

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 );
}
like image 158
Ashot Avatar answered Oct 18 '22 01:10

Ashot