Sum 2 integers
-
okay... Have you tried anything at all ?
You can start "here":http://www.developer.nokia.com/Community/Wiki/How_to_use_Qt_Creator_IDE and "there":https://qt-project.org/videos
-
Each line edit holds its text value in a QString object. You can access values by "QLineEdit::text":http://qt-project.org/doc/qt-4.8/qlineedit.html#text-prop . You need to convert text values of QLineEdit s to integer. It's done by "QString::toInt":http://qt-project.org/doc/qt-4.8/qstring.html#toInt . Then just add your numbers, and put result in desired QLineEdit. Convert integer to QString by "QString::number":http://qt-project.org/doc/qt-4.8/qstring.html#number-4 and set text of QLineEdit.
Hope that helps.
-
But i don't know how to make functions, i mean in c++ is a lot more easy..
like:
@
int Sum(int a, int b){
return a+b;
}
@
now i'm felling like i'm lost.. i thought Designer was a lot more easier..By default Qt made this:
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}
@[Edit: Please add @ tags around your code; mlong]
-
[quote author="Vesco" date="1337023716"]
now i'm felling like i'm lost.. i thought Designer was a lot more easier..
[/quote]
The Qt Designer makes things far more easier when we are creating a forms and adding widgets. The more you practice the more you learn. So as you have already added three textbox/lineEdit you can further set the objectName in the property editor as lineEditOne , lineEditTwo and lineEditThree. Also you can further add button/pushButton and set the objectName as btnAdd.
So that the user can enter the values in the lineEdit and Click on Add button to display the result in lineEditThree.
You can try the following code on_btnAdd_Clicked() slot(you can create through designer)
@
void MainWindow::on_btnAdd_clicked()
{
int a = ui->lineEditOne->text().toInt();
int b = ui->lineEditTwo->text().toInt();
int result = a+b;
ui->lineEditThree->setText(QString::number(result));}@
Try this if it works. As a beginner you can start with some "Video tutorials ":http://www.voidrealms.com/tutorials.aspx?filter=qt
-
The lineEdit::text() returns a QString so in order to assign it to an integer value we need to cast it, so we use .toInt(). Similarly when we are setting the value to the lineEdit it has to be a QString. So we cast it from int to QString using QString::number();
Look at Soroush's post above.