Enabling a button only if two fields are filled
How to implement such functionality that a button is enabled only if 开发者_Go百科two lineEdit's are filled with text?
You want to monitor both line edits for changes:
connect(lineEdit1, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton()));
connect(lineEdit2, SIGNAL(textChanged(const QString&)), SLOT(checkShouldEnableButton()));
And then you need to enable/disable the button when text is present in both:
void YourWidget::checkShouldEnableButton() {
button->setEnabled(
!lineEdit1->text().isEmpty() && !lineEdit2->text().isEmpty()
);
}
If you only care about user edits, you can use the textEdited(const QString&) signal instead of the textChanged signal.
Connect both widgets' textChanged
signal to the same slot, which calls
button -> setEnabled (edit1 -> text .size () && edit2 -> text .size ())
精彩评论