Documente Academic
Documente Profesional
Documente Cultură
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++.
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.
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.
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 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
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; }
switch ( nPick ) {
case C_to_K:
case C_to_F:
case F_to_C:
case F_to_K:
case K_to_C:
case K_to_F:
default:
//the message just in the case that something is gone wrong way...
QMessageBox::warning(this,
tr("Error"),
tr("Something went wrong!"));
}
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.
... 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…