Message box


switch(QMessageBox::warning(this, "Warning",
"Database file is opened,\n"
"please close it first.\n"
"Close database file?",
"Yes", "No", 0, 1)) {
case 0:
db.close(); // close database
dbOpened = false;
openFile(); // open file dialog
break; // pay attention to here
case 1:
break; // and here

Timer event


#include “qtimer.h”

in main.cpp main function add:
startTimer(100) //start timer, and timer event triggered every 100 ms

PS: it is a little bit strange here. i don't need to declare a QTimer class. i may find the reason in the future since i am a just beginner in Qt

in main.h add:
void timerEvent(QTimerEvent *event);

in main.cpp add:
void MainWindow::timerEvent(QTimerEvent *event


my concern was right, my previous code was wrong. i actually calling the system timer event, not the timer which i created.

here is the correct code:

in main.h add:

QTimer *timer1;
void timerEvent();

in main.cpp add:

timer1 = new QTimer(this); // start timer event
connect(timer1, SIGNAL(timeout()), this, SLOT(timerEvent()));


Find application icon


in KDE:

$ kde-config --path icon
will list all icon directory.


$ gnome-config –datadir

will list a path which contains a folder called “pixmaps”.

Get total record count (SQLite)

my_sqlModel = new QSqlQueryModel(); //define a new sql model
my_sqlModel.setQuery(“select * from item”);
int cols = my_sqlModel.rowCount();

Qt Installation in Linux

(Qt 在Linux上的安装) Qt SDK
2.go to the downloaded file folder:
On Linux/Unix, need to make the file executable in order to run it. You can either do this with your desktop's file manager or, at the command line, type:

$ chmod u+x qt-sdk-linux-x86-opensource-2009.02.bin

should now be able to execute the file as normal. to do this from the command line by typing:

$ ./qt-sdk-linux-x86-opensource-2009.02.bin

3.install GNU C++ compliler:

$ sudo apt-get install g++
$ sudo apt-get install build-essential

Create second window/form/dialog


Add New.. Qt – Qt Designer Form Class
Need to include newform.h in mainwindow.cpp file where has slot to open this new window;
Add "extern newDialog *newdialog" in mainwindow.cpp;
"newDialog *newdialog" in newwindow.cpp;

Then, in click-button which open new window slot, add:

void MainWindow::on_pushButton_clicked()
   if (!newdialog)

=  new newDialog (NULL);

The above method creates dialog in heap,  so when i click close(), i don't delete it from memory. To close dialog or window completely, the code needs to be changed as:

void MainWindow::on_pushButton_clicked()
     newDialog newdialog(NULL);

we can not use:;

show() does not block, so you'll only see a quick flash of your dialog, as it'll be destroyed immediately after calling show() ( when exiting the method ). Using exec() blocks until dialog is closed by the user. 

if create new a window/dialog by using code, and did not pre-create window/dialog from “add new.. QT Designer form class”, the new window/dialog may be no shows on the top. This is because that the new window/dialog does not have a parent window/dialog. In order to make sure the new window/dialog shows on the top, use “add new.. QT Designer form class” create a new window/dialog first.

Globle variabal definition


Assume I have a.cpp b.cpp c.cpp and I need use integer i in all three of them.

I declare extern int i in globvar.h file;

Define int i in globevar.cpp file;

#include globvar.h file in all three .cpp files.

Varible definition

parameter definition should be in .h file. If this variable is used in this window/form (qqWidget.h), just declare in “private:”. But if used in anther window/form, should declare in “public:”.

// globalvar.h

extern int variable1;
extern int variable2;


// globalvar.cpp implementation

int variable1= 0;
int variable = 0;

Qt is not qute

my qt program's gui looks like normal...

need to add style header in the including file:

#include "mainwindow.h"

int main(int argc, char *argv[])
QApplication a(argc, argv);
MainWindow w;;
a.setStyle(new QPlastiqueStyle); //this command must be in front of ”return a.exec()”
return a.exec();


Qt4.5 Installation in Windows

(Qt4.5 安装)
to install Qt, need to download and install all three files from Qt website:

after drag and drop component, no need to declare it again in project.
but need to add ui-> in front of each used component if you wanna to use/edit it.


2D barcodes creator/decoder -- Libdmtx


Libdmtx is a 2D barcode, or Data Matrix barcodes, creator and decoder.

it took me a while to install it. here is mainly procedure of installation:

download libdmtx-0.7.0.tar.gz from
$ tar -zxf libdmtx-0.7.0.tar.gz

go to new libdmtx folder
$ ./configure

i got an error message:
checking for MAGICK... no
configure: error: dmtxread/dmtxwrite requires Wand >= 6.2.4
that's because i am using ubuntu. alright, i need to install ImageMagick from the source.
download ImageMagick source file, unpacked,
$ ./configure
$ make
$ sudo make install

(edit: I may need this:
$ sudo apt-get install libmagick++-dev
$ sudo apt-get install libgraphicsmagick1-dev

i may need to install g++ before install ImageMagick
$ sudo apt-get install g++

got anther error when i ran ./configure:
/usr/bin/ld: cannot find -lperl

ok, need to install libperl
$ sudo apt-get install libperl-dev

then, go back to install ImageMagick, and libdmtx.

all done..

To read a 2D barcode:
$ dmtxread /~/Desktop/image.png  
$ dmtxread -N 1 /home/czhang/mypro/UIM.jpg >> /home/czhang/mypro/UIM.txt
read barcode and export code to text file.

PS: This command will take a while (maybe 5 to 15 seconds) to finish decoding a 2D barcode image.

To speed up decoding, take a look command option. 

To create a 2D barcode:
$ dmtxwrite <(echo -n 2dbarcodecontent) -o image.png

get web-cam capture software from here:

Fedora install:
root$ yum install fswebcam

may need to install GDlib:

take a photo with webcam:
$ fswebcam -F 15 -r 352x288 --no-banner --save /mnt/cdrive/UIM.jpg