C++ Qt。如何处理双击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24518228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:45:32  来源:igfitidea点击:

Qt. How to handle double click event

c++qt

提问by Ufx

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?

发生双击时 OnDc 不会调用。我做错了什么?

回答by Ashot

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

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

You can override QMainWindow::mouseDoubleClickEvent

你可以覆盖 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 );
}