2009-09-30

Change Timezone in Debian

>dpkg-reconfigure tzdata
Then follow the step

2009-09-28

Fix size of QWidget

To fix the size of QWidget:


//Label for image
QLabel * oLabelImageCenter = new QLabel;
oLabelImageCenter->setBackgroundRole(QPalette::Base);
oLabelImageCenter->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
oLabelImageCenter->setScaledContents(true);
oLabelImageCenter->setFixedSize(200, 160);

2009-09-26

colorgcc

Colorgcc is a perl script that makes the error and warning message of gcc be highlighted by different color. Nice tool from Johannes Schlüter.

>wget http://schlueters.de/colorgcc.1.3.2.txt
>mv colorgcc.1.3.2.txt colorgcc
>mkdir /usr/local/bin/colorgcc
>mv colorgcc /usr/local/bin/colorgcc
>cd /usr/local/bin/colorgcc
>ln -s ./colorgcc g++
>ln -s ./colorgcc gcc
>export PATH=/usr/local/bin/colorgcc:$PATH

2009-09-22

Compile with Qt State Machine Framework

1) You need to download Qt State machine Framework
2) >tar xvzf $(QtStateMachine).tar.gz
3) In your .pro file add a line:

include($(QtStateMachineInstalledPath)/src/qtstatemachine.pri)

4)> qmake then make

Color Highlight in VIM

"A color editor like Vim can improve the productivity of programmers by 2 to 3 times!!"

>apt-get install vim
Add following lines into ~/.vimrc



" Set syntax highlighting to always on
syntax enable

" Set the background to dark and the colorscheme to murphy
set background=dark

" Set automatic filetype detection to on
filetype on



Change Qt Widget Background

Qt Widget's Background is changed by setPalette(QPalette);
The "Qt Tutorial 8 - Preparing for Battle" is changed: add a button to change the background color of ConnonField.
========
main.cpp:
========

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));

QPushButton *change = new QPushButton(tr("Change"));

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

LCDRange *angle = new LCDRange;
angle->setRange(5, 70);

CannonField *cannonField = new CannonField;

connect(angle, SIGNAL(valueChanged(int)),
cannonField, SLOT(setAngle(int)));
connect(cannonField, SIGNAL(angleChanged(int)),
angle, SLOT(setValue(int)));
connect(change, SIGNAL(clicked()),
cannonField, SLOT(changeBackground()));

...
}


===========
cannonfield.cpp
============
...
void CannonField::changeBackground()
{
setPalette(QPalette(QColor(0, 250, 200)));
update();

}
...



2009-09-21

Fullscreen Qt widget

If your Qt executable is the single application running at your embedded system. It makes sense that your Qt widget is border-less and full-screen.
Max modifies the example in Qt tutorial 4:

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent, Qt::FramelessWindowHint)
{
//setFixedSize(200, 120);

QPushButton *quit = new QPushButton("Quit", this);
quit->setGeometry(62, 40, 75, 30);
quit->setFont(QFont("Times", 18, QFont::Bold));

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
//widget.show();
widget.showFullScreen ();
return app.exec();
}

Compile the code using Embedded Qt and run the binary among qvfb (See "Program and Test Qt Embedded Code on PC - virtual framebuffer"). The following snapshot is at 240x320 PDA configuration.

Use Class Declaration in C++ Header File

A good C++ programmer always seeks to let his header files as slim as possible. The following header file is a bad example, in which an unnecessary header file is included.

#ifndef MYCLASS_H
#define MYCLASS_H

#include <SuperTime>

//bad, slow down the compilation
#include "Number.h"

//good. class declaration is sufficient for a pointer variable
//class Number;

class MyTime : public SuperTime
{

public:
MyTime(SuperTime *parent = 0);
int value() const;
void setValue(int value);

void valueChanged(int newValue);
private:
Number *number;
};

#endif // MYCLASS_H


This makes the compilation of big projects much slower, because the compiler usually spends most of its time parsing header files, not the actual source code. As suggested in the comments, use a class declaration instead including a header file. The final binary files are same as before, but this trick alone can often speed up compilations by a factor of two or more. Belows are the example of declaration of the template class and class with namespace:





template < class L > List;
namespace card {
class MainBrowser;
}

2009-09-11

State Machine in Qt

Qt provides a two classes to implements state machines.
QtStateMachine offers the basic APIs for state machine mechanism, while QtScriptedStateMachine is a subclass of QtStateMachine and adds the missing parts to support SCXML. Max tried to implement the following state machine. The code is not yet tested, ;) Hope it works!


//selection state groups
QtState *pStateSelection = new QtState();
QtState *pStateSearch = new QtState(pStateSelection);
QtState *pStateBrowse = new QtState(pStateSelection);
pStateSelection->setInitialState(pStateBrowse);

//processing state groups
QtState *pStateProcessing = new QtState();
QtState *pStateMaterial = new QtState(pStateProcessing);
QtState *pStateProcedure = new QtState(pStateProcessing);
pStateProcessing->setInitialState(pStateMaterial);

//transitions from "Selection" state
pStateSelection->addTransition(buttonStart, SIGNAL(clicked()), pStateProcessing);

//transitions from "Browse" state
pStateBrowse->addTransition(buttonSearch, SIGNAL(clicked()), pStateSearch);

//transitions from "Search" state
pStateSearch->addTransition(buttonOK, SIGNAL(clicked()), pStateBrowse);
pStateSearch->addTransition(buttonCancel, SIGNAL(clicked()), pStateBrowse);

//transitions from "Processing" state
pStateProcessing->addTransition(buttonFinish, SIGNAL(clicked()), pStateSelection);
pStateProcessing->addTransition(buttonCancel, SIGNAL(clicked()), pStateSelection);

//transitions from "Material" state
pStateMaterial->addTransition(buttonProcess, SIGNAL(clicked()), pStateProcedure);

//transitions from "Procedure" state
pStateProcedure->addTransition(buttonMaterial, SIGNAL(clicked()), pStateMaterial);

//start the state-machine
QtStateMachine oMachine;
oMachine.addState(pStateSelection);
oMachine.addState(pStateProcessing);
aMachine.setInitialState(pStateSelection);

oMachine.start();

Reference:

2009-09-10

Create Sqlite Database by SQL Script.

Sqlite supports c/c++, Tcl and sql interfaces. Max wrote some sql scripts to create and initialize a single-table sqlite DB. Put the following three files in the same fold and execute createDB.sh.

1) createDB.sql (create a DB)

CREATE TABLE tb_master
(
Id integer PRIMARY KEY,
Name text NOT NULL UNIQUE,
Creator text,
Group_name varchar(20),
Time_stamp integer,
Snapshot blob,
Thumbnail blob,
Ingredient text,
Workflow text
);

2) demoData.sql (insert demo data)


INSERT INTO tb_master
VALUES
(
1,
'Applepipe',
'Max',
'Cake',
10,
'ddd',
'ddd',
'dddd',
'ssss'
);
INSERT INTO tb_master
VALUES
(
2,
'Chinaball',
'Jasmin',
'Soup',
11,
'ddd',
'ddd',
'dddd',
'ssss'
);



3) createDB.sh (main shell script)

#!/bin/sh
# This is a script to create a sqlite3 database "test.db"

#local variables
DB=test.db
CREATE_SCRIPT=createDB.sql
DATA_SCRIPT=demoData.sql

#remove database file if it exists.
if [ -f $DB ]
then
echo remove $DB
rm -rf $DB
fi


#create a database with sql script
echo create $DB
sqlite3 $DB < $CREATE_SCRIPT

#initialize with testing data
echo insert records
sqlite3 $DB < $DATA_SCRIPT

2009-09-09

Compile c/c++ code with sqlite3 library.

1) Install sqlite3 library.
  • >wget http://www.sqlite.org/sqlite-amalgamation-3.6.17.tar.gz
  • >tar xvfz qlite-amalgamation-3.6.17.tar.gz
  • >cd sqlite-3.6.17
  • >./configure
  • >make
  • >make install

The library libsqlite3.la now is installed to /usr/bin/...

2) Compile as following. Try the test code.
  • >gcc -o test test.c -lpthread -ldl -lsqlite3

2009-09-08

First impression of SQLite3

"SQLite is not designed to replace Oracle. It is designed to replace fopen()."

What a clear signal! SQLite's target is not a "enterprise database engine" but a samll, simple but smart database for applications.

Key features
  1. Embedded devices and applications
  2. Zero-Configuration
  3. Single Database File
  4. Stable Cross-Platform Database File
  5. C/C++ API
  6. SQL Compatibility
  7. 5 Datatypes In SQLite Version 3: NULL, INTEGER,REAL,TEXT,BLOB
  8. License in public domain (total free in product)
    References

    Putty in chinese

    A good article to configure Putty to see Chinese.

    Override assigment operators in C#

    A C++ class can implement assignment operators to
    1) assign itself, or
    2) convert the other class to itself.
    See the example.

    Unlike C++, C# does not support override of assignment operator. C# uses "implicit" key word to replace the assignment operator. See the example here.

    2009-09-07

    Program and Test Qt Embedded Code on PC - virtual framebuffer


    "Qt for Embedded Linux applications write directly to the framebuffer, eliminating the need for the X Window System and saving memory. For development and debugging purposes, a virtual framebuffer can be used, allowing Qt for Embedded Linux programs to be developed on a desktop machine, without switching between consoles and X11." (Source: http://doc.trolltech.com)

    0) You need a linux host, I have debian Lenny at home.
    1) Download Qt for X11. The Library source (*.tar.gz), not the SDK (*.bin).
    2) Installing Qt/X11. You may meet following error message during ./configure.

    You might need to modify the include and library search paths by editing QMAKE_INCDIR_X11 and QMAKE_LIBDIR_X11 in /home/lolveley/bin/qt-x11-opensource-src-4.5.1/mkspecs/linux-g++.

    The reason is the some dependencies are missing. Try this:
    • > apt-get build-dep qt4-qmake

    3) Qt for Embedded Linux library must be configured and compiled with the -qvfb option

    • > ./configure -qvfb
    • > make
    • >make install

    4) Compile and run tool qvfb (Qt virtual framebuffer) as a normal Qt for X11 application.
    • >cd path/to/Qt/tools/qvfb
    • >make
    • >make install
    If you encounter the error "/usr/bin/ld: cannot find -lXtst", you need install "libxtst-dev".

    5) Running embedded applications using the virtual framebuffer. Note: try the examples compiled with embedded Qt, not X11. Otherwise, you may encounter the error like "qvfb: cannot connect to X server". See example here.


    Reference .


    2009-09-04

    Create a ISO file in linux

    How to create a iso file?
    Forget about Nero, forget about MagicISO. These windows software just waste time and money. In Linux everything is just a line of command.

    >dd if=/dev/cdrom of=2440.iso
    5597332+0 records in
    5597332+0 records out
    2865833984 bytes (2.9 GB) copied, 430.944 s, 6.7 MB/s

    Maybe you want to mount this iso file
    > mkdir /dev/2440
    >mount -o loop 2440.iso /dev/2440/

    Or you want to mount the iso file at system startup, then add one line into /etc/fstab
    /$(yourpath)/2440.iso /mnt/2440 iso9660 ro,loop,auto 0 0