Hi,
I have a QGraphicsItem
(actually, a QDeclarativeItem
) and I want it to take up the entire visible space of the QGraphicsView
(again, its actually the derived QDeclarativeView
class) to which it was added. Normally, you can use QDeclarativeView::setResizeMode(QDeclarativeView::SizeRootObjectToView)
and QDeclarativeView
will automatically resize the root object to fit the view.
The problem I'm having is that I am manually creating the root widget using
QDeclarativeComponent component(declarativeView->engine(), QUrl(qml));
QDeclarativeItem* object = qobject_cast<QDeclarativeItem*>(component.create());
if (object)
{
declarativeView->scene()->addItem(object);
...
instead of letting QDeclarativeView
do it automatically by calling setSource()
. The reason I'm doing this is because I want to swap the QML scene when certain events occur, but I don't want to destroy the previous scene. Calling setSource()
deletes all items that were added before setSource()
is called. So, instead I'm creating the "root object" myself and adding it to the scene manually.
I am using the windows resizeEvent to resize my QDeclarativeItem
, like this:
void AppWindow::resizeEvent (QResizeEvent* event)
{
QDeclarativeItem* object = sceneCache.value(sceneName, 0);
if (object)
{
object->setWidth(declarativeView->viewport()->width());
object->setHeight(declarativeView->viewport()->height());
}
}
This does work! But, its not very pretty. If you resize the window quickly, the QDeclarativeItem
is not resized quickly enough and you briefly see a gray background before it catches up and resizes it. Its also not very smooth.
This only happens if I have a complex item being resized (in my case, its a QWebKit widget). It works fine for simpler items. The thing is, however, that if I let QDeclarativeView
do it, I have neither of these problems: its resized correctly and smoothly.
I imagine this is not specific to the QtDeclarative stuff, but rather QGraphicsView, though maybe I'm wrong there.
Does anyone have any ideas?