You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, August 10th 2009, 5:09pm

Qt Calculator... -_+

I've made a pretty simple calculator and I wanna show you guys to help me complete it... I have a attached a zip file together with this post that contains the source code for you to download and check it out...

Well, anyway, here are my concerns...

-For now, I don't have a "backspace" button for this calculator because I do not know how to make a function for it...I want to know how can I make that work...

-The "decimal" or the "dot" button of this calculator doesn't work as it should be... If you check out what I've done so far with my source code, you would see that you can do tons of dots or decimal places.. Which is pretty weird,, because Base-10 consists of 1 decimal place... lol,, xD ...

-And... well, this could be the toughest for me to get around with... Hmmmmm, this calculator functions by pressing buttons on its UI with your mouse... I want that to work the same on the keyboard's keys... Say, the button with text "1", works by clickin on it with your mouse, should also do the same when I press "1" either from the numpad or from the alphanumeric keys of my keyboard (above letter "Q").... same goes with the "=" button, probably by pressing Enter from my keyboard; backspace with the backspace key of my keyboard, etc...

Hmmmm.. well, ofcourse, I don't want unnecessary characters showing on my calculator's lineEdit widget, so uhmmmm, how am I going to filter that to show only 0-9 digits plus the dot, and no alphabet characters nor any special characters even if the user tries to press any key other than digits 0-9 and the dot....

One more thing... Pressing "=" works, but it's kinda incomplete... Try this, do a little addition with my calc... say, 1 + 1 and press "="... Well, yeah, it would show "2"... But the thing is, it's not like the other calculators I saw.... Try pressing "=" again.... Ahh, now it stays there... shouldn't it go like this? 1 + 1 = 2, press "=" again, = 3, and again, = 4, and so on and so forth... You know what I mean? Hehehehe....


Well, thanks in advance... God bless always....

~ShadeS_07 -_+ 8) :D ^^
ShadeS_07 has attached the following file:
  • qtCalc.zip (2.68 kB - 104 times downloaded - latest: Nov 23rd 2016, 5:38am)

This post has been edited 1 times, last edit by "ShadeS_07" (Aug 10th 2009, 5:17pm)


profox

Trainee

  • "profox" is male

Posts: 111

Location: Belgium

  • Send private message

2

Monday, August 10th 2009, 5:48pm

RE: Qt Calculator... -_+


-For now, I don't have a "backspace" button for this calculator because I do not know how to make a function for it...I want to know how can I make that work...

Source code

1
2
3
4
5
6
void MainWindow::on_btnBackSpace_clicked()
{
    lineEdit_str = ui->lineEdit->text();
    lineEdit_str.chop(1);
    ui->lineEdit->setText(lineEdit_str);
}



-The "decimal" or the "dot" button of this calculator doesn't work as it should be... If you check out what I've done so far with my source code, you would see that you can do tons of dots or decimal places.. Which is pretty weird,, because Base-10 consists of 1 decimal place... lol,, xD ...

Source code

1
2
3
4
5
6
7
void MainWindow::btn_dot()
{
    if (!lineEdit_str.contains('.')) {
        lineEdit_str = lineEdit_str + ui->btnDot->text();
        ui->lineEdit->setText(lineEdit_str);
    }
}



-And... well, this could be the toughest for me to get around with... Hmmmmm, this calculator functions by pressing buttons on its UI with your mouse... I want that to work the same on the keyboard's keys... Say, the button with text "1", works by clickin on it with your mouse, should also do the same when I press "1" either from the numpad or from the alphanumeric keys of my keyboard (above letter "Q").... same goes with the "=" button, probably by pressing Enter from my keyboard; backspace with the backspace key of my keyboard, etc...

Hmmmm.. well, ofcourse, I don't want unnecessary characters showing on my calculator's lineEdit widget, so uhmmmm, how am I going to filter that to show only 0-9 digits plus the dot, and no alphabet characters nor any special characters even if the user tries to press any key other than digits 0-9 and the dot....

Reimplement keyPressEvent like this:

mainwindow.h

Source code

1
2
protected:
    void keyPressEvent(QKeyEvent *e);


mainwindow.cpp
at the top:

Source code

1
#include <QKeyEvent>

somewhere:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//just a small example, add all the keys you need like this
void MainWindow::keyPressEvent(QKeyEvent *e) {
    switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
            eql();
            break;
        case Qt::Key_0:
            btn0();
            break;
        case Qt::Key_1:
            btn1();
            break;
        case Qt::Key_2:
            btn2();
            break;
        default:
            break;
    }
}



One more thing... Pressing "=" works, but it's kinda incomplete... Try this, do a little addition with my calc... say, 1 + 1 and press "="... Well, yeah, it would show "2"... But the thing is, it's not like the other calculators I saw.... Try pressing "=" again.... Ahh, now it stays there... shouldn't it go like this? 1 + 1 = 2, press "=" again, = 3, and again, = 4, and so on and so forth... You know what I mean? Hehehehe....

Do not do this:

Source code

1
op_ch = "";

in your eql() function

By the way... The Qt docs have a more advanced example of a calculator. You might want to check it out:
http://doc.qtsoftware.com/4.5/widgets-calculator.html

3

Monday, August 10th 2009, 10:49pm

Wow, thanks man... I'm gonna try all those after my class! Thank you very much dude.... Glad that it's you again, who helped me.... Ahh, you never fail to help peepz yo... keep it up dude! God bless.... ~ShadeS_07 -_+ 8) :D ;)

4

Tuesday, August 11th 2009, 7:48am

Hmmmm.. well, ofcourse, I don't want unnecessary characters showing on my calculator's lineEdit widget, so uhmmmm, how am I going to filter that to show only 0-9 digits plus the dot, and no alphabet characters nor any special characters even if the user tries to press any key other than digits 0-9 and the dot....

I think subclassing QValidator is the way to go.

5

Tuesday, August 11th 2009, 1:39pm

Thanks you job, that also helped me... ^_^

Well, uhmmmm, now I am having another problem... When I multiply 6561 by 6561 (6561 ^ 2, that is..) I should get 43046721, just like I would from other calculator application... But in my case here's what I get: 4.30467e+07

Any idea how to get around with that folks? Thanks ya'll! God bless always...

~ShadeS_07,, -_+

6

Tuesday, August 11th 2009, 2:40pm

Change your "num" array and " rslt_flt" from floats to doubles (or qreals) then you'll have double the precision you have now.
You should also use the extra arguments of QString::number to format the output.

7

Tuesday, August 11th 2009, 10:59pm

I did change it from float data type to double data type before I posted here, dude.... But,, qreal? Never heard of it nor have tried it before... Well, I'll give it a shot anyway... Thanks job! Be right back for the result.... -_+

8

Wednesday, November 30th 2016, 6:41am

One more thing... Pressing "=" works, but it's kinda incomplete... Try this, do a little addition with my calc... say, 1 + 1 and press "="... Well, yeah, it would show "2"... But the thing is, it's not like the other calculators I saw.... Try pressing "=" again.... Ahh, now it stays there... shouldn't it go like this? 1 + 1 = 2, press "=" again, = 3, and again, = 4, and so on and so forth... You know what I mean?

can someone help me out in resolving this problem , even i got struck here