Like all Eletrical Engineer students thats frequents the laboratory class rooms, i needed to keep in my mind the color code to know the resistance of resistors. The picture below show you this code:
Then , i build a Qt application to solution my problem using the QPainter class.
The QWidget contains a method called paintEvent(QPaintEvent *event) to receive paint events passed like event parameter which can be reimplemented in a subclass . The paint event is a request to repaintall or part of widget . In this post we will do it , that is , repaint part of widget.
Drawing :
At Start, i created a brawn QLinearGradient to use at QPainter object background:
After that , i created a QPainter and i set the pen and the brush using the gradient created above :
painter = new QPainter(this);
painter->setRenderHint(QPainter::Antialiasing,true);
painter->setPen(QPen(gradient,2));
painter->setBrush(QBrush(gradient));
In this step i used the method drawPie to draw a pie defined into a rectangle and two angles : the start angle and finish angle.In this case the start is 30 degrees and finish is 300 degrees.
painter->drawPie(30,280,120,120,30*16,300*16);
The same is done here:
painter->drawPie(400,280,120,120,210*16,300*16);
Here , i drawed a simple rectangle:
painter->drawRect(90,310,370,60);
Using the same logic above, when the user clicked at calculate button i drawed more four rectangles
to represent the colors of resistor setted.At this moment too, i calculated the resistance in accordance
with the table showed at starts of this post.
In this post, I will present how build a .deb package . This is a usual implementation ,but the difference is the Qt Maemo application . Other problem that this post will to solve is how to put the Qt application at Menu of Maemo Device.
.Deb Packages In Brief:
Deb is the extension of the Debian software package format and the most often used name for such binary packages. Like the Deb part of the term Debian. Debian packages are used in distributions based on Debian, such as Ubuntu and others.
Prerequisites:
You need to have libqt4-dev ,Scratchbox and Maemo SDK installed on your Computer and libqt4-dev installed on your Scratchbox ARM target and your Maemo Device . To more informations of ScratchBox Installation click here and to Qt on Maemo click here.
Creating the Application:
You need to create a simple Qt HelloWorld Project on computer. I created a project with two labels how is showed below . Remember of to set the size of QWidget to 720 of Width and 420 of Height because the resolution of Maemo Device.
Copying the Application to Scratchbox:
First , copy the sources files of application to a folder at Scratchbox. In general the address is /scratchbox/users/username/home/username/. I do this:
You’ll need to create a control file for your package using a simple text edit. In this post I use the Vi.
[sbox-DIABLO_ARMEL: ~] > vi HelloWorldApplication/DEBIAN/control
In this step we’ll look at what each field in the control file means and what kind of information you need to provide The syntax for the control fields is very simple: the name of the field, followed by a colon, and then the body or value of the field.
Package: HelloWorld
Priority: optional
Version: 1.0
Architecture: all
Maintainer: Flavio Fabricio <flaviofabricio@gmail.com>
Depends: libqt4-dev
Description: This is a HelloWorld Application
Returns to HelloWorld Application directory and create the folders below:
[sbox-DIABLO_ARMEL: ~] > cd HelloWorldApplication/
[sbox-DIABLO_ARMEL: ~/HelloWorldApplication] > mkdir usr
Creating the /usr subdirectories:
[sbox-DIABLO_ARMEL: ~/HelloWorldApplication] > cd usr/
[sbox-DIABLO_ARMEL: ~/HelloWorldApplication/usr] > mkdir bin
[sbox-DIABLO_ARMEL: ~/HelloWorldApplication/usr] > mkdir lib
[sbox-DIABLO_ARMEL: ~/HelloWorldApplication/usr] > mkdir share
[sbox-DIABLO_ARMEL: ~] > cd HelloWorld
[sbox-DIABLO_ARMEL: ~/HelloWorld] > ls
HelloWorld helloworld.cpp main.cpp helloworld.png
HelloWorld.pro helloworld.h helloworld.ui ui_helloworld.h
In this step, you will to copy the executable file to /usr/bin folder. This executable is called by helloworld.desktop at line Exec=/usr/bin/HelloWorld on the file described above.
You will need of a picture to use like Icon. You can search some image at the Google Images.In this case I renamed the picture to helloworld.png.This image is called by helloworld.desktop at line Icon=helloworld on the file described above and sended to /usr/share/pixmaps/ folder.
I used the Scp command with the IP address of device. The IP address of device is searched at Settings->Connection Manager , after that , click at Connection Manager at left-Up-Corner->internet connection->IP address.
To do a SOAP webservice request using QtSOAP, is simple, the two main objects that you’re going to need from QtSOAP area a QtSoapHttpTransport, that in this example it’s going to be created in the constructor and a QtSoapMessage, that you can see it use later.
In this example the SOAP client call a method called “sum” that sum two numbers that are passed as parameters.
First of all let’s implement the client.h file:
#include <QDebug>
#include <qtsoap.h>
class Client : public QObject
{
Q_OBJECT
public:
QtSoapHttpTransport http;
int number1;
int number2;
Client(int number1, int number2, QObject *parent = 0);
void soma();
private slots:
void getResponse();
};
#endif
As we could see the numbers that are going to be in the method parameters are passed in constructor of Client class.
Now implement the client.cpp
#include "client.h"
Client::Client(int number1, int number2, QObject *parent) : QObject(parent)
{
this->number1 = number1;
this->number2 = number2;
http.setHost(host, port);
// Or use just http.setHost(host)
connect(&http, SIGNAL(responseReady()), this, SLOT(getResponse()));
}
void Client::sum()
{
QtSoapMessage request;
request.setMethod("sum");
request.addMethodArgument("number1", "", number1);
request.addMethodArgument("number2", "", number2);
qDebug("sum of %i and %i...", number1, number2);
http.submitRequest(request, "/soma");
}
void Client::getResponse()
{
const QtSoapMessage &message = http.getResponse();
if (message.isFault()) {
qDebug("Error: %s", qPrintable(message.faultString().toString()));
}
qDebug()<< message.returnValue().toString();
}
In the constructor of Client class the two numbers are passed as parameter and the QtSoapHttpTransport host is set.
The sum method is the most important one, first of all you have to create the request message, that contains the method that will be called and the parameters of it. After the creation of message just submit the request to QtSoapHttpTransport host in the right path. The getResponse slot is very important, when the QtSoapHttpTransport receives the response a signal is emitted and enters in this slot, where the response message is going to be shown.
After implementing client.h and client.cpp you just have to implement the main.cpp that is gonna use this functions and it’s done.
Qt provides two classes for serialization, DataStream class to serialization of binary data and QTextStream to “parsing” input stream(reading and writing text). QDataStream class can be used to write portables files (i.e., a data stream written under Windows can be read by other running GNU/Linux or Solaris).
Thus, there are situations that serialization of binary data are very useful to us, for example, create a dictionary like a configuration file. In our example, we write, update and read a QHash(our dictionary).See the list of data types which can be serialized here
#include <QtCore>
#include <QApplication>
#include <QHash>
#include <QFile>
#include <QDataStream>
#define PATH "./file.dat"
int main()
{
//create a dictionary
QHash<QString,QString> dict;
dict["project.owner"] = “owner”;
dict["project.version"] = “0.10.0″;
QFile file(PATH);
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // write the data
out << dict;
file.close();
//setting new a value
dict["project.owner"] = “new”;
//update the dictionary
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file
in >> dict;
qDebug() << “value: ” << dict.value(”project.owner”);
return 0;
}
LibQxt adds some great functionality to the qt toolkit. One of them is a simple framework for building webservers and services. So here is a simple webservice that adds two numbers and returns the result back to who ever is sending the request.
Obs: the complete source code is available for download below.
First we create our service that will add the numbers and return the result. To do this we extend QxtWebServiceDirectory and reimplement the “indexRequested()” function so we can parse the message and get the parameters.
The xml parser is implemented using the qt library, however, the focus here is how to create the webservice.
indexRequested() is called whenever someone sends something to the index url of the service. So if we create a service called “add” and someone sends a SOAP request to “http://SOMEIP:PORT/add/”, indexRequested() will be called. indexRequested() receives a QxtWebRequestEvent*, which is an object that contains all the request information sent by the client. It contains the request headers, session Id, request Id and the message itself.
Below is the code for indexRequested() and buildResponse(). buildResponse() builds the response(duh!) using the SOAP protocol, so the client can understand what is being sent back.
We now create a webserver by extending QxtHttpSessionManager and reimplementing the “incomingRequest()” function, so we can see if someone is requesting something.
For doing colored logging using QtDebug, you need to install a custom message handler (a sink for all qDebug(), qWarn(), qCritical(), qFatal() log messages). This can be done using the qInstallMsgHandler(handler) function, as follows. Here we also define color macros and a macro for formatting our message to “<time> <level> <message>”.
Done that, export logInitialize() and call it on the beginning of your application. Notice that now all your QtDebug-based logging messages show neat colors and timestamps.
In this post I will show an basic example of a Qt application which uses QXmlStreamReader and QXmlStreamWriter objects to write and read XML files . I will also show how to change icon of QPushbuttonaccording to its state (Unchecked or Checked).
WRITING XML:
When we selected the fields with the informations and clicked on FileMenu->SaveAs the function SaveXMLFIle() is called . Then, a dialog will request for to save the filename. Once the XML is created and is showed on QTextEdit.
When we clicked on FileMenu->Open the function OpenXMLofFIle() is called . Is opened a QFileDialog and the file is choosen. After the the XML is showed on QTextEdit and the others fields are filled.
When the PushButton of the Lamp is clicked, its picture is changed according to state (ON or OFF).On the XmlUpnp class constructor is started two QIcons with two pictures. The first is a Off lamp and the second is a ON Lamp. At the moment of the click on the Button ,the on_pushButtonX_toggled() function is called changing the picture to according of the Lamp state , (Checked=Off or Unchecked=On).
The start screen of application shows four pushbuttons and for each pushbutton, a Label with the state of the lamp, a QComboBox with the room where the lamp is , other QComboBox with the lamp potential and a QTextEdit with the XML code.
All fields are clean waiting the user change the value or open a XML file at FileMenu->Open .Then the XML file is showed on the QTextEdit and the others fields are filled with the information on the XML opened.
Sometimes it’s usefull to know the ip address from your local network interfaces, especially if you are using mobile or embedded devices, it’s sometimes difficult now and then to get this information going through the settings menu, (i’ve personally struggled to get the ip address off a 5800 xpress music phone), so here is how you do it:
// First create a QNetworkInterface object.
QNetworkInterface interface;
// Then declare a QList using QHostAddress as the list type.
// After this, you can simply use ".allAddresses()" method to get ALL the
// network interface information, easy enough no?
QList<QHostAddress> IpList = interface.allAddresses();
// After that you can display the ip numbers on the console or do whatever you
// want with them.
for (int i = 0; i < IpList.size(); i++)
cout << "Interface " << i << ":" << IpList.at(i).toString() << endl;
When compiling software using Qt for s60 or OpenC++, you can come across some problems. One of the very first problems is when you code on some other OS(linux for example) and port your code to windows for compiling and testing on the device. If most of the problems have something to do with the header file “_limits.h”, there is a simple solution. All you have to do is copy the folowing to your .mmp file:
OPTION CW -wchar_t on
MACRO _WCHAR_T_DECLARED
If you are using Qt for S60 and dont know how to add the lines to your mmp file, since when you run qmake, the mmp file is rebuild with the .pro expecifications. then all you have to do is add the following lines to your .pro file:
In this post, I will present all steps To Developing a first Qt/C++ applications for S60 mobile phones . I recorded a full video of all necessary softwares installation that allow the development of mobile phone applications.
Necessary Softwares :
Nokia Pc Suite ->Is a software package used to establish an interface between Nokia mobile devices and computers.
Carbide C++ ->Is a software development tool for C++ development on Symbian OS. It is used to develop phones that use the OS, as well as applications that run on those phones.
Active Perl -> ActivePerl is the industry-standard Perl distribution, available for Windows, Linux, Mac OS X, etc..
Java Runtime Environment(JRE)->Is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts.
S60 Sdk 3rd Edition Fp1 or higher->A software development kit (SDK or “devkit”) is typically a set of development tools that allows a software engineer to create applications for a certain software framework, hardware platform, computer system, video game console, operating system, or similar platform.
(In this Post we Installed Sdk 3rd Edition and Sdk 5rd Edition)
OpenC/C++ for S60 *3rd Edition Fp1-> is a development environment from Nokia for the S60 platform.
(Automatically included on SDK 3rd Edition Fp2 and higher)
Qt for S60 “Garden”->Qt is a powerful C++ application development framework, which makes it easy for developers to create applications once and then deploy them on any of the Windows, Mac, Linux, Windows CE, Windows Mobile and embedded Linux platforms. With the inclusion of the S60 platform, developers have an additional 80 million* target devices that they can support with their Qt-based applications.
First Video :
Step 1:
Download and Install Nokia Pc Suite at address: Nokia PC Suite.
Step 2:
Before the download and installation of Carbide is necessary a free Registration on Forum Nokia webSite and after Download and Install Nokia Carbide at address: Carbide C++ .
Step 3:
At the end of Carbide Installation was showed the link of ActivePerl download . You can use this or go to the address : Active Perlfor download and install the carbide.
Step 4:
In this step download and install Java Runtime Environment(JRE) on the addres : JRE.
Step 5:
The address for the fifth step is SDK 3rd Edition Fp1 and you can download and install the SDK 3rd Edition FP1.
Step 6:
The sixth step show you how to install the OpenC/C++ for S60 3rd Edition using the address OPENC/C++ SDK 3RD .
Step 7 :(Optional)
The seventh step is Optional because the developer use the Sdk specified for the mobile phone edition . On ForumNokia WebSite is possible view the edition of each mobile Nokia Devices Specifications .For the download and installation of the SDK 5th Edition use SDK 5th Edition .
Second Video :
Step 8 :
In this step we will show you how to install the most important tool of this Post : “The Qt Garden” . On the S60 Pre Releases Qt Garden you will get many informations and details about qt installation . The step by step of qt installation is on Step-by-Step Qt Installation .
Step 9 :
The ninth step show a important point of qt installation . It’s the fifth step of tutorial step by step above and show you how to extract Qt binaries of Qt Garden directories to SDK directories.
Third Video :
Step 10:
This step show you how to adjust the patches of QT to SDK installation . This is on Qt Patches.
Fourth Video :
Step 11:
The eleventh step show you all need configurations for the normal use of the Carbide C++.
Step 12 and Step 13:
These steps show you finally how to Build a Example Project for S60 Mobile Phone since the 3rd Edition until 5th Edition devices.