2010-01-29

Useful Qmake Variables

The reference of qmake variable can be found http://doc.trolltech.com/4.6/qmake-variable-reference.html. Two of useful variables

OBJECTS_DIR = .obj
MOC_DIR = .moc

specifiy the location of temporary files. In above example, they are located at the .obj and .moc.

2010-01-28

Z Value of GraphicsItem

Z value of each QGraphicsItem determines its stacking order of visibility in parent items.
It can be explicitly specified by setZValue( qrea z ) . Otherwise, it is decided by the order of being assigned to its parent through these two functions:
  • QGraphicsItem::setParentItem ( QGraphicsItem * parent )
  • QGraphicsItemGroup::addToGroup ( QGraphicsItem * item )

Use qmake include file pri

Qmake use pro file to define the project, while pri file is the include file for pro file. For example, the Qt state machine modual has a "qtstatemachine.pri" for external inclusion, you need to simply add

include(../../externals/qtstatemachine/src/qtstatemachine.pri)

into pro file to compily your code together with Qt state machine framework.
You can also optimise the modulise your code for external usage. Take an example, I have implemented a clock object by several classes. The classical pro file may looks like:

######## clock.pro #########################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += clStateMachine.h \
clThread.h

SOURCES += clStateMachine.cpp \
clThread.cpp \
main.cpp

include(../../externals/qtstatemachine/src/qtstatemachine.pri)
#######################################

Clearly, main.cpp is for the purpose of testing the object, the clock object is implemented in the other file. If you want to reuse this object in the other project, which may be located at the different path, you'd better to use pri file to modulize your object implmentation. Create a clock.pri at the same directory like:

######## clock.pri #########################
INCLUDEPATH += $$PWD
HEADERS += $$PWD/clStateMachine.h \
$$PWD/clThread.h

SOURCES += $$PWD/clStateMachine.cpp \
$$PWD/clThread.cpp

include($$PWD/../../externals/qtstatemachine/src/qtstatemachine.pri)

########################################

The old clock.pro is adapted like:

######### clock.pro (new) ###################
TEMPLATE = app
TARGET =
DEPENDPATH += .

# Input

SOURCES += main.cpp

include(./clock.pri)
########################################

Now you include your clock.pri in any other project by adding the following line into the pro of your project

include({relative or absolute path of clock.pri})

2010-01-27

Qt update its examples pages

Looks much better.
http://qt.nokia.com/doc/4.6/examples.html

Set SVN Global Ignore Pattern

Edit the file ~/.subversion/config and uncomment global-ignores = *.* list. Add any file pattern you want to be ignored. thats all.

reference:

Q_PROPERTY example

class CLLcd : public QLCDNumber
{
Q_OBJECT
Q_PROPERTY( bool editable READ editable WRITE setEditable )
public:
bool editable(){return editable_; }
void setEditable(bool editable_in){editable_=editable_in;}
private:
bool editable_;
}

2010-01-22

Make LCDNumber Transparent



MyClockLcd::MyClockLcd ( QWidget *parent )
:QLCDNumber(parent)
{
//show decimal
setMode( Dec );

//segment style
setSegmentStyle( Filled );

// transparent background
setAttribute( Qt::WA_NoBackground );

//no frame
setFrameStyle( QFrame::NoFrame );
display("00:00");

}

2010-01-21

Java-style Iterator vs. STL-style Iterator

The best comparison of Java-style and STL-style iterator is given in this article: http://doc.trolltech.com/4.5/containers.html#the-foreach-keyword. Some citations for summary:


  • Java-style iterator. "..Unlike STL-style iterators (covered below), Java-style iterators point between items rather than directly at items. For this reason, they are either pointing to the very beginning of the container (before the first item), at the very end of the container (after the last item), or between two items..."

  • STL-style iterator: "...The API of the STL iterators is modelled on pointers in an array. For example, the ++ operator advances the iterator to the next item, and the * operator returns the item that the iterator points to..."

Foreach in Qt Container

Great! Qt container supports foreach iteration, which simplies largely the code. See the example for comparison:


========= java-like iterator===============
QLinkedList<> list;
...
QLinkedListIterator<> i(list);
while (i.hasNext())
qDebug()<< i.next()

========== STL-like iterator=============
QLinkedList<> list;
...
QListedList<>::const_iterator it = list.begin();
while( it!= list.end())
{
qDebug()<< (*it); ++it; }

=========foreach iteration===============

QLinkedList<>> list;
...
foreach (QString str, list)
qDebug() << str

===================================

Reference:

2010-01-20

Debug Qt Application with gdb

If the Qt application is built in Linux system (Max uses Debian Lenny), you need to just run
  • > gdb app
See the thread http://jingfenghanmax.blogspot.com/2010/01/useful-gdb-commands.html

However, you need to add debug option into makefile by add line into *.pro file
  • CONFIG += config qt
Note: "+=" instead of "=", otherwise, the application may have problem when it tries to load the resource files defined in qrc file.

tracert

Ping shows whether the server is online, while tracert checks the way to reach the server.
See the comparison at http://www.werle.com/intagent/k11_4.htm

2010-01-19

LCD Clock among Qt









Useful gdb commands

  1. gdb main //start debug
  2. run //run target
  3. backtrace //show the calling stack at the point of fault
  4. x 0xbff972e0 // examine the value in memory
  5. break 52 or break LinkedList::remove //set break
  6. condition 1 item_to_remove==1 //set conditional break
  7. s //step
  8. c //continue
  9. quit //quit
To make qmake generated debug info, a line need to add into *.pro file

>CONFIG = qt opengl debug

iostream vs. iostream.h

A frequent piece of advice is often given to new C++ programmers is to use instead of or instead of . This is often given with only the explanation that the .h forms are deprecated without explaining what the difference is and why, in fact, using the extensionless version is superior. See the reference:

Two Ways to create QGraphicsProxyWidget

The QGraphicsProxyWidget class provides a proxy layer for embedding a QWidget in a QGraphicsScene. There are two ways to create QGraphicsProxyWidget:

  • use QGraphicsScene::addWidget. Obviously, you need available QGraphicsScene object by hand. See bellow
QGraphicsScene scene;
QGraphicsProxyWidget *proxy = scene.addWidget(groupBox);
  • Use QGraphicsProxyWidge::setWidget. And you can add this QGraphicsProxyWidget to scene afterwards
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
proxy->setWidget(groupBox);




Let Children of QGraphicsItemGroup handle their own event

QGraphicsItem can have its own child QGraphicsItem objects. However, QGraphicsItem has no API (like setItems) to add its child. It is only possible allow the child attach to the parent, like
QGraphicsItemGroup, a subclass of QGraphicsItem, allows to add and remove children
But a very very important function, we must pay attention to:
If "enabled=true", QGraphicsItemGroup will handle all the events. For example, the event of mouse click on the child item won't be handled by child item.

If "enabled=false", QGraphicsItem Group will not block the child item's event and let child item handle it own event.