Getting Started with Qtopia

From the horse's mouth (Trolltech) come instructions on how to get started writing applications for Qtopia.
Setting Up the Development Environment

We need to set some environmental variables for our KDevelop project. Run KDevelop, and then click on Project→Project Options→Run Options. Add these variables:

Name: QTDIR Value: /opt/Qtopia
Name: QPEDIR Value: /opt/Qtopia
Name PATH Value: /opt/Qtopia/bin:$PATH
Name LD_LIBRARY_PATH Value: /opt/Qtopia/lib:$LD_LIBRARY_PATH

Similarly, add to the Make Options, for desktop development:

Name: QTDIR Value: /opt/Qtopia
Name: QPEDIR Value: /opt/Qtopia
Name PATH Value: /opt/Qtopia/bin:/opt/Qtopia/tmake/bin:$PATH
Name LD_LIBRARY_PATH Value: /opt/Qtopia/lib:$LD_LIBRARY_PATH
Name TMAKEPATH Value:/opt/Qtopia/tmake/lib/qws/linux-generic-g++

Add -lqtopia to the LIBS line in skizzy.pro, as Qtopia 1.7 adds a new library.

At this point, you need to generate a Makefile manually, as KDevelop does not use tmake correctly:

# export TMAKEPATH=/opt/Qtopia/tmake/lib/qws/linux-generic-g++
# tmake -o Makefile skizzy.pro

Then you can build the project (F8) from within KDevelop. This little glitch will be resolved in newer versions of Qtopia that use qmake to generate Makefiles.

Getting Gritty

Let's add some functionality to skizzy.

Start the Designer 2 application and open skizzybase.ui, and delete the QLabel. Add a QTabWidget with a QComboBox on the first tab, a QListBox on the second tab and a QMultiLineEdit on the third tab, for example (Figure 2).

Figure 2. If you use QLayouts, it allows your application to resize depending on the display resolution or screen rotation.

Save the .ui file.

Open the file skizzy.cpp with KDevelop. You will see our application is derived from skizzyBase:

skizzy::skizzy( QWidget* parent,  const char* name, WFlags fl )
    : skizzyBase( parent, name, fl )

I want to change main.cpp to a better method of constructing the application that was added since the time that the Qtopia templates for KDevelop were created.

We change the usual main() function:


int main( int argc, char ** argv )
{
    QPEApplication a( argc, argv );
    skizzy mw;
    a.showMainWidget( &mw );
    return a.exec();
}

to Qtopia's application macro:

QTOPIA_ADD_APPLICATION("skizzy",skizzy);
QTOPIA_MAIN

This allows us to create a quicklaunch application, which helps speed up startup time, by using the common application constructor that is already in memory.

My application skizzy doesn't do anything yet, so include:


#include <qpe/fontdatabase.h>

Add a private member:

FontDatabase fdb;

and some functions as private slots:


private slots:
void fillCombo();
void comboSelected(const QString &);
void showFont( QListBoxItem *);

We need to add a few lines in skizzy.cpp here for things we will use:


#include <qstringlist.h>
#include <qcombobox.h>
#include <qtabwidget.h>
#include <qlistbox.h>
#include <qmultilineedit.h>
#include <qfont.h>
#include <qfontinfo.h>

#include <qpe/fontdatabase.h>

and then add the implementations, to which we will connect our widgets signals:


/*
This function uses Qtopia's FontDatabase to
fill the combobox with a list of font names.*/
void skizzy::fillCombo()
{
	QStringList families = fontdb.families();
  	for ( QStringList::Iterator f = families.begin(); f != families.end();++f ) {
      	QString family = *f;
		ComboBox1->insertItem( family);
	}
}

/*
This gets called when the combobox is selected, and
fills the listbox on the second tab with the name,
style and point size for the family of fonts
selected, and raises it. */
void skizzy::comboSelected(const QString &selectedFont)
{
ListBox1->clear();

 QStringList styles = fdb.styles( selectedFont );
  	for ( QStringList::Iterator s = styles.begin(); s != styles.end();++s ) {
              QString style = *s;
              QValueList<int> smoothies = fdb.smoothSizes( selectedFont, style );
               for ( QValueList<int>::Iterator points = smoothies.begin(); points != smoothies.end(); ++points ) {
                   QString pointSize = selectedFont + " "+ style +" "+QString::number( *points ) + " ";
                  ListBox1 ->insertItem( pointSize);
               }
           }
    TabWidget2->showPage(tab2);
}

/*
This shows example text of the selected font in
the QMultiLineWidget on the 3rd tab, and raises it.*/
void skizzy::showFont( QListBoxItem *item)
{
	QStringList fontItemString = QStringList::split(' ',item->text());
	QString family, style, point;

    family = fontItemString[0];
    style = fontItemString[1];
    point = fontItemString[2];
    bool ok;
    int i_size = point.toInt(&ok,10);

    if (!ok) {
        style += " "+fontItemString[2];
        point = fontItemString[3];
        i_size = point.toInt(&ok,10);
    }

    QFont selectedFont( family);
    selectedFont.setPointSize(i_size);

     if(style.find("Italic",0,TRUE) != -1) {
          selectedFont.setItalic(TRUE);
     }

     if(style.find("Bold",0,TRUE) != -1) {
        selectedFont.setWeight(QFont::Bold);
     }

     if(style.find("Light",0,TRUE) != -1) {
        selectedFont.setWeight(QFont::Light);
    }

    MultiLineEdit1->setFont( selectedFont);
    MultiLineEdit1->setText( tr( "The Quick Brown Fox Jumps Over The Lazy Dog" ) );
    MultiLineEdit1->setWordWrap( QMultiLineEdit::WidgetWidth);

	TabWidget2->showPage(tab3);
}

Qt and Qtopia use signals for sending messages between widgets. Every widget has some kind of signal it emits, and we can use the connect macros to link up functionality.

Connect ComboBox1's activated signal, and ListBox1's clicked signal to our slots, like this:


skizzy::skizzy( QWidget* parent,  const char* name, WFlags fl )
    : skizzyBase( parent, name, fl )
{
    connect(bye, SIGNAL(clicked()), this, SLOT(goodBye()));
    connect(ComboBox1, SIGNAL(activated(const QString &)), this, SLOT(comboSelected(const QString &)));
    connect(ListBox1, SIGNAL( clicked ( QListBoxItem * )), this, SLOT(showFont( QListBoxItem*)));
    fillCombo();
}

Notice how the slot function takes an argument of the exact same type as the signal.

In KDevelop, either press F8, or in the menu, select Build→Build Project.

It should now compile, using the native compiler. To run it, start up QVFb, and simply select Build→Execute Main Program. The skizzy application should show up in QVFb.

Figure 3. Skizzy with real, if not useful features.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

How to install Qtopia core for arm?

Anonymous's picture

I'd be grateful if you could tell me step by step how to install Qtopia for arm. Whether there are any compatible (more importantly incompatible) toolchains for Qtopia core to work. I tried to compile Qtopia core 4.2.2 for arm by giving ./configure -embedded arm, and the make failed. I am using the toolchain with binutils 2.17 and gcc 4.1.1. My native gcc has version 4.0.0
Qtopia core has compiled successfully for my i.386 machine.

Qtopia application deployment

Sakthi's picture

The article is wonderful and it covers the things for beginners...

But we need some more guidelines on 'Deployment of advanced
Qtopia application on target board' because any small application
could be deployed to the target board without any problem..

So we need some more guidelines about the deployment of application
that uses pim library ...developed using Qtopia 1.7 sdk but the
target has qtopia 1.5 ( does not have any pim libraries )

Thanks

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState