PDA

View Full Version : QLineEdit text not retrieved



trojansmith1990
17th May 2011, 19:53
i cannot access or retrieve text from my QLineEdit object......i have given the object name as "dist"
and declared as:

QLineEdit *dist=new QLineEdit(this);

and tried to retrieve text with:

dist->text();

and later i am converting text to int by:

bool ok=1;
QString str=dist->text().toInt(&ok,10);

the value of ok before converting is 1 and after converting(toInt) is 0.....
i dunno why it is not converting or why it is not retrieving values from object name "dist"....
using Qt4....ubuntu

i also have QMetaObject(connectslotsbyname) in my ui_mainwindow.h file....could anyone kindly please help

Zlatomir
17th May 2011, 20:04
If the ok variable is false it means that the conversion didn't succeed, see the documentation here (http://doc.qt.nokia.com/latest/qstring.html#toInt).

And this line:

dist->text();
doesn't store the returned QString, use it like:

QString stringFromLineEdit = dist->text();
And make sure your code is executed after the lineEdit dist have the input you expect.

Santosh Reddy
17th May 2011, 20:06
may be you should use int instead of QString



int number = dist->text().toInt(&ok,10);

trojansmith1990
17th May 2011, 20:15
void MainWindow::on_plot_clicked()
{
bool ok=true;
cout<<"ok:"<<ok<<endl;
dist=new QLineEdit(this);
azim=new QLineEdit(this);

QString r=(dist->text());
QString a=(azim->text());
QByteArray barray=r.toLatin1();
const char *ch=barray.constData();
int rad = atoi(ch);

QByteArray barray2=a.toLatin1();
const char *ch2=barray2.constData();
int ang = atoi(ch2);


float x=rad*cos(ang);
float y=rad*sin(ang);

cout<<"ch:"<<ch<<endl;
cout<<"a:"<<ang<<endl;
cout<<"ok:"<<ok<<endl;
QGraphicsItem *obj1=scene->addRect(x,y,x+10,y+10);

}


this was my mainwindow.cpp

here is my header file:



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QMessageBox>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPainter>
#include <QRectF>
#include <QGraphicsEllipseItem>
#include <QString>
#include <QGraphicsScale>
#include <QMetaObject>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QGraphicsView *view;
QGraphicsScene *scene;
QPainter *paint;
static const int min_x=0;
static const int min_y=0;
static const int max_x=500;
static const int max_y=500;
QGraphicsEllipseItem *ell1;
QGraphicsEllipseItem *ell2;
QGraphicsEllipseItem *center;
QLineEdit *dist;
QLineEdit *azim;
QLineEdit *scale;
QString sc_str;
QGraphicsScale *sc;


private:
Ui::MainWindow *ui;

private slots:


private slots:
void on_scale_map_clicked();

void on_plot_clicked();
};

#endif // MAINWINDOW_H





this is not working.....

Zlatomir
17th May 2011, 20:19
Are you sure that you show the QLineEdit declared by you in your class QLineEdit *dist; and not some other QLineEdit generated from the ui file?

trojansmith1990
17th May 2011, 20:29
ya sorry...i used int to store it itself ...just messed up....this somethiing new i tried....but failed too.....

int number = dist->text().toInt(&ok,10);

was not working.....it looks trivial....but anyway thanks for trying....

Added after 6 minutes:

its still not working...i tried with the latest code given above.....the two samples pasted above.....not working.....

please look in "dist" variable and "azim"....line 8 of mainwindow.cpp onwards...thank you

yes....i have declared QLineEdit.....not generated....

yes i did declare it myself...

its still not working...i tried with the latest code given above.....the two samples pasted above.....not working.....

please look in "dist" variable and "azim"....line 8 of mainwindow.cpp onwards...thank you

wysota
17th May 2011, 20:42
Could you add this line right before the one with toInt() and tell us what it prints (you'll need to #include <QtDebug> as well)?

qDebug() << "Line edit text:" << dist->text();

Zlatomir
17th May 2011, 20:47
I know that you declare it yourself, but what i wanted to say is to check if you don't somehow show some other QLineEdit from the ui (from designer) and that have the text you expected - because from the code you posted i see you have a designer file and you also code some more QWidgets.

Santosh Reddy
17th May 2011, 21:02
..
dist=new QLineEdit(this);
azim=new QLineEdit(this);

QString r=(dist->text());
QString a=(azim->text());
...


You are creating the QLineEdit and immediatly reading the input, when will the user/operator get a chance to enter data / number ?

May you need to create QLineEdit's earlier somewhere like ctor, then read them in the plot_slot....