Skip to content

Commit 2c14c1f

Browse files
committed
Merge #13791: gui: Reject dialogs if key escape is pressed
7bf22bf gui: Reject options dialog when key escape is pressed (João Barbosa) 4a43306 gui: Reject edit address dialog when key escape is pressed (João Barbosa) f7a5531 gui: Add GUIUtil::ItemDelegate with keyEscapePressed signal (João Barbosa) Pull request description: Currently `EditAddressDialog` and `OptionsDialog` don't close when the escape key is pressed. The `QDataWidgetMapper` instances prevents closing the dialogs because the escape key is used to reset the widgets values. More details and workarounds in https://stackoverflow.com/a/51487847 and http://qtramblings.blogspot.com/2010/10/qdatawidgetmapper-annoyances.html. The adopted solution is different from the above references. It turns out that `QDataWidgetMapper::setItemDelegate` sets the event filter for all mapped widgets. So in this PR the mapper's delegate are changed to a custom `GUIUtil::ItemDelegate` that offers the signal `keyEscapePressed`, which is connected to the `QDialog::reject` slot. Note that the installed event filter lets all events pass, so the current behaviour isn't changed, meaning that widgets values are reset in addition to closing the dialog. Tree-SHA512: 9c961d488480b4ccc3880a11a8f1824b65f77570ee8918c7302c62775a1a73e52ae988a31a55ffff87b4170ddbecf833c2f09b66095c00eb6854a4d43f030f1f
2 parents 660abc1 + 7bf22bf commit 2c14c1f

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/qt/editaddressdialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ EditAddressDialog::EditAddressDialog(Mode _mode, QWidget *parent) :
3939

4040
mapper = new QDataWidgetMapper(this);
4141
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
42+
43+
GUIUtil::ItemDelegate* delegate = new GUIUtil::ItemDelegate(mapper);
44+
connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &EditAddressDialog::reject);
45+
mapper->setItemDelegate(delegate);
4246
}
4347

4448
EditAddressDialog::~EditAddressDialog()

src/qt/guiutil.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <QDoubleValidator>
5050
#include <QFileDialog>
5151
#include <QFont>
52+
#include <QKeyEvent>
5253
#include <QLineEdit>
5354
#include <QSettings>
5455
#include <QTextDocument> // for Qt::mightBeRichText
@@ -927,4 +928,14 @@ void ClickableProgressBar::mouseReleaseEvent(QMouseEvent *event)
927928
Q_EMIT clicked(event->pos());
928929
}
929930

931+
bool ItemDelegate::eventFilter(QObject *object, QEvent *event)
932+
{
933+
if (event->type() == QEvent::KeyPress) {
934+
if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
935+
Q_EMIT keyEscapePressed();
936+
}
937+
}
938+
return QItemDelegate::eventFilter(object, event);
939+
}
940+
930941
} // namespace GUIUtil

src/qt/guiutil.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <QEvent>
1212
#include <QHeaderView>
13+
#include <QItemDelegate>
1314
#include <QMessageBox>
1415
#include <QObject>
1516
#include <QProgressBar>
@@ -232,6 +233,18 @@ namespace GUIUtil
232233

233234
typedef ClickableProgressBar ProgressBar;
234235

236+
class ItemDelegate : public QItemDelegate
237+
{
238+
Q_OBJECT
239+
public:
240+
ItemDelegate(QObject* parent) : QItemDelegate(parent) {}
241+
242+
Q_SIGNALS:
243+
void keyEscapePressed();
244+
245+
private:
246+
bool eventFilter(QObject *object, QEvent *event);
247+
};
235248
} // namespace GUIUtil
236249

237250
#endif // BITCOIN_QT_GUIUTIL_H

src/qt/optionsdialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
115115
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
116116
mapper->setOrientation(Qt::Vertical);
117117

118+
GUIUtil::ItemDelegate* delegate = new GUIUtil::ItemDelegate(mapper);
119+
connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
120+
mapper->setItemDelegate(delegate);
121+
118122
/* setup/change UI elements when proxy IPs are invalid/valid */
119123
ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
120124
ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));

0 commit comments

Comments
 (0)