[Qt-interest] Adding QGraphicsScene to another widget...

Malyushytsky, Alex alex at wai.com
Thu Sep 3 22:45:54 CEST 2009


Check lifetime of your objects.

The way you allocate
     QGraphicsScene scene;
     QGraphicsView view(&scene);
Make them do be created and destroyed in constructor of  MainWidget.
So they don't survive long enough even to be shown when MainWidget is shown.

Regards,
     Alex

From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Topi Hukkanen
Sent: Thursday, September 03, 2009 1:24 PM
To: qt-interest at trolltech.com
Subject: [Qt-interest] Adding QGraphicsScene to another widget...

Hi All,

I've been struggling with this issue for a while now... I was wondering if anyone could help me out.

I'm trying to put a QGraphicsSvgItem into a QGraphicsScene into a normal QWidget.

When I do the following, everything works fine:

--------------------EXAMPLE 1--------------------------------
----------------------------main.cpp-----------------------------


#include <QtGui>
#include "qtpropertyanimation.h"
#include <QGraphicsSvgItem>
#include <QSvgRenderer>
#include "infobox.h"
#include <QPushButton>
#include <QHBoxLayout>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QWidget mainWidget;
    QHBoxLayout *layout = new QHBoxLayout(&mainWidget);

    QSvgRenderer *renderer = new QSvgRenderer(QString("gfx.svg"));
    InfoBox *infoBox = new InfoBox; // QGraphicsSvgItem
    infoBox->setSharedRenderer(renderer);
    infoBox->setElementId(QString("info-box"));
    renderer->setViewBox(QRect(0,0,300,300));
    infoBox->setZValue(0);

    QGraphicsScene scene;
    scene.setSceneRect(0, 0, 300, 300);
    scene.setBackgroundBrush(Qt::black);
    scene.addItem(infoBox);

    QRectF mySceneRect = scene.sceneRect();
    qDebug() << "mySceneRect: " << mySceneRect.width() << " " << mySceneRect.height();

    infoBox->setRect(mySceneRect);

    QGraphicsView view(&scene);
    view.setFrameStyle(0);
    view.setAlignment(Qt::AlignLeft | Qt::AlignTop);
    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setFixedSize(300, 300);
    //view.show();

    QtPropertyAnimation *myAnim = new QtPropertyAnimation(infoBox, "percentSize");
    myAnim->setDuration(10000);
    myAnim->setEasingCurve(QtEasingCurve::OutElastic);
    myAnim->setStartValue(0);
    myAnim->setEndValue(1);
    myAnim->start();

    layout->addWidget(&view);
    mainWidget.setLayout(layout);
    mainWidget.show();

    return app.exec();
}

-----------------------------------------------------------------------------------------

So in that example, my SVG "info-box" appears on the screen and the widget is 300x300.

However, if I try to pull the derive from QWidget, the QGraphicsScene stops appearing.


--------------------EXAMPLE 2--------------------------------
----------------------------main.cpp-----------------------------

#include <QtGui>
#include "mainwidget.h"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    MainWidget mainWidget;
    mainWidget.show();

    return app.exec();
}


------------------------mainwidget.h--------------------------

#include <QtGui>
#include "qtpropertyanimation.h"
#include <QGraphicsSvgItem>
#include <QSvgRenderer>
#include "infobox.h"
#include <QPushButton>
#include <QHBoxLayout>

class MainWidget : public QWidget
{
    Q_OBJECT

public:

    MainWidget(QWidget *parent = 0) : QWidget(parent)
    {
    qDebug() << "MainWidget::MainWidget() =>";

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setSpacing(5);

    QSvgRenderer *renderer = new QSvgRenderer(QString("gfx.svg"));
    InfoBox *infoBox = new InfoBox;
    infoBox->setSharedRenderer(renderer);
    infoBox->setElementId(QString("info-box"));
    renderer->setViewBox(QRect(0,0,300,300));
    infoBox->setZValue(0);

    QGraphicsScene scene;
    scene.setSceneRect(0, 0, 300, 300);
    scene.setBackgroundBrush(Qt::black);
    scene.addItem(infoBox);

    QRectF mySceneRect = scene.sceneRect();
    qDebug() << "mySceneRect: " << mySceneRect.width() << " " << mySceneRect.height();

    infoBox->setRect(mySceneRect);

    QGraphicsView view(&scene);
    view.setFrameStyle(0);
    view.setAlignment(Qt::AlignLeft | Qt::AlignTop);
    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setFixedSize(300, 300);
    //view.show();

    QtPropertyAnimation *myAnim = new QtPropertyAnimation(infoBox, "percentSize");
    myAnim->setDuration(10000);
    myAnim->setEasingCurve(QtEasingCurve::OutElastic);
    myAnim->setStartValue(0);
    myAnim->setEndValue(1);
    myAnim->start();

    layout->addWidget(&view);

    setLayout(layout);

    qDebug() << "MainWidget::MainWidget() <=";
    }


};

--------------------------------------------------------------------------------

In EXAMPLE2, nothing shows up in the widget... it's the minimum size for ubuntu linux ~10x10 pixels.

Can anyone tell what I'm doing wrong?  Is this some blatantly obvious thing that I'm overlooking?


Regards,
Topi


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."

"Please consider our environment before printing this email."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090903/b9af231c/attachment.html 


More information about the Qt-interest-old mailing list