Sunteți pe pagina 1din 5

T H E T E M P E R AT U R E C O N V E R T E R W I T H Q t G U I A N D C + +

INTRODUCTION

This is an introductory article, from which the one will be able to gain some basic knowledge of the GUI applications
developed under Qt and C++.

In this article, the one needs to have active knowledge of:


 C++,
 Linux will be great,
 and some basic ideas about Qt.

Also, the one will need to have:


 Qt ready,
 C++ ready,
 and some desire, to learn new stuff about creating GUI applications ready.

If you are eager to learn now, let’s see, together, how to create an application with the graphical user interface.

MAIN PART

In this article, the converter of the temperature is developed. As we should know, the temperature is some quantity
that expresses how cold or hot an object is. The one measures already mentioned quantity, with some of known
scales.
The best know scale is the Celsius one, however there are Kelvin’s and Fahrenhight’s scales. There are few more,
less known, though.

Let’s see what are the challenges, needed to be addressed by the application :
• The very first thing that is needed, is widget that will take the string form the user, and convert it into the
double value. The widget is used in order to figure out the temperature measured.
• The second thing we need, is to set from which scale the input value is converted into which scale.
• The third, we need to present the result to a user.

In the application, the input from the user is taken as a string, and then converted into the double number. This
operation is crucial in many situations, but in order to keep it all simple at the start phase, we will assume that the
user “knows what is it doing”.

Let’s start …
One needs to create GUI application and provides the name for its class. In this particular case, the class
ctemperatureconvertor is used.

The Qt creator has few important folders:


• Headers,
• Sources,
• Forms.

In the first one a header files are stored, in the second one a source files are added and the last, but not the least, is a
form folder, which is used in order to create the form and add some widgets.
In this particular case the content of the folders is:
• Headers contain: ctemperatureconvertor.h,
• Sources contain: ctemperatureconvertor.h and main.h
• Forms contain: ctemperatureconvertor.ui.

As we need to create the graphical user interface, let us start with that.

In our application we will use:


• Two display widget- utilized in order to explain the purpose of the rest of the widgets used.
• One input widget- that is used in order to take input from the user and convert it into the double value.
• Six radio buttons- which are there, in order to figure out the type of the conversion.

The name of the first label is inputLabel and it explains how the textInput is used. The name of the second label is
pickLabel and it explains how the radioButtons are used.
The name of the widget used in order to input the string is valueInput.
The radio buttons, utilized in order to set from which scale the input will be used, are called: CtoK_Button,
CtoF_Button, KtoF_Button, KtoC_Button, FtoC_Button and FtoK_Button.
Then one needs the button to perform the conversion, that button is named ConvertButton.

In order to place the widget onto the form, all you need to do is “pick the widget and place it on the form”. Then you
set its property and signals.

Okay, let’s prepare our form. Since we don’t need: the status bar, the menu bar and the tool bar. Well, you just
remove them for this application.
Then the property the Window Title is changed into “The Temperature Converter”
In order to be able to perform some actions, the one need to set up the slot. The slot is created if you press the right
button on the mouse and pick clicked() …
Yeah, that is it.

Now the codding time, and the code for the button is:

//the set of macros used for the switch


#define C_to_K 1
#define C_to_F 2
#define F_to_C 3
#define F_to_K 4
#define K_to_C 5
#define K_to_F 6

//the variable used in order to get the pick from the user

int nPick = 0;

//the double value is obtained from the user and converted into double value

double dInputValue = ui->valueInput->text().toDouble(),


dResult = 0.0;

//then we check which button is checked

if( ui->CtoK_Button->isChecked() )
{ nPick = C_to_K; }
else if( ui->CtoF_Button->isChecked() )
{ nPick = C_to_F; }
else if( ui->FtoC_Button->isChecked() )
{ nPick = F_to_C; }
else if( ui->FtoK_Button->isChecked() )
{ nPick = F_to_K; }
else if( ui->KtoC_Button->isChecked() )
{ nPick = K_to_C; }
else if(ui->KtoF_Button->isChecked() )
{ nPick = K_to_F; }

//according to the pick the temperature is converted

switch ( nPick ) {

case C_to_K:

dResult = dInputValue + 273.15;


break;

case C_to_F:

dResult = dInputValue * ( 9.0/5.0 ) + 32;


break;

case F_to_C:

dResult = (dInputValue - 32.0)*(5.0/9.0);


break;

case F_to_K:

dResult = (dInputValue + 459.67)*(5.0/9.0);


break;

case K_to_C:

dResult = dInputValue – 273.15;


break;

case K_to_F:

dResult = dInputValue*(9.0/5.0) - 459.67;


break;

default:

//the message just in the case that something is gone wrong way...
QMessageBox::warning(this,
tr("Error"),
tr("Something went wrong!"));
}

//now the result is presented in one message box

QmessageBox::information(this,
"The converted value",
QString("%1").arg(dResult,0,'f',4));
}

The code is simple and that is due to the fact that the aim of this article is to provide some short introduction. So, if th
one is interested in this then the constants could be placed into the header file and some different codes with some
higher level programming could be added.
There is no explanation of the code, due to the fact that the code is well commented and mostly self explanatory.

Okay, there is the picture of a running application left to be seen,

... and,

thats it…

CONCLUSION

If you have figure out all, there should be an application created and in the case that you have some more knowledge,
you can do all you wish to do with your application and even share that with the friends of yours…

S-ar putea să vă placă și