Qt Help! String to number?

+2 Pete Mettaprasert · November 17, 2014
Can someone show me a simple Qt program area of a circle code that I can input and learn. I finished the tutorial but I want to create an application for numbers. This is just a simple put in a number, click a button, and display another number on to the label. Anybody have a good tutorial for that?

Post a Reply


Oldest  Newest  Rating
0 Geoffrey Wade · November 22, 2014
Well I replied and it didnt post, so I am gonna post it again lol.
create a global variable for the pi, do not use pi as the var. The variable pi is used way back somewhere by the meta-object compiler in reference to graphical orientations, (You can check the literature on it to figure exactly why that is if you want, i just know that is) call it: double MyCirc = 3.14.. use 16 places in the variable for accuracy. Then import the cmath and math.h headers. Use the pow built in from std library (also include <cstdlib> if preferred) and then use the raise to power function (pow) with a strategic intermediate variable: as shown below:

area = Mycirc*r^2; // im the area of a circle

 * assuming your radius could always be changing then xrad would be global
 * and r would pertain to the computation performed in the local function
 * this of course is beyond the scope of your post but food for though
 * upon possible applications

double r = xrad; 

QString myTemp = ""; // create the string intermediate

myTemp =  QString::number( pow( r, 2)); // calculate radius squared

double myTemp1 = 0; // initialize it

myTemp1 = myTemp.toDouble(); // stripped to double

myTemp = ""; // clear the intermediate string

myTemp += QString::number(MyCirc * myTemp1);

myTemp1 = 0; // clear the intermediate double value

total = myTemp;

myTemp = ""; // clear the intermediate for further computation
 * output the area to the preferred object ( this case a QLineEdit)

lineEdit -> setText(total);

total = "";
xrad = 0;

I actually made some mistakes in the first reply. Haven't coded any
Qt in about a week. Ive been applying to job postings like it was a full time job!
I had forgotten the area of a circle. durrr!! circumrea = 2*pi*r^durrrr
(Like dude ive taken through multi variable calculus! Where is my brain today)

P.S. you probably want to make your function void and go back to Bjarnes rules of class abstraction and inheritance meaning you would want to kick it off the operation by using a global boolean "trigger". Its best not to return a value, if a function or class abstraction suffices. Or you could just bring the function itself into the top-level frame's or widget's (e.g. QDialog or QMainWindow) constructor and then create an object with in the int main of the top-level frame widget. Or you could just print it to the command line (standard out) in which case all code written and computations performed are done so with in the body of the int main(int argc, char *arg[]); In this case it wouldn't be a Qt widgets application. I dont know if you are using NetBeans, Trolltech, or Ubuntu's sdk, but there is slight differences in all three Ide's with how they handle the Qt framework. It would be Qt console program, or use QtCore.

Ive actually written a top notch 40+ operation scientific calculator in Qt. Hard coded everything. I do not use the palette or designer. That thing messes me up if i decided to change may layout, so I hard code.

Anyways Best of Luck,
you got any more questions let me know.
And if you got some work Please Let me know my student loans for grad school are coming due.

0 Pete Mettaprasert · November 17, 2014
I think it is my knowledge of C++. It just seems a lot of applications are text based rather than mathematical and functions. I have a problem understanding Qt as well. I can do what Bucky does in the tutorials and I'm looking to read more from a book I recently acquired. I'm trying to experiment and try writing code, but I am also scared I'm going to be really frustrated due to my lack of theory knowledge and examples. I don't have a good background. But I just need to build a simple GUI and it seems that the amount of information drops dramatically when Qt or GTK is inserted next to C++ or C. 
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator