PDA

View Full Version : how to get the text from a LineEdit using QT??



Soumya Somasekhar Ram
4th September 2013, 09:56
I want to display the text in the LineEdit using Qt.
but when i use the code an empty string wiil be displayed.
The following is my code.


if( QPushButton *button = (QPushButton *)sender())
{// it's a "QPushButton", do something with pb here
QFrame* popup1 = new QFrame(this, Qt::Tool | Qt::Window );
popup1->setWindowTitle("Rename the folder");
QLineEdit *lb=new QLineEdit(popup1);
connect( lb, SIGNAL( returnPressed() ), lb, SLOT( hide() ) );
lb->setFocus();
lb->show();
lb->move(QCursor::pos());
qDebug()<<lb->text();
button->setText(lb->text());
button->setObjectName(lb->text());

} }
Can any one help me.
Thanks in advance.

wagmare
4th September 2013, 10:37
if( QPushButton *button = (QPushButton *)sender())
{// it's a "QPushButton", do something with pb here
QFrame* popup1 = new QFrame(this, Qt::Tool | Qt::Window );
popup1->setWindowTitle("Rename the folder");
QLineEdit *lb=new QLineEdit(popup1);
connect( lb, SIGNAL( returnPressed() ), lb, SLOT( hide() ) );
lb->setFocus();
lb->show();
lb->move(QCursor:os());
qDebug()<<lb->text();
button->setText(lb->text());
button->setObjectName(lb->text());

} }

u are not setting any text in QLineEdit in ur code .
u shuld use

lb->setText("Ur Text that u want to display");
or else while constructing the lineEdit
QLineEdit::QLineEdit ( const QString & contents, QWidget * parent = 0 )
u can set

QLineEdit *lb=new QLineEdit("Ur Text", popup1);

Soumya Somasekhar Ram
4th September 2013, 11:29
Actually the text is entered at run time.
Whatever is entering at runtime, that should be displayed using text() and set as the text of the button.

anda_skoa
4th September 2013, 11:40
Sure, but your code just reads the initial text, which in your snippet, is empty.

You create a line edit and read its content, which if course is unchanged since there has been no possibility for any user interaction.

What you probably want is to show the input window and set the button once there the user has entered some text.
Connect a slot to editingFinished() or returnPressed() and read the text in there.

Cheers,
_

wagmare
4th September 2013, 11:44
Actually the text is entered at run time.
Whatever is entering at runtime, that should be displayed using text() and set as the text of the button.
u need to connect the signal of lineEdit to a slot which will update the text of QPushButton

connect( lb, SIGNAL( textChanged (const QString &) ), lb, SLOT( changeButtonText(const QString &) ) );

slot:

changeButtonText(const QString &data)
{
button->setText(data);
}

as u used returnPressed() , remove the hide() slot
and add ur changeButtonText()

connect( lb, SIGNAL( returnPressed ( ), lb, SLOT( changeButtonText() ) )

and in
changeButtonText()
{
button->setText(lb->text());
}

anda_skoa
4th September 2013, 14:35
Just to clarify what wagmare wrote: those are two options, both will result in the line edit's text on the button.
The first option will update the button text as the user types, the second one will update it only at the end.

Either option can keep the connect to hide() though.

Cheers,
_

Soumya Somasekhar Ram
6th September 2013, 06:07
That code didn't work.
Actually its because of the object name of the dynamically created push button won't available using the following code.

QPushButton *button = (QPushButton *)sender()

Can u suggest any other way to get the reference of that particular button ,when user clicks on it.
Thank you for your response.

anda_skoa
6th September 2013, 09:36
Rule of thumb: if you need to access a widget after construction, keep its pointer in a member variable of your class :)

Cheers,
_

Soumya Somasekhar Ram
6th September 2013, 12:37
But the problem is ,here I am creating alarge no of pushButtons dynamically.
I have to identify which one is currently selected.
Is there any other method for checking the active widget?
QObject::sender() return the refference of the main window.
Can any one help me.
Thanks in advance.

anda_skoa
7th September 2013, 10:46
But the problem is ,here I am creating alarge no of pushButtons dynamically.


Ah, you didn't say that.

Look at QButtonGroup or alternatively for QSignalMapper

Cheers,
_

Soumya Somasekhar Ram
11th September 2013, 11:03
In the above reply of Mr.Wagmare, where should I define the functions changeButtonText(const QString &data) and changeButtonText().

I defined it as
void Mainwindow::changeButtonText(const QString &data)
{
}
Then ,it shows a run time error that no such slot for lb(label name here).

anda_skoa
11th September 2013, 14:43
Well, obviously you need to use the pointer of the main window instance as the receiver object in the connect statement.
e.g. "this" if you do the connect from inside a main window method

Cheers,
_