[Interest] Creating QML Component instances in C++

Unai IRIGOYEN u.irigoyen at gmail.com
Mon Apr 2 19:36:28 CEST 2012


Hi,
I 'm not sure it' the solution to your problem but I do create object 
instances from delegates in one of my classes and the objects are visible.
I don't have the code with me right now but the only difference I think there 
is from your code to mine is that I set the parent property of my newly 
created object so il is linked in the QML tree.
So try adding:
obj->setProperty("parent", QVariant::fromValue<QDeclarativeItem*>(this));

Of course you can change "this" by the pointer to the desired QDeclarativeItem 
if needed.
Good luck!

PS: I have seen tha you used obj->setParent(QObject*) but I don't think this 
has some effect on QML rendering.

Le dimanche 1 avril 2012 23:50:32, Gabriel M. Beddingfield a écrit :
> All:
> 
> I'm trying to make a container, similar to GridView.  I can set a delegate
> and create instances of them, but I can't seem to make them visible.  What
> am I doing wrong?
> 
> To illustrate my problem, I've set up a minimal example (attached) with a
> custom Component called "FourSquare."  It's supposed to lay out four of the
> delegates in a 2x2 grid.  The expected results of the program is to see 4
> red squares.  However, I don't see them... all I see is the background.
> 
> void FourSquare::update_layout()
> {
>     int rows, cols, r, c;
>     int x, y, m;
>     QObject *obj;
> 
>     if (!m_delegate)
>         return;
> 
>     rows = 2;
>     cols = 2;
>     m = 2; /* margin */
>     y = 0;
>     for (r = 0 ; r < rows ; ++r) {
>         x = 0;
>         for (c = 0 ; c < cols ; ++c) {
>             if (!m_children[r][c]) {
>                 obj = m_delegate->create();
>                 obj->setParent(this);
>             }
>             obj->setProperty("x", x);
>             obj->setProperty("y", y);
>             obj->setProperty("width", m_cellWidth);
>             obj->setProperty("height", m_cellHeight);
>             x += m_cellWidth + m;
>         }
>         y += m_cellHeight + m;
>     }
> }
> 
> Thanks in advance!
> 
> -gabriel
> 
> p.s. I'm using Qt 4.7.4 on Ubuntu 11.10 (x86).
-- 


Unai IRIGOYEN

Ingénieur
Mail: u.irigoyen at gmail.com
Tel: +33 (0)6 10 68 50 15

Profils sociaux professionnels:
Viadeo: http://wwww.viadeo.com/fr/profile/unai.irigoyen
LinkedIn: http://fr.linkedin.com/in/irigoyenunai




More information about the Interest mailing list