PDA

View Full Version : LineEdit's



Johnnyj2j
8th July 2012, 19:34
How do i put text from a line edit into a string then print it out into anouther line edit????
Thanks in advance

Zlatomir
8th July 2012, 21:41
You get it with QString text() and set it with void setText ( const QString & ) documentation link (http://qt-project.org/doc/qt-4.8/qlineedit.html#text-prop)

Johnnyj2j
8th July 2012, 22:24
Thanks that helped me out allot but it lead to anouther question


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

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

void MainWindow::on_actionClose_triggered()
{

}

void MainWindow::on_pushButton_clicked()
{
QString Num1 = ui->lineEdit ->text();
QString Num2 = ui->lineEdit ->text();
ui->lineEdit_3->setText(Num1+Num2);
}

void MainWindow::on_pushButton_2_clicked()
{
QString Num1 = ui->lineEdit ->text();
QString Num2 = ui->lineEdit ->text();
ui->lineEdit_3->setText(Num1-Num2);
}

void MainWindow::on_pushButton_3_clicked()
{
QString Num1 = ui->lineEdit ->text();
QString Num2 = ui->lineEdit ->text();
ui->lineEdit_3->setText(Num1/Num2);
}

void MainWindow::on_pushButton_4_clicked()
{
QString Num1 = ui->lineEdit ->text();
QString Num2 = ui->lineEdit ->text();
ui->lineEdit_3->setText(Num1*Num2);
}

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

ChrisW67
9th July 2012, 00:14
You cannot divide or multiply strings... they are functions you want to apply to numbers. Have a look at QString::toInt() and QString::toDouble()

Johnnyj2j
9th July 2012, 02:26
void MainWindow::on_pushButton_clicked()
{
int Num1 = ui->lineEdit ->text().toInt();
int Num2 = ui->lineEdit_2 ->text().toInt();
QString Answer = Num1+Num2;
QString go = Answer.toStdString();
ui->lineEdit_3->setText(go);
}

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

chriskon149
9th July 2012, 04:26
void MainWindow::on_pushButton_clicked()
{
int Num1 = ui->lineEdit ->text().toInt();
int Num2 = ui->lineEdit_2 ->text().toInt();
QString Answer = Num1+Num2;
QString go = Answer.toStdString();
ui->lineEdit_3->setText(go);
}

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


void MainWindow::on_pushButton_clicked()
{
int Num1 = ui->lineEdit->text().toInt();
int Num2 = ui->linEdit_2->text().toInt();
int Answer = Num1 + Num2;
ui->linEdit_3->setText(QString::number(Answer));
}



I hope this helps!

Chris

Johnnyj2j
9th July 2012, 12:34
i changed it a little but it works thanks man :)