Is it possible to add some behavior at the beginning of Qt-slot or before?
-
I use an auth form. Its implementation is not mine, so I cannot change it. The form has two fields for a login and password and also two buttons (ok + cancel).
Let's imagine the
authorizemethod looks like this (I replaced the code sending requests to a server with one condition inside theifstatement):void AuthForm::on_btn_ok_clicked() { if (loginLineEdit->text() == "auth" && passwordLineEdit->text() == "auth") emit accepted(); else emit rejected(); }I can connect to signals
AuthForm::acceptedorAuthForm::rejectedto get result of authorization. But the problem is the default implementation ofAuthFormdoes not validate form fields. Before the program send requests to authorize the user I want it to check if the form fields are empty or if the login match a mask.It works:
- user clicks
ok - calling of
on_btn_ok_clicked()immediately - sending requests to get user logged in or not
I want it to work:
- user clicks
ok - verifying if the login match mask and check if the fields are not empty
- using of default
on_btn_ok_clicked()implementation
I think it should be like a middle layer between signal and slot. But I don't know if it is possible and how to do it.
So, is it possible to add some behavior at the beginning of Qt-slot not modifying it?
- user clicks
-
You have to modify AuthForm to achieve what you want. How should it work otherwise? You don't have access to the ui elements outside AuthForm and even then you could not stop on_btn_ok_clicked() from being called without modifying the AuthForm code.
-
You can listen to textChanged signal of lineEdit and enable/disable the button accordingly:
ui->setupUi(this); connect(ui->loginLineEdit,&QLineEdit::textChanged,[this](const QString &text) { QPushButton* okBut=ui->buttonBox->button(QDialogButtonBox::Ok); // button OK enabled if the field is not empty if(text.isEmpty()) okBut->setEnabled(false); else okBut->setEnabled(true); }); -
Hi,
Even shorter and just as clear:
connect(ui->loginLineEdit,&QLineEdit::textChanged,[this](const QString &text) { QPushButton* okBut=ui->buttonBox->button(QDialogButtonBox::Ok); okBut->setDisabled(text.isEmpty()); });