Sum 2 integers
Hi all, i need to do a presentation of Qt Designer for my class.
So i’m going to do an application which does the sum between 2 integers and place the result into a TextBox.
But i’m having trouble making it.
I hope you guys can help me!
14 replies
Each line edit holds its text value in a QString object. You can access values by QLineEdit::text [qt-project.org] . You need to convert text values of QLineEdit s to integer. It’s done by QString::toInt [qt-project.org] . Then just add your numbers, and put result in desired QLineEdit. Convert integer to QString by QString::number [qt-project.org] 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"
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
[Edit: Please add @ tags around your code; mlong]
now i’m felling like i’m lost.. i thought Designer was a lot more easier..
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;
- }
Try this if it works. As a beginner you can start with some Video tutorials [voidrealms.com]
You must log in to post a reply. Not a member yet? Register here!



