qt - What is the signal for when a widget loses focus? -
in dialog, when tab key pressed, focus changes widget. in qt, there signal when widget loses focus? can use check if input valid or not? if not, can set focus , ask user re-input?
there's no signal if want know when widget has lost focus, override , reimplement void qwidget::focusoutevent(qfocusevent* event)
in widget. called whenever widget has lost focus. give focus widget, use qwidget::setfocus(qt::focusreason)
.
to validate input in qlineedit
or qcombobox
can subclass qvalidator
, implement own validator, or use 1 of existing subclasses, qintvalidator
, qdoublevalidator
, or qregexpvalidator
. set validator qlineedit::setvalidator(const qvalidator*)
, qcombobox::setvalidator(const qvalidator*)
respectively.
if want validate contents of modal dialog box, 1 way override qdialog::exec()
implementation this:
int mydialog::exec() { while (true) { if (qdialog::exec() == qdialog::rejected) { return qdialog::rejected; } if (validate()) { return qdialog::accepted; } } } bool mydialog::validate() { if (lineedit->text().isempty()) { qmessagebox::critical(this, "invalid value", "the specified value not valid"); lineedit->setfocus(); lineedit->selectall(); return false; } return true; }
it not allow user close dialog ok button or other button accepted role unless contents of dialog validated. in example assume dialog has qlineedit
named lineedit
, validate
function make sure content not empty. if is, set focus qlineedit
, show dialog again.
Comments
Post a Comment