Monday, April 5, 2010

Transparent QImage or QPixmap picture

To create transparent picture use QPainter's method setCompositionMode and QPixmap filled with transparent color.

Code example:

QImage image("123.png");

// Create new picture for transparent
QPixmap transparent(image.size());
// Do transparency
transparent.fill(Qt::transparent);
QPainter p;
p.begin(&transparent);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.drawPixmap(0, 0, QPixmap::fromImage(image));
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);

// Set transparency level to 150 (possible values are 0-255)
// The alpha channel of a color specifies the transparency effect, 
// 0 represents a fully transparent color, while 255 represents
// a fully opaque color.
p.fillRect(transparent.rect(), QColor(0, 0, 0, 150));
p.end();

//put semi-transparent picture to QLabel 
QLabel* label = new QLabel(); 
label->setPixmap(transparent);

Thursday, April 1, 2010

Qt & VS 2005/2008

Using Visual Studio for QT development sometimes occurs link errors connected with moc files.
Code example:

Error 1 error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall cDirectXWidget::metaObject(void)const " (?metaObject@cDirectXWidget@@UBEPBUQMetaObject@@XZ) mainQT.obj
Error 2 error LNK2001: unresolved external symbol "public: virtual void * __thiscall cDirectXWidget::qt_metacast(char const *)" (?qt_metacast@cDirectXWidget@@UAEPAXPBD@Z) mainQT.obj
Error 3 error LNK2001: unresolved external symbol "public: virtual int __thiscall cDirectXWidget::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@cDirectXWidget@@UAEHW4Call@QMetaObject@@HPAPAX@Z) mainQT.obj


To solve them sure that you have installed qt integrator for visual studio. If not you may download in here. It helps.

If integrator is installed then:
  • delete all moc_*.cpp files
  • exclude all headers with Q_OBJECT macro from project
  • add all excluded headers to project
  • click "Build" and pray. :)
Also you may have such error like:

Error 2 error LNK2019: unresolved external symbol "int __cdecl qInitResources_App(void)" (?qInitResources_App@@YAHXZ) referenced in function main main.obj


It means that you didn't link to you project auto generated file named like qrc_*.cpp. In this file all functions like qInitResource are defined.