Call accept() when user double clicks on QInputDialog's QListWidget.
-
Automatically calling accept() on double clicking an item in the listview of a QInputDialog with QInputDialog::UseListViewForComboBoxItems option set turns out to be surprisingly difficult.
I'm subclassing QInputDialog to catch and filter QEvent::MouseButtonDblClick using the approach discussed here http://qt-project.org/forums/viewthread/3055/
@void MyInputDialog::childEvent(QChildEvent *e) {
if (e->child()->isWidgetType()) {
if (e->type() == QEvent::ChildAdded)
addChild(e->child());
else if (e->type() == QEvent::ChildRemoved)
removeChild(e->child());
}QWidget::childEvent(e);
}
void MyInputDialog::addChild(QObject* pObject) {
if (pObject && pObject->isWidgetType()) {
pObject->installEventFilter(this);qDebug() << pObject->metaObject()->className(); const QObjectList& childList = pObject->children(); for (QObjectList::const_iterator it = childList.begin(); it != childList.end(); ++it) addChild(*it); for (QObjectList::const_iterator it = childList.begin(); it != childList.end(); ++it) addChild(*it); }
}
void MyInputDialog::removeChild(QObject* pObject) {
if (pObject && pObject->isWidgetType()) {
pObject->removeEventFilter(this);const QObjectList& childList = pObject->children(); for (QObjectList::const_iterator it = childList.begin(); it != childList.end(); ++it) removeChild(*it); }
}
bool MyInputDialog::eventFilter(QObject obj, QEvent e) {
switch (e->type()) {
case QEvent::ChildAdded:
{
QChildEvent ce = (QChildEvent)e;
addChild(ce->child());
}
break;case QEvent::ChildRemoved: { QChildEvent* ce = (QChildEvent*)e; removeChild(ce->child()); } break; case QEvent::MouseButtonDblClick: { if (obj != this) { accept(); e->accept(); return true; } } break; } return QInputDialog::eventFilter(obj, e);
}@
However, no double click event arrives in eventFilter() for the QListWidget shown.
The qDebug() statement in addChild() just prints the following:
@QWidget
QWidget
QWidget
QWidget
QWidget
QWidget
QWidget
QWidget
QWidget
QWidget
QWidget
QWidget@Any ideas on why the above does not work? I suspect its a timing issue as discussed in the last comment of the referenced post, but I have no idea on how to get around that reliably.
Is there an easier way to do this?