Qt Tutorial 2 : C++ Calculator
HTML-код
- Опубликовано: 15 сен 2024
- Get the Code : bit.ly/qt-tut2
Best Qt Book : amzn.to/2KvFTsQ
/ derekbanas
MY UDEMY COURSES ARE 87.5% OFF TIL January 16th ($9.99) One is FREE
➡️ Python Data Science Series for $9.99 : Highest Rated & Largest Python Udemy Course + 56 Hrs + 200 Videos + Data Science bit.ly/Master_...
➡️ C++ Programming Bootcamp Series for $9.99 : Over 23 Hrs + 53 Videos + Quizzes + Graded Assignments + New Videos Every Month bit.ly/C_Cours...
➡️ FREE 15 hour Golang Course!!! : bit.ly/go-tuto... (100 Available)
The most requested next C++ app you guys asked for was a calculator, so here it is. We'll make a C++ GUI Calculator app in one video. Along the way we'll learn a lot! We'll cover a topic which confuses many people which is how to setup event handling with Signals and Slots. We'll also cover how to work with widgets in many ways, setting up an interface, stylesheets, casting, regular expressions and much more.
Brought to you thanks to contributions from my Patreon subscribers such as perl6.org/
In case anyone is following the tutorial with Qt6: QRegExp is depreciated. To spare you a look in the documentation, the interior of the "CangeNumberSign()" function can be re-written as follows:
#include
// [...]
void Calculator::ChangeNumberSign() {
QString displayVal = ui->Display->text();
QRegularExpression reg("[-]?[0-9.]*");
QRegularExpressionMatch match = reg.match(displayVal);
if(match.hasMatch()) {
double dblDisplayVal = displayVal.toDouble();
double dblDisplayValSign = -1 * dblDisplayVal;
ui->Display->setText(QString::number(dblDisplayValSign));
}
}
@KiwiX No worries, glad I could be of help :)
Thank you very much, bro!
Thank youuuuu!!
Many many Thank YOU!!!
Thank you so much for the help
in case someone needs the other functions here's how I did it :
void Calculator::Memoire()
{
QString displayVal = ui->Display->text();
MemoireVal = displayVal.toDouble();
ui->Display->setText("");
}
void Calculator::MemoireCall()
{
ui->Display->setText(QString::number(MemoireVal));
}
void Calculator::MemoireClear()
{
MemoireVal = 0.0;
}
void Calculator::Clear()
{
ui->Display->setText("");
}
Hi Derek. Thanks for the helpful tutorial. For whomever it may help, I found a couple of bugs. First, numbers are tacked onto the end of the current display when number buttons are pressed. This is a problem once the display switches over to scientific notation. The numbers get appended to the exponent instead of the main number. This can easily be fixed by getting rid of newVal in the NumPressed method and replacing dblNewVal with "double dblNewVal = displayVal.toDouble() * 10 + butVal.toDouble();". So basically, you multiply the current display by 10 and add it to the currently entered digit. The second bug is that your regex excludes scientific notation numbers from being negated. Perhaps I am missing something, but it looks like there is no need for a regex at all. All of the input is limited to what you can press on the calculator, so regex seems like overkill. Maybe just get rid of the regex in ChangeNumberSign and replace the condition of the if statement with "QString::compare(displayVal, "") != 0". This way, if the +/- sign is pressed in between operations (when the screen is blank), nothing will happen to the sign. Also the setText call in the same method should use the 'g' flag again to switch to scientific notation after 16 digits.
Thanks for everything you do and the time you spend on helping us. As someone whose main language is not English I can say that your voice is so unique that it can be understood perfectly without subtitles. Keep it up and wish you the best!
Thank you very much :) That is great to know that so many people find these helpful
At last, QT Tutorial, that's what I've waited for ages. Thank You :)
I hope you find it useful :)
Hi can you teach me how he added button 7,0 1,3,4..
Derek, I can sit down and do these things, but my biggest blocker is I never have the environment setup right. Can you go over some "how to setup your environment in one video" lessons, and maybe do it for several environments (pycharm, several C++ free environments, setting up one with maybe other editors like that popular one, sublime text, maybe one for Mac vs Windows users. These environment setups really stop me from doing a lot.) Thanks!
I too had this problem man
Here's the link to a demo video
ruclips.net/video/i_ONRqKLfQs/видео.html
Some times I think. How can he knows all this things? Would you make a video teaching how to learn better?
I'm working on it. It is a matter of having the right tools and breaking learning up into bite sized pieces through out the day
@@derekbanas Qt has a lot of things in his documentation.
@@amirhassan6549 too much things for real, they should show us the technics they use.
(5 calculations into testing the app...) "THIS CALCULATOR HAS NO DECIMAL!!!" Super fun tutorial, can't wait to Qt it up some more.
That's funny :) Either way I'm happy you liked it
The speed is just right. Thanks for a really great tutorial.
Thank you very much :)
Such a deceptively simple concept, _a calculator._ But you learn SO MUCH by adding functionality to it. Like period support, backspace support, keyboard input support (hint: the "*" and "/" keys are called "Qt::Key::Key_Asterisk" and "Qt::Key::Key_Slash", not "Qt::Key::Key_division" and "Qt::Key::Key_multiply").
Thank you! This was incredibly educational.
21:54 That 'if' condition doesn't make any sense. Because ".toDouble()" gives you a 'double'. Don't compare it to an 'int'. Both conditions are identical.
27:09 "Qt::CaseInsensitive" seems unnecessary since there's no uppercase or lowercase there. Those are symbols.
31:11 Don't just blindly divide... Check if you're dividing by zero first! I'm surprised it doesn't crash. Qt must be catching exceptions under the hood.
Really nice packaged tutorial. I'm being enjoying a lot of your other tutorials in C++. Thank you Derek.
I might be add some ClearButton function like:
void colculator::on_Clear_clicked()
{
ui->Display->setText("0.0");
}
my version is:
void Calculator::on_btn_Clear_released()
{
double calcVal = 0.0;
ui->Display->setText("");
}
Best channel on RUclips and great teacher.......
Thank you for the nice compliment :)
Make tutorials on ai.
I'm working on it
@@derekbanas can you please tell me is Qt good for mobile (I mean both Android and iOS) development?
Spent hours trying to find out how to do some stuff before I found the answer here. Great video, thank you.
Thank you :) I'm happy I could help
i've just started to learn C++ and QT. immensly helpful video. thank you
thanks for this good tutorial.
Thank you for watching it :)
Sir, your videos give learners a perfect headstart. Expecting more videos...
Thank you very much :)
This great Derek. Real use applications and learning.
Thank you :) Many more are coming
Derek I have been in jail for about a year and just got out I thought of you and your videos every single day I was in jail you are an inspiration to me everything about your videos is amazing
Thank you very much :) I wish you all the best for your future
I don't know why but my program doesn't accept QRegExp but accepts QRegularExpression and again says Syntax Error.
I added #Include and then nextline with reg.exactMatch, it says exactMatch is no member in QRegularExpression..
Can you please help me???
Derek thankyou for the amazing tutorial. Would you consider doing a C++ tutorial for Unreal Engine 4?
I'll do anything if I get enough requests.
I don't fully understand the course yet. But I like the delivery. Thumbs up teacher!
Thank you very much :)
Thank you sir,we want lot of videos on different applications made by c++,qt.. it's possible by great like you sir...
Thank you :) I'm happy I could help
Excelente Mister Dereck..... , a ver si nos deja un ejemplo de Qt usando Golang que es mi lenguaje favorito
Great tut as usual! Just one comment it might have been good to put your Math function triggers into a Container object like QList or QMap, it would have demonstrated some of the useful general purpose data types that come with Qt. It also would have meant a few less lines of code ;)
wow another Qt tutorial. I really need to learn C++ to enjoy these
A bunch more are coming. Qt is great
Really, really good Tutorial! Thank you a lot for that, helped me greatly.
I'm happy I could help :)
19:07 That's the OLD way of connecting, using the "SIGNAL" and "SLOT" macros. Newer way uses the name of the class function.
*And don't use "released".*
Use "clicked" instead. Meaning: _&QPushButton::clicked_
Because with "released", you can click and, while holding down, drag away from the button, and it will register as clicked. 😐 You WANT the option to "cancel" a click by dragging away from it.
From Bangladesh, thanks Derek for another wonderful video, you are amazing!
You're very kind thank you :)
Some people *don't have the form file calculator.ui nor calculator.cpp nor calculator.h*
It's because that class has a different name (I think it's because of Qt edition they changed the way to name classes)
To solve this (for me it was mainwindow) just write *MainWindow::* instead of *Calculator::*
I hope it's clear why this is the solution
Man, I had a problem here 16:20
It says "no member named Display in Ui::Calculator"
Some body may help me? I'm a kinda freaking here kkk
Hey mate, a question: in Calculator::NumPressed() method, minute 22 -> why do you create a new QPushButton *button to get the sender ? is this not possible just by setting up a parameter in this method by default and sending the widget in the SLOT(NumPressed(widget)) ? . I come from PyQt this is why I'm asking this kind of question. thanks in advance
Amazing! You make it look so simple and fun to do! Great video! I'll start and check this out for myself. Thanks Derek...continue making great videos!
Thank you for the nice compliment :) Many more are coming
@@derekbanas
Hi can you teach me how he added button 7,0 1,3,4..
Nice! Thank you very much! - But shouldn't a calculator have at least a comma button?
Hi at the run time this demo on Linux machine I am getting this warning: QObject::connect: Cannot connect (nullptr)::clicked() to Calculator::NumPressed()
and during the pressing button nothing is happening ...
Hello< when i compile the program i got :
Warning: QT_DEVICE_PIXEL_RATIO is deprecated. Instead use:
QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
QT_SCREEN_SCALE_FACTORS to set per-screen DPI.
QT_SCALE_FACTOR to set the application global scale factor.
i've used qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", QByteArray("1")); in the main but still the same
Am I the only one horrified by the use of global variables?
Am I the only one horrified by the constant use of the word "aaaaanddd ... " ?
its not a problem for such small projects if one codes like he does in this video. Even less of a problem when using long meaningful names. For much bigger projects are definitely a minefield. I would not recommend their usage either, because a beginner is very likely to use short generic names where name clashing even in small code becomes a big issue and a big pain.
Yeah. If he was teaching proper coding styles, I would agree with you. I feel like he isn't though. More like how to use qt.
Hi can you teach me how he added button 7,0 1,3,4..
'QRegExp' was not declared in this scope. I get this error
my PC
2 GB ram
1.8ghz cpu
64 bit processor
No graphic card
Can qt run in my pc
No then what are light framework or editor for app development.
Thanks a lot for other good qt tutorial. Expecting more tutorials on qt.
I'm happy you liked them :)
why are we redefining the global variables? why is it wrong if we don't do that? could you explain it a bit further?
Hey thats incomplete tutorial, how will u render the app and give it logo and all. do u need to open IDE everytime and run it
Hi Derek, some years ago you made a series about refactoring. I think that this example will be a valuable cadidate for another splendid refactoring video. By the way, I found this video more interesting than the World Championship in Russia. :)
Thank you :) Yes I plan on making a bigger algorithms and refactoring tutorial series for C++. When I made one for Java I cut them a bit short because at the time nobody was watching them. I would have loved to watch the World Cup because I'm a soccer coach, but I've been obsessed with learning Japanese for the last 2 months.
I enjoyed watching this video
Thanks Derek
Thank you :)
please don't stop here. Make more videos on Qt :)
I'm planning at least 2 more
Derek, how could we implement the buttons to activate with the keyboard as an optional input method?
i need hmi program qt creator for converting (km/hr to m/s ) using labels(eg:pushbutton,label,lineedit)
Derek please on html5
With meta and division tags more covering
Here is an HTML5 tutorial ruclips.net/video/kDyJN7qQETA/видео.html and CSS3 ruclips.net/video/CUxH_rWSI1k/видео.html
I have an error QObject::connect: Cannot connect (nullptr)::released() to MainWindow::numPressed()
please help me i am getting this error
warning: no previous extern declaration for non-static variable.
Same here
watching(#learning) the tutorial during #coronatime #covid19 #quarantine
don't know when we'll be out of this...
thanks for the tutorial !! :)
-from India
I hope you and your family stay well :)
Actually I have a doubts the output should show like this (1+1+2*3/) how we get both numbers and operators in a display
fantastic loved it
member acces into incomplete type 'class Ui::Calculator'
I can't figure out what's wrong and why I am getting this error, anybody knows?
Your main ui file is not called Calculator.ui so you have instead mainwindows instead. Thus, you should use MainWindow:: everywhere he wrote Calculator::
I guess it's because we have different editions so the classes' names were different from the tutorial
gonna copy this comment outside also
Can you make incremental search function?
Hey, Derek! Thanks for creating these videos. Do you mind me asking how you learned Qt?
I'm happy you liked it :) Mainly from the website
Please upload more and more projects
Project building is a great way of learning.
Most important thing is how will you deploy this as a software having DLL and exe files where we can easily start our software
if you dont mind make a video on deployment of software.
thank you!
Hi Derek, should each widget have its own header file or each widget rather defined as a class in the .cpp?
Thanks for the tutorial! Is there a reason you used if else instead of a switch statement?
I'm happy it helped :) Personal preference was the reason
Really good Tutorial, thanks for posting
Thank you :) I'm happy I could help
When i type "double calcVal = 0.0;" there appears an error:
"no previous extern delcaration for non-static variable 'calcVal' ".
I'm doing everything EXACTLY the same like on video. Can't really find any solution in the internet. Can someone help me?
Put static in front of it and it will go away
duude you have many habits that are considered as "smelly" in Cpp. Global variables, complicated nested cs, deprecated casting style, not using modern features like auto keyword (which is boss in Qt5 btw) etc. Anyway that's a good work. Have a nice day!
But In qt 6 It will be go like that
QString display=ui->label->text();
QRegularExpression reg("[-]?[0-9.]*");
QRegularExpressionMatch match=reg.match(display);
if(match.hasMatch()){
double dblDisplay=display.toDouble();
double dblDisplayValSign=-1*dblDisplay;
ui->label->setText(QString::number(dblDisplayValSign));
}
you are the best, thanks for this tutorial
Thank you very much :)
Can not seem to figure out why it wont run. here is the compile output with the error.
\CalculatorTutorial.pro" -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug"
ASSERT: "fileName.isEmpty() || isAbsolutePath(fileName)" in file C:/Users/qt/work/qt/qtbase/qmake/library/ioutils.cpp, line 53
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
18:40:14: The process "C:\Qt\5.11.1\mingw53_32\bin\qmake.exe" exited with code 3.
Error while building/deploying project CalculatorTutorial (kit: Desktop Qt 5.11.1 MinGW 32bit)
When executing step "qmake"
18:40:14: Elapsed time: 00:07.
Very talented teacher! thanks.
Thank you very much :)
I get to 19:15 and I'm wondering where did you get the function release() from?
I'm having a rough time setting up my onclick listener with slots.
also up to this point, where do you specify the function of numPressed()? I apologize, I have a function written as a class in c++ and I can NOT for the life of my get it to activate when I press on a button.
Can you make a video on How to Learn a Programming Language like you? or How to be like you?
I mainly break things down into roman numeral lists and then memorize and maintain knowledge with a free program called Anki. Look for my Anki tutorial on youtube
Could you create tutorial where you explain how to connect qml and c++? Maybe any bigger project? I think that it's good idea.
Please consider my mind.
Yes I plan on covering it
When i can expect it? ;)
Sorry I don't have a definite time frame. ASAP
thanks for replying me back there and man you're like an idol too me,i just want to gain knowledge as much as you have i hope i can meet you someday can you please tell me how to get started into os development like a step by step approach ?
QT or RAD studio ?
Where we declared signal add
when I created the project, and got into Kit Selection, nothing was highlighted and I am not sure what to add, any help?
you should install the Qt again, i think the offline install is better.
The guy says that the style sheets are "very close to CSS", that's not entirely true since that's exactly CSS.
what does namespace ui{
class calculator;
} mean ?
Is it equal to declaring calculator class inside the namespace
We use namespaces to avoid conflicts if other code uses a function of the same name.
Very good tutorial thank you.
Thank you :)
How do you know so much man? I am amazed. Is there some way you learn or are you just that talented?
I'm not that smart. I'm sure of that. I'm going to upload a video soon on how I study, but I think I basically do the same as everyone else. Flash cards, studying all day in short bursts when I'm not busy, exercise when I get stressed out by learning, etc.
Here is a short day in my life.
1. Wake up and study while I drink a smoothie and coffee (Flash cards or book) (1 hr)
2. Shower and then work on regular job stuff (2 hrs)
3. Run 30 minutes
4. Work 1.5 hrs
5. Lunch while studying (Flash cards or book) 30 minutes
6. Work 2 hrs
7. Run 45 minutes
8. Study while drinking water (Flash cards or book) 30 minutes
9. Make dinner while studying 1 hr
10. After dinner I study or work during time remaining for the day
I hope that helps
Derek Banas I am quite sure I'll learn from that video as well..please upload as soon as possible.
Please try to do a tutorial for creating DLL files in C++
And keep up the good work
I'll see what I can do :)
Hey Derek! I'm going to be applying to some internships this year and I've heard that personal/side projects are a good way of increasing your chances of getting a job. What are some simple, easy, yet great personal coding projects to build that will look good on my resume? :)
Yes most definitely. It needs to be a project you are passionate about. I got hired at Apple mainly because I made a VR system with a custom head mounted display and a hacked Nintendo power glove. It doesn't matter what the project is, but it should be big and something you love merged with programming or electronics.
Wait you work at Apple? How come you never made a video called "What it's like working at Apple", or "How to get an interview at Apple?". I think a lot of people would love to see that.
Just one question sir how do you learned so many programming languages 😫😌😅
Please help! In the calculator.cpp at 15:14, my Qt says: allocation of incomplete type 'Ui::Calc'.
How do I fix this?
I have the code here for free www.newthinktank.com/2018/06/qt-tutorial-2-c-calculator/
I had this same problem, for me it was because while creating the project in the beginning I didn't rename Class Name from "MainWindow" to "Calculator". I then tried renaming it after the project was created and got that error.
Hey man I know am 2 years late and all but I am having a assembler message error can you please help
Derek i read somewhere in one of your videos. You use xamarin for mobile app dev , how is it compared to Qt ?. Qt also allows mobile dev and which framework or tech stack you use for web dev ?
Yes I use Xamarin and I much prefer it over Qt for mobile development
Oh okay. I have only used Qt so far for mobile app dev, never tried Xamarin till now maybe i will give it a shot
Very nice tutorial.
Thank you :)
Cool! Elin dert gormesin, gardash! Ko'p yashang!
Adobe animate tutorial?
Also thank you for all your tutorials.
Your hard work is appreciated.
Thank you for watching :) Sorry I don't own AA
No problem
Can you do any adobe cc tutorial or is it out of question?
Sorry I only have my outdated Adobe tutorials at the moment
Derek Banas Don't worry sir.
You don't have to do everything.
Hi,
Can you tell me that, if I can do it like this:
I have one button and I want to write in 2 different lineEdits. Like When I click on one lineEdits the buttons start writing on lineEdit1 if I click on lineEdit2 then it starts writing on linEdit2.
Is there any solution for that?
wtf it keeps giving me 0 when I press the equal button
First comment, first like, without watching the full video :D.
Thank you very much :)
Thanks dude
I'm happy to be of help :)
Very nice Video please continue with common use cases using/read/write configs/files, create settings editor, control/execute/detect system apps python bash zsh
Thank you :) I'll try to do my best
Sir can you teach it with gtk library too which is also a cross platform gui library and it is free. Because some major applications like VLC media player is also built on it according to my knowledge.
Please........................
I'll see what I can do
What's that font? Looks cool
Please do cmake next
I'll see what I can do
Thank you derek bananas :D
I'm happy to help :)
Hello, I realize this was done about a year ago, however I just found it today. I think, for my taste, you forgot the decimal point. I would really like to learn to make a nice "big" calculator for my laptop touch screen. I use Fedora 30 Mate, QT Creator 4.9.1, and know very little to nothing about programming. Your video had me with great hopes until I didn't see the decimal point, unless I missed something. If I did, I apologize. However, I have not found any video or online stuff for building a calculator that fits what I need. But still, Many many thanks for as far as this video took me. If you have any suggestions, I would more than appreciate it.
Sorry about the decimal. I made a JavaScript calculator with a decimal here and it would be easy to use that code as a guide to add that functionality ruclips.net/video/22QAmVctAMc/видео.html
ui->Display
this dont working, maybe ui->lineEdit ??
Do you have any tutorials on how to setup a website? Like from scratch to the top. One that is practical. Whether with WordPress or without it.
Do you mean the design? I have tutorials on all of the above.
I make a whole site design here ruclips.net/video/9fHSbiCISOA/видео.html
Wordpress design here ruclips.net/video/Ghnrxgk-jCc/видео.html
I have other tutorials on my channel for PHP, MySQL, JavaScript, JQuery, etc....
Derek Banas yeah, I saw them last night. Do you mean the one with designing a webpage and inkspace. I didn't get it how that designed the webpage, as it turned into a simple image. I mean like what is a host and setup a website from getting the domain and a host and how to connect a android app to the website. You teach very well and thank you.
If you get an account at a place like Go Daddy, or any other they'll have a thing called the control panel in which you upload your code. If you want to use Wordpress you'll click on a link on the site and it will install Wordpress. Do a search for the hosting company and Wordpress and they'll show you everything step by step. I hope that helps
Derek Banas ok, tnx. I also thought it would be a good topic to cover as the demand for it is at a high level and many people would enjoy it too. 😊🤗 or as a summary to all the topics related covered. Like build from scratch and update it using php or asp.net. thanks for your great channel ❤