Hello Guys

Im very new in Qt and have a little Problem, maybe its not a Qt Problem or a thinking error.

Okay lets start
I want to build a GUI that have a modular window architecture. I thought i create a QMainWindow and set a QGraphicsView as the centralWidget.
Now i create a MainScene and set it up to the View and in this Scene i have some buttons (QObejct and QGraphicsItem inherited).
When i click e.g. "ServiceButton" then i emit a Signal from the button to the scene and from the scene to the View.
Now i want to set a new Scene to the View, but first delete the old scene.

At this point my program crashes :/

i hope somebody understands me

But my Problem is, that i don't know if this idear 100% would be work.

Here is my Hole Code:

Main.cpp:
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include "mainview.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7.  
  8. QMainWindow* wMain = new QMainWindow();
  9. MainView* mainView = new MainView();
  10. wMain->setWindowFlags(Qt::FramelessWindowHint);
  11. wMain->setCentralWidget(mainView);
  12. wMain->show();
  13.  
  14. return a.exec();
  15. }
To copy to clipboard, switch view to plain text mode 

MainView.cpp + .h:
Qt Code:
  1. #ifndef MAINVIEW_H
  2. #define MAINVIEW_H
  3.  
  4. #include <QGraphicsView>
  5. #include "mainscene.h"
  6. #include "basicscene.h"
  7.  
  8. class MainView : public QGraphicsView
  9. {
  10. Q_OBJECT
  11. public:
  12.  
  13. MainView(QWidget* parent = 0);
  14. ~MainView();
  15.  
  16. protected:
  17. void resizeEvent(QResizeEvent *event);
  18.  
  19. public slots:
  20. void createBasicScene();
  21. void createMainScene();
  22.  
  23. private:
  24. MainScene* m_mainScene;
  25. BasicScene* m_basicScene;
  26.  
  27. };
  28.  
  29. #endif // MAINVIEW_H
  30.  
  31.  
  32. #include "mainview.h"
  33. #include <QDebug>
  34. #include <QMouseEvent>
  35. #include <QtOpenGL/QtOpenGL>
  36.  
  37.  
  38. MainView::MainView(QWidget* parent) : QGraphicsView(parent)
  39. {
  40.  
  41. qDebug() << "Start GraphicsView";
  42. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  43. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  44. setGeometry(QRect(0,0,800,480));
  45.  
  46. m_mainScene = new MainScene();
  47. m_mainScene->setSceneRect(QRectF(rect()));
  48.  
  49. setScene(m_mainScene);
  50. setRenderHint(QPainter::Antialiasing);
  51. setViewport(new QGLWidget());
  52.  
  53. connect(m_mainScene, SIGNAL(setBasicScene()), this, SLOT(createBasicScene()));
  54.  
  55. }
  56.  
  57. MainView::~MainView()
  58. {
  59. delete m_mainScene;
  60. delete m_basicScene;
  61. }
  62.  
  63. void MainView::resizeEvent(QResizeEvent *event)
  64. {
  65. QGraphicsView::resizeEvent(event);
  66. }
  67.  
  68.  
  69. void MainView::createBasicScene()
  70. {
  71. if(scene() != NULL)
  72. delete scene();
  73. m_basicScene = new BasicScene();
  74. setScene(m_basicScene);
  75. connect(m_basicScene, SIGNAL(setMainScene()), this, SLOT(createMainScene()));
  76. }
  77.  
  78. void MainView::createMainScene()
  79. {
  80. if(scene() != NULL)
  81. delete scene();
  82. m_mainScene = new MainScene();
  83. setScene(m_mainScene);
  84. connect(m_mainScene, SIGNAL(setBasicScene()), this, SLOT(createBasicScene()));
  85. }
To copy to clipboard, switch view to plain text mode 

MainScene.cpp + .h:
Qt Code:
  1. #ifndef MAINSCENE_H
  2. #define MAINSCENE_H
  3.  
  4. #include <QGraphicsScene>
  5. #include <QDebug>
  6. #include "roundrectitem.h"
  7. #include <QGraphicsSceneMouseEvent>
  8.  
  9. class MainScene : public QGraphicsScene
  10. {
  11. Q_OBJECT
  12. public:
  13. MainScene(QObject* parent = 0);
  14. ~MainScene();
  15.  
  16. protected:
  17. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  18.  
  19.  
  20. public slots:
  21. void buttonService();
  22.  
  23. signals:
  24. void setBasicScene();
  25.  
  26. private:
  27. RoundRectItem* rect;
  28. // RoundRectItem* rect2;
  29. // RoundRectItem* rect3;
  30.  
  31. };
  32.  
  33. #endif // MAINSCENE_H
  34.  
  35. #include "mainscene.h"
  36.  
  37.  
  38. MainScene::MainScene(QObject* parent) : QGraphicsScene(parent)
  39. {
  40. setSceneRect(0,0,800,480);
  41. qDebug() << "Start MainScene";
  42. QRectF iconRect(-100, -100, 200, 200);
  43. QColor iconColor(214,240,110,128);
  44. rect = new RoundRectItem(iconRect, iconColor);
  45.  
  46. rect->setObjectName("button");
  47.  
  48. addItem(rect);
  49.  
  50. rect->setPos(100,100);
  51.  
  52. connect(rect, SIGNAL(clicked()), this, SLOT(buttonService()));
  53. }
  54.  
  55. MainScene::~MainScene()
  56. {
  57. //removeItem(rect);
  58. clear();
  59. }
  60.  
  61. void MainScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
  62. {
  63.  
  64. QGraphicsScene::mousePressEvent(event);
  65. qDebug() << "MainScene: Pressed";
  66. // RoundRectItem* child = qgraphicsitem_cast<RoundRectItem*>( itemAt(event->scenePos()));
  67. // if(!child)
  68. // return;
  69.  
  70. }
  71.  
  72.  
  73. void MainScene::buttonService()
  74. {
  75. emit setBasicScene();
  76. }
To copy to clipboard, switch view to plain text mode 

RoundRectItem.cpp + .h:
Qt Code:
  1. #ifndef ROUNDRECTITEM_H
  2. #define ROUNDRECTITEM_H
  3.  
  4. #include <QGraphicsObject>
  5. #include <QLinearGradient>
  6. #include <QGraphicsDropShadowEffect>
  7. #include <QPropertyAnimation>
  8. #include <QGraphicsRotation>
  9. #include <QGraphicsItem>
  10. #include <QObject>
  11.  
  12. class RoundRectItem : public QObject, public QGraphicsItem
  13. {
  14. Q_OBJECT
  15. Q_PROPERTY(bool fill READ fill WRITE setFill)
  16. public:
  17. RoundRectItem(const QRectF bounds, const QColor &color);
  18. ~RoundRectItem();
  19. QPixmap pixmap()const;
  20. void setPixmap(const QPixmap &pixmap);
  21.  
  22. QRectF boundingRect() const;
  23. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  24.  
  25. bool fill()const;
  26. void setFill(bool fill);
  27.  
  28. QString objectName() const;
  29. void setObjectName(const QString &name);
  30.  
  31. protected:
  32. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  33.  
  34. signals:
  35. void clicked();
  36.  
  37.  
  38.  
  39. private:
  40. QPixmap pix;
  41. bool fillRect;
  42. QRectF bounds;
  43. QLinearGradient gradient;
  44. QGraphicsDropShadowEffect* shadow;
  45. QString m_objectName;
  46.  
  47.  
  48. };
  49.  
  50. #endif // ROUNDRECTITEM_H
  51.  
  52. #include "roundrectitem.h"
  53. #include <QtGui>
  54. #include <QDebug>
  55. #include <QPainterPath>
  56.  
  57. RoundRectItem::RoundRectItem(const QRectF bounds, const QColor &color)
  58. : QObject(0), QGraphicsItem(), fillRect(false), bounds(bounds)
  59. {
  60. gradient.setStart(bounds.topLeft());
  61. gradient.setFinalStop(bounds.bottomRight());
  62. gradient.setColorAt(0, color);
  63. gradient.setColorAt(1, color.dark(200));
  64. setCacheMode(ItemCoordinateCache);
  65.  
  66. shadow = new QGraphicsDropShadowEffect();
  67. shadow->setBlurRadius(50);
  68. shadow->setColor(QColor(0x40,0x40,0x40,255));
  69. setGraphicsEffect(shadow);
  70.  
  71. m_objectName = "x";
  72.  
  73. }
  74.  
  75. RoundRectItem::~RoundRectItem()
  76. {
  77. delete shadow;
  78. }
  79.  
  80.  
  81. QPixmap RoundRectItem::pixmap()const
  82. {
  83. return pix;
  84. }
  85.  
  86. void RoundRectItem::setPixmap(const QPixmap &pixmap)
  87. {
  88. pix = pixmap;
  89.  
  90. setFlags(flags() | QGraphicsItem::ItemIsSelectable);
  91. update();
  92. }
  93.  
  94. void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  95. {
  96. Q_UNUSED(option);
  97. Q_UNUSED(widget);
  98. painter->setPen(Qt::NoPen);
  99. painter->setBrush(QColor(0,0,0,64));
  100. painter->drawRoundRect(bounds.translated(2,2));
  101. // painter->drawRoundRect(bounds);
  102.  
  103. if(fillRect)
  104. painter->setBrush(QApplication::palette().brush(QPalette::Window));
  105. else
  106. painter->setBrush(gradient);
  107. painter->setPen(QPen(Qt::black,1));
  108. painter->drawRoundRect(bounds);
  109.  
  110. if(!pix.isNull())
  111. {
  112. painter->scale(1.95,1.95);
  113. painter->drawPixmap(-pix.width() /2, -pix.height()/2, pix);
  114. }
  115. else{
  116.  
  117. QPainterPath* path = new QPainterPath();
  118. QFont serifFont("Times", 20, QFont::Bold);
  119. path->addText(bounds.center().x(),bounds.center().y(), serifFont, m_objectName);
  120. painter->drawPath(*path);
  121. }
  122. }
  123.  
  124. bool RoundRectItem::fill() const
  125. {
  126. return fillRect;
  127. }
  128.  
  129.  
  130. void RoundRectItem::setFill(bool fill)
  131. {
  132. fillRect = fill;
  133. update();
  134. }
  135.  
  136. QString RoundRectItem::objectName() const
  137. {
  138. return m_objectName;
  139. }
  140.  
  141. void RoundRectItem::setObjectName(const QString &name)
  142. {
  143. m_objectName = name;
  144. }
  145.  
  146. QRectF RoundRectItem::boundingRect()const
  147. {
  148. return bounds.adjusted(0,0,2,2);
  149. }
  150.  
  151. void RoundRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
  152. {
  153.  
  154. // QGraphicsItem::mousePressEvent(event);
  155. qDebug() << "RoundRectItem: Pressed";
  156. emit clicked();
  157.  
  158. }
To copy to clipboard, switch view to plain text mode 

The BasicScene is just Similar to MainScene.

Or can i made my idear in another way to change the scene's or Views?

Thank for Help

Best regards

nudels