PDA

View Full Version : Enabling buttons when lineEdit has text



k12yp70n
28th May 2009, 15:25
Hi there folks,

I wrote a piece of code that is supposed to put a button enabled (previously grayed out)

declaration:



public slots:
void allowinput();
signals:
void allfieldsfull();


implementation:



adddialog::adddialog() {
setupUi(this);
connect (this, SIGNAL (allfieldsfull()), this, SLOT (allowinput()));
connect (pushButton_Cancel, SIGNAL (clicked()), this, SLOT (close()));
if (lineEdit_name->text() != " ") {
emit allfieldsfull();
}
}

void adddialog::allowinput() {
pushButton_ok->setEnabled(true);
}


the problem is: it's not working!

any solutions? code samples would be very much appreciated.

Thanks in advance,
:)

Lykurg
28th May 2009, 15:39
any solutions?
Yes, read about signals and slots in the documentation, seems you haven't understand the concept right.

Use textChanged() signal of the line edit, connect it to your slot, check the value and alter the state of your button.

wagmare
29th May 2009, 06:52
if (lineEdit_name->text() != " ") {
emit allfieldsfull();
}

also dont use like this condition ..

use
QString text = lineEdit_name->text();
if (!text.isEmpty())
emit allfieldsfull();

Lykurg
29th May 2009, 07:03
QString text = lineEdit_name->text();
if (!text.isEmpty())
emit allfieldsfull();


if (!lineEdit_name->text().isEmpty())
emit allfieldsfull();
Is even more better because of avoiding a temporary variable. But nonetheless in that case a signal is not necessary, because you can simply call your member function.

wagmare
29th May 2009, 07:18
if (!lineEdit_name->text().isEmpty())


yes i know this is more simpler ... but for newbie i explained clearly that we check in QString function ..

and see i use "also" .. to tell him in future u use conditions like this in different application ..
here using textChanged() signal is the better one ... i agree ...