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

Topi Hukkanen topi.hukkanen at gmail.com
Thu Sep 3 22:23:36 CEST 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090903/85bf17f5/attachment.html 


More information about the Qt-interest-old mailing list