Results 1 to 9 of 9

Thread: QLineEdit text not retrieved

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLineEdit text not retrieved

    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
    Last edited by trojansmith1990; 17th May 2011 at 20:06.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit text not retrieved

    If the ok variable is false it means that the conversion didn't succeed, see the documentation here.

    And this line:
    Qt Code:
    1. dist->text();
    To copy to clipboard, switch view to plain text mode 
    doesn't store the returned QString, use it like:
    Qt Code:
    1. QString stringFromLineEdit = dist->text();
    To copy to clipboard, switch view to plain text mode 
    And make sure your code is executed after the lineEdit dist have the input you expect.

  3. #3
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit text not retrieved

    Qt Code:
    1. void MainWindow::on_plot_clicked()
    2. {
    3. bool ok=true;
    4. cout<<"ok:"<<ok<<endl;
    5. dist=new QLineEdit(this);
    6. azim=new QLineEdit(this);
    7.  
    8. QString r=(dist->text());
    9. QString a=(azim->text());
    10. QByteArray barray=r.toLatin1();
    11. const char *ch=barray.constData();
    12. int rad = atoi(ch);
    13.  
    14. QByteArray barray2=a.toLatin1();
    15. const char *ch2=barray2.constData();
    16. int ang = atoi(ch2);
    17.  
    18.  
    19. float x=rad*cos(ang);
    20. float y=rad*sin(ang);
    21.  
    22. cout<<"ch:"<<ch<<endl;
    23. cout<<"a:"<<ang<<endl;
    24. cout<<"ok:"<<ok<<endl;
    25. QGraphicsItem *obj1=scene->addRect(x,y,x+10,y+10);
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    this was my mainwindow.cpp

    here is my header file:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QLineEdit>
    6. #include <QMessageBox>
    7. #include <QGraphicsScene>
    8. #include <QGraphicsView>
    9. #include <QPainter>
    10. #include <QRectF>
    11. #include <QGraphicsEllipseItem>
    12. #include <QString>
    13. #include <QGraphicsScale>
    14. #include <QMetaObject>
    15.  
    16. namespace Ui {
    17. class MainWindow;
    18. }
    19.  
    20. class MainWindow : public QMainWindow
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. explicit MainWindow(QWidget *parent = 0);
    26. ~MainWindow();
    27. QPainter *paint;
    28. static const int min_x=0;
    29. static const int min_y=0;
    30. static const int max_x=500;
    31. static const int max_y=500;
    32. QLineEdit *dist;
    33. QLineEdit *azim;
    34. QLineEdit *scale;
    35. QString sc_str;
    36. QGraphicsScale *sc;
    37.  
    38.  
    39. private:
    40. Ui::MainWindow *ui;
    41.  
    42. private slots:
    43.  
    44.  
    45. private slots:
    46. void on_scale_map_clicked();
    47.  
    48. void on_plot_clicked();
    49. };
    50.  
    51. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 



    this is not working.....

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit text not retrieved

    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?

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QLineEdit text not retrieved

    Qt Code:
    1. ..
    2. dist=new QLineEdit(this);
    3. azim=new QLineEdit(this);
    4.  
    5. QString r=(dist->text());
    6. QString a=(azim->text());
    7. ...
    To copy to clipboard, switch view to plain text mode 

    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....

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QLineEdit text not retrieved

    may be you should use int instead of QString

    Qt Code:
    1. int number = dist->text().toInt(&ok,10);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 17th May 2011 at 20:10. Reason: spelling corrections

Similar Threads

  1. QLineEdit default text
    By Guilo in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2011, 10:44
  2. I cannot get the string from QLineEdit->text()!
    By MIH1406 in forum Qt Programming
    Replies: 0
    Last Post: 3rd June 2010, 11:26
  3. QLineEdit and background text
    By SnarlCat in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2009, 22:17
  4. QLineEdit text() crash - Qt3
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 10th June 2008, 09:42
  5. Delete items retrieved from QTreeWidget.takeItem
    By bruccutler in forum Qt Programming
    Replies: 3
    Last Post: 22nd March 2007, 23:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.