PDA

View Full Version : Casting QGraphicsItem child from QGraphicsItem



patrik08
28th August 2008, 21:24
Why on qt4.4 i can not qgraphicsitem_cast child item?
on other lower version is possibel ... is this a Bug?




d:/qt4.4/include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h: In function `
T qgraphicsitem_cast(QGraphicsItem*) [with T = AbsoluteLayer*]':
textapi\Layer_Auto_Extended.cpp:226: instantiated from here
d:/qt4.4/include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h:945: error: ag
gregate value used where an integer was expected
d:/qt4.4/include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h:945: error: ag
gregate value used where an integer was expected
mingw32-make[1]: *** [build/.obj/Layer_Auto_Extended.o] Error 1
mingw32-make[1]: Leaving directory `C:/_dev2/luxor_svn/fop_miniscribus.2.0.r1/sr
c'
mingw32-make: *** [debug] Error 2


this is the error code piece....



/* check if absolute layer need a new page document */
void TextLayer::childAreaCheck()
{
/////// qDebug() << "### childArea ";
AbsoluteLayer *itemabsolute = 0;
QList<QGraphicsItem *> subLevelItems = childItems();
qreal lastdown = 400.0; /* A4:2 */

for (int i = 0; i < subLevelItems.size(); ++i) {

if (itemabsolute = qgraphicsitem_cast<AbsoluteLayer *>(subLevelItems[i]) ) {
itemabsolute->UpdatePageFormat();
lastdown = qMax (lastdown,itemabsolute->pos().y());
}

}

QPointF lastpoint(50.,lastdown);
////////qDebug() << "### childArea lastpoint " << lastpoint;
const QRectF allpageRect = boundingRect();
if (!allpageRect.contains(lastpoint)) {
QTextCursor Tcursor = textCursor();
Tcursor.movePosition(QTextCursor::End);
Tcursor.beginEditBlock();
Tcursor.insertText(" \t");
Tcursor.setBlockFormat(PageBreackBlock());
Tcursor.endEditBlock();
}
}

/* type is declared */

enum { Typex = UserType + 3 };
int type() const { return Typex; }

jacek
29th August 2008, 00:37
The docs say:
To enable use of qgraphicsitem_cast() with a custom item, reimplement this function and declare a Type enum value equal to your custom item's type.
You don't have the Type enum, but Typex.

patrik08
29th August 2008, 07:36
I solved on other Type to makeit readable ....





enum { Layer = 1 };
int type() const {return Layer;}


template <class T> inline T layer_cast(QGraphicsItem *item)
{
return int(static_cast<T>(0)->Layer) == int(AbsoluteLayer::Layer)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}

template <class T> inline T layer_cast(const QGraphicsItem *item)
{
return int(static_cast<T>(0)->Layer) == int(AbsoluteLayer::Layer)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}



AbsText::AbsText()
: device(0)
{

}
/* only one connect */
LayerText *AbsText::txtControl() const
{
if (!device) {
AbsoluteLayer *that = layer_cast<AbsoluteLayer *>(q);
device = new LayerText(); /* text-api all event */
connect(device, SIGNAL(q_cursor_newPos() ),q, SLOT(cursor_wake_up()));
connect(device, SIGNAL(q_update(QRect) ),q, SLOT(updatearea(QRect)));
connect(device, SIGNAL(q_visible(QRectF) ),q, SLOT(ensureVisible(QRectF)));
///////connect(device, SIGNAL(q_update_scene()),q, SLOT(SceneReload()));
}
return device;
}

jacek
29th August 2008, 16:37
You have to change Type to Layer in lines #8 and #14 too.