Results 1 to 7 of 7

Thread: LineEdit's

  1. #1
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    6

    Default LineEdit's

    How do i put text from a line edit into a string then print it out into anouther line edit????
    Thanks in advance

  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: LineEdit's

    You get it with QString text() and set it with void setText ( const QString & ) documentation link

  3. The following user says thank you to Zlatomir for this useful post:

    Johnnyj2j (9th July 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    6

    Default Re: LineEdit's

    Thanks that helped me out allot but it lead to anouther question

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <iostream>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_actionClose_triggered()
    18. {
    19.  
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. QString Num1 = ui->lineEdit ->text();
    25. QString Num2 = ui->lineEdit ->text();
    26. ui->lineEdit_3->setText(Num1+Num2);
    27. }
    28.  
    29. void MainWindow::on_pushButton_2_clicked()
    30. {
    31. QString Num1 = ui->lineEdit ->text();
    32. QString Num2 = ui->lineEdit ->text();
    33. ui->lineEdit_3->setText(Num1-Num2);
    34. }
    35.  
    36. void MainWindow::on_pushButton_3_clicked()
    37. {
    38. QString Num1 = ui->lineEdit ->text();
    39. QString Num2 = ui->lineEdit ->text();
    40. ui->lineEdit_3->setText(Num1/Num2);
    41. }
    42.  
    43. void MainWindow::on_pushButton_4_clicked()
    44. {
    45. QString Num1 = ui->lineEdit ->text();
    46. QString Num2 = ui->lineEdit ->text();
    47. ui->lineEdit_3->setText(Num1*Num2);
    48. }
    To copy to clipboard, switch view to plain text mode 

    This is the code i used it in for a simple caculator but for some reason i get errors when useing + - * / ect how do i fix this?
    thanks agin

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: LineEdit's

    You cannot divide or multiply strings... they are functions you want to apply to numbers. Have a look at QString::toInt() and QString::toDouble()

  6. The following user says thank you to ChrisW67 for this useful post:

    Johnnyj2j (9th July 2012)

  7. #5
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    6

    Default Re: LineEdit's

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. int Num1 = ui->lineEdit ->text().toInt();
    4. int Num2 = ui->lineEdit_2 ->text().toInt();
    5. QString Answer = Num1+Num2;
    6. QString go = Answer.toStdString();
    7. ui->lineEdit_3->setText(go);
    8. }
    To copy to clipboard, switch view to plain text mode 

    im still cant figure it out :/ and i have read the help file... i think i made them into int but them i belive i have to make them into a string agin after i do the caculation to print them out....

  8. #6
    Join Date
    Aug 2011
    Location
    California, USA
    Posts
    24
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: LineEdit's

    Quote Originally Posted by Johnnyj2j View Post
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. int Num1 = ui->lineEdit ->text().toInt();
    4. int Num2 = ui->lineEdit_2 ->text().toInt();
    5. QString Answer = Num1+Num2;
    6. QString go = Answer.toStdString();
    7. ui->lineEdit_3->setText(go);
    8. }
    To copy to clipboard, switch view to plain text mode 

    im still cant figure it out :/ and i have read the help file... i think i made them into int but them i belive i have to make them into a string agin after i do the caculation to print them out....
    What I do is use QString::number(myNumber) to convert numbers (ints, doubles, etc) into strings.

    You can try this (it should do what you want).
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. int Num1 = ui->lineEdit->text().toInt();
    4. int Num2 = ui->linEdit_2->text().toInt();
    5. int Answer = Num1 + Num2;
    6. ui->linEdit_3->setText(QString::number(Answer));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I hope this helps!

    Chris

  9. The following user says thank you to chriskon149 for this useful post:

    Johnnyj2j (9th July 2012)

  10. #7
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    6

    Default Re: LineEdit's

    i changed it a little but it works thanks man

Similar Threads

  1. LineEdit.
    By Rewo in forum Newbie
    Replies: 17
    Last Post: 2nd July 2010, 09:37
  2. Help on using QTableWidget with LineEdit.
    By narendra in forum Qt Programming
    Replies: 3
    Last Post: 31st December 2009, 15:30
  3. Validate a value of lineEdit
    By edgar in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2009, 18:22
  4. lineedit in qpainter
    By Devoraz in forum Newbie
    Replies: 9
    Last Post: 5th August 2009, 01:52
  5. How to update lineEdit
    By HelloDan in forum Qt Programming
    Replies: 9
    Last Post: 17th February 2009, 07:01

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.