PDA

View Full Version : To Display output like c=5 in qt creator within same line edit.



nawaz
30th January 2013, 09:24
i want to display output line c=5 in sam line edit. but m only getting 5 .

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()),this,SLOT(result()));
}

void MainWindow::result()
{
int a=2;
int b=3 ;
int c= a+b;
QString s="";
ui->lineEdit->setText("c=");
ui->lineEdit->setText(s.setNum(c));


}

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

Zlatomir
30th January 2013, 09:44
Look at arg() (http://qt-project.org/doc/qt-4.8/qstring.html#arg) member function in QString and it's overloads, and construct a QString with what you need and set that to display in the label (or lineEdit).

d_stranz
30th January 2013, 16:21
Every time you call setText(), it completely replaces the current text in the line edit. So your second call to setText() erases the "c=" and replaces it with "5". What Zlatomir is telling you is to look at the QString::arg() methods, and use them to build a QString that contains both "c=" and the result of the calculation, then call setText() once with that formatted string.