QListWidget itemClicked和itemDoubleClicked - 双击时阻止单击

时间:2014-03-03 09:04:39

标签: c++ qt signals-slots qlistwidget

到目前为止,我有这段代码,它运行良好:

QObject::connect(mListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemDoubleClicked(QListWidgetItem*)));
QObject::connect(mListWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked(QListWidgetItem*)));

问题是,每次双击某个项目时,都会执行itemClicked个插槽。

如果用户双击某个项目,我可以屏蔽itemClicked个广告位吗?那么itemDoubleClicked会被执行吗?

2 个答案:

答案 0 :(得分:1)

实际上双击某个项会产生itemClickeditemDoubleClicked信号:点击+点击。您可以使用计时器并在超时后检查itemDoubleClicked之后是否发生itemClicked信号,如果是,则忽略itemClicked信号。

答案 1 :(得分:0)

感谢vahancho的想法,使用计时器。这是我的解决方案:

<强> YourClass.h

private:
    QListWidgetItem* mSingleClickedItem;
    bool mDoubleClicked;

private slots:
    void itemClickedTimeout();

<强> YourClass.cpp

void YourClass::itemClicked(QListWidgetItem* listWidgetItem) {
    if (!mDoubleClicked) {
        QTimer::singleShot(300, this, SLOT(itemClickedTimeout()));
        // use QApplication::doubleClickInterval() instead of 300
        mSingleClickedItem = listWidgetItem;
    }
}

void YourClass::itemClickedTimeout() {
    if (!mDoubleClicked) {
        // do something, listitem has been clicked once
    } else mDoubleClicked = false;
}

void YourClass::itemDoubleClicked(QListWidgetItem* listWidgetItem) {
    mDoubleClicked = true;

    // do something, listitem has been clicked twice
}