[Interest] Creating QML Component instances in C++

43 views
Skip to first unread message

Gabriel M. Beddingfield

unread,
Apr 2, 2012, 12:50:32 AM4/2/12
to inte...@qt-project.org

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).

delegates.tar.bz2

Unai IRIGOYEN

unread,
Apr 2, 2012, 1:36:28 PM4/2/12
to inte...@qt-project.org
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.

--


Unai IRIGOYEN

Ingénieur
Mail: u.iri...@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

_______________________________________________
Interest mailing list
Inte...@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Gabriel M. Beddingfield

unread,
Apr 2, 2012, 7:25:20 PM4/2/12
to Unai IRIGOYEN, inte...@qt-project.org
On 04/02/2012 12:36 PM, Unai IRIGOYEN wrote:
> So try adding:
> obj->setProperty("parent", QVariant::fromValue<QDeclarativeItem*>(this));

That did the trick! Thanks!

-gabriel

Reply all
Reply to author
Forward
0 new messages