PDA

View Full Version : Qt Calculator



Johnnyj2j
7th July 2012, 16:31
http://i46.tinypic.com/htb2m8.png


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

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

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

void MainWindow::on_actionClose_triggered() //Close the Program "An action in the file menu"
{

}

void MainWindow::on_pushButton_clicked() //Addition
{

}

void MainWindow::on_pushButton_2_clicked() //Subtraction
{

}

void MainWindow::on_pushButton_3_clicked() //Divishion
{

}

void MainWindow::on_pushButton_4_clicked() //Multiplication
{

}
As you can see above im trying to creat a Calculator but iv run into a small problem im extreemly good with C++ which is what im codeing with but i dont now how to use buttons to execute a function or how to hold what is typed in a textline in a variable so if some one could tell me how to do that or show me where i can find a tutorial for it i would be greatfull

also if anyone nows how to close the main window through an action i created called exit that would be great
Thanks in advance sorry if this is noobie :rolleyes:

wysota
7th July 2012, 17:27
Calculator Example

Johnnyj2j
7th July 2012, 17:55
thanks for the example but i find it hard to follow everything would you mind just stating how to

1) Call on text in textline
2) Activate Function if button pressed
3) extit through action

if its not asking to much...

ChrisW67
9th July 2012, 00:28
... im extreemly good with C++ ... how to hold what is typed in a textline in a variable

Really? It's just C++ and the necessary functions are well documented. Qt does not make C++ suddenly change its rules.


QString value = ui->lineedit->text();


1) QLineEdit::text()

2) Connect the QPushButton::clicked() signal to a function that is declared as a slot, or rely on the Qt Designer automatic connection by name as you are doing. These sorts of connections are shown in almost every single Qt example in the manual (widget examples more than QML examples).

3) Close the last remaining top level application window with QWidget::close() as in almost every single Qt example in the manual, or call QCoreApplication::quit() directly or through the qApp object.

The manual is your friend.

chriskon149
9th July 2012, 04:44
2) Activate Function if button pressed

As an alternative to ChrisW67's answer, whatever you put in the scope of the following function will be called whenever the "pushButton" widget is "clicked" in MainWindow's form.



void MainWindow::on_pushButton_clicked()
{
//Code to execute when pushButton is clicked
}


Hope this helps!

Chris