PDA

View Full Version : MDI display Item in QGraphicsView



wisconxing
4th November 2008, 02:31
Use Qt 4.3.2, I have a mainwindow and a button, to open a MDI subwindow to display some GraphicsItem in a scene in GraphicsView. I can open subwindow,but can't see anything.
follow is my program. Does anyone know?
//------- main.cpp --------------
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;

mainWin.show();
return app.exec();
}

//------- mainwindow.h ----------
#include <QMainWindow>

class QMdiArea;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
private:
QMdiArea *mdiArea;
};
#endif

//-------- mainwindow.cpp -----------
void MainWindow::disp()
{ //draw a sun Item in scene,and put scene into view
QGraphicsEllipseItem *sun = new QGraphicsEllipseItem(0,0,50,50);
sun->setBrush(Qt::red);
sun->setPen(QPen(Qt::red));

QGraphicsScene scene(0, 0, 60,60);
scene.addItem(sun);

QGraphicsView view(&scene);

QMdiSubWindow *subWindow = mdiArea->addSubWindow(&view);

subWindow->show();
}

jacek
7th November 2008, 00:11
You create both the scene and the view on the stack, so they go out of scope when disp() returns.