PDA

View Full Version : How to disable a QLineEdit?



ewwen
14th May 2011, 11:37
Hi everybody!

I´m using 3 QLineEdits that someone can enter a number in each.

Idea: 1) QLineEdit No. 1 is enabled, and QLE No. 2 and No. 3 are disabled. --> If someone enters a number in QLE No.1, than No. 2 is enabled. No. 3 stays disabled. --> If someone enters a number in QLE No. 2, than even No. 3 is enabled.

I´m not sure at all, if this is possible. So far I haven´t found a thread, where someone was disabling a QLineEdit. In the Qt-Helpfile I only found "dragEnabled" within the properties...

If someone has got an idea, please let me know...

Thanks in advance.

Zlatomir
14th May 2011, 11:47
You can use the setDisabled(...); (http://doc.qt.nokia.com/latest/qwidget.html#setDisabled) slot.

ewwen
14th May 2011, 15:41
Thanks :-)! I try it!

tinysoft
15th May 2011, 06:01
use signal and slot :
the signal is when the text of the lineEdit change
the slot is to check if the lineEdit is not empty , then set the lineEdit enabled.

example:

signals:


connect(ui->lineEdit1,SIGNAL(textChanged(QString)),this,SLOT(l ineEdit1TextChanged()));
connect(ui->lineEdit2,SIGNALtextChanged(QString)),this,SLOT(li neEdit2TextChanged()));


slots:


void MainWindow::lineEdit1TextChanged()
{
if(ui->lineEdit1->text()=="")
ui->lineEdit2->setDisabled(1);
else ui->lineEdit2->setEnabled(1);
}

void MainWindow::lineEdit2TextChanged()
{
if(ui->lineEdit2->text()=="")
ui->lineEdit3->setDisabled(1);
else ui->lineEdit3->setEnabled(1);
}

ewwen
18th May 2011, 13:49
Hi, thanks for your code-snippet.

I tried (with validator):
-------------

....
in1->setValidator(validator);
in2->setValidator(validator);
.....

connect(in1,SIGNAL(textChanged(String)),this,SLOT( in1TextChanged()));
}

void Tab::in1TextChanged()
{
if(in1->text()=="")
{
in2->setDisabled(1);
}
else
{
in2->setEnabled(1);
}
}
-------------
with
-------------

private slots:
void in1TextChanged();

private:
...
QLineEdit *in1;
QLineEdit *in2;
-------------
set as pointers in the headerfile;

and:
--------

void Tab::calculate()
{
.....
bool ok;
double number1 = in1->text().toDouble(&ok);
double number2 = in2->text().toDouble(&ok);
.......
--------------

But the "disable"-Function does not work.

Do you have an idea?

tinysoft
18th May 2011, 17:42
every thing seems fine ... :confused:

make sure that you set the in2 disabled at the bigging of the Tab class after declaration ( in2=new QLineEdit; )

also make sure that the connect statement exist at the same place.

ewwen
19th May 2011, 14:03
Hi!

Thanks tinysoft for your reply!

I tried now everything new from scratch.



#include "mainwindow.h"
#include <QObject>
#include <QtGui>
#include <QIntValidator>
#include <QWidget>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QGridLayout *grid = new QGridLayout;
grid->addWidget(input(), 0, 0);
setLayout(grid);

setWindowTitle(tr("QLineEdit example"));
setMinimumSize(500, 400);

}

QGroupBox *MainWindow::input()
{
QGroupBox *groupBox = new QGroupBox(tr("Input"));
QLabel *fwerl = new QLabel(tr("<font>Insert your values:</font>"));
in1 = new QLineEdit;
in2 = new QLineEdit;
in3 = new QLineEdit;

QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(fwerl);
formLayout->addRow(tr("1st value:"), in1);
formLayout->addRow(tr("2nd value:"), in2);
formLayout->addRow(tr("3rd value:"), in3);
groupBox->setLayout(formLayout);

connect(in1,SIGNAL(textChanged(QString)),this,SLOT (in1TextChanged()));
connect(in2,SIGNAL(textChanged(QString)),this,SLOT (in2TextChanged()));

QValidator *validator = new QDoubleValidator(0.0, 1.0, 10, in1);
in1->setValidator(validator);
in2->setValidator(validator);
in3->setValidator(validator);
return groupBox;
}

void MainWindow::in1TextChanged()
{
if(in1->text()=="")
{
in2->setDisabled(1);
}
else
{
in2->setEnabled(1);
}
}

void MainWindow::in2TextChanged()
{
if(in2->text()=="")
{
in3->setDisabled(1);
}
else
{
in3->setEnabled(1);
}
}

MainWindow::~MainWindow()
{
delete mainWindow;
}



Now the problem is, the QLineEdits are not displayed. But I don´t even get a warning - everything compiles fine...

Do you know why? If I use "setCentralWidget(grid)" instead of setLayout(grid) it doesn´t run anymore...

tinysoft
19th May 2011, 21:54
i am confused .. is it possible to return QGroupBox ?

QGroupBox *MainWindow::input();

you can use the designer you know .. its much more easier .i wrote your example using the designer to create the widgets and the layout .. i uploaded it ( here ) (http://www.4shared.com/file/_v2ee5ri/Disable_QLineEdit.html)

i hope its helpful

ewwen
21st May 2011, 08:53
Hi tinysoft!

Thank you very much. With your file and some work I found the problems:
1) I was missing:

in1->setDisabled(1);....
2)

QWidget *widget = new QWidget;
setCentralWidget(widget);
because of QMainWindow. QWidget, for example, is a member of it.
and
3) I removed the GroupBox and just used:


QGridLayout *formLayout = new QGridLayout;
...
widget->setLayout(formLayout);


Now everything works fine :-)!

Thanks for your hints and have a nice day!

tinysoft
21st May 2011, 13:43
i'm glad that you solved it :)

have a nice day u 2