到目前为止,我有这段代码,它运行良好:
QObject::connect(mListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(itemDoubleClicked(QListWidgetItem*)));
QObject::connect(mListWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked(QListWidgetItem*)));
问题是,每次双击某个项目时,都会执行itemClicked
个插槽。
如果用户双击某个项目,我可以屏蔽itemClicked
个广告位吗?那么itemDoubleClicked
会被执行吗?
答案 0 :(得分:1)
实际上双击某个项会产生itemClicked
和itemDoubleClicked
信号:点击+点击。您可以使用计时器并在超时后检查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
}