Results 1 to 4 of 4

Thread: QGraphicsView, QGraphicsScene and QGraphicsItem Problem :(

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy QGraphicsView, QGraphicsScene and QGraphicsItem Problem :(

    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

  2. #2
    Join Date
    Sep 2011
    Location
    Mannheim, Germany
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView, QGraphicsScene and QGraphicsItem Problem :(

    so vielleicht?

    Qt Code:
    1. connect(m_mainScene, SIGNAL(setBasicScene()), this, SLOT(createBasicScene()), Qt:QueedConnection);
    2.  
    3.  
    4.  
    5. if(scene() != NULL)
    6. m_basicScene->deleteLater();
    7. m_basicScene = new BasicScene();
    8. setScene(m_basicScene);
    9. connect(m_basicScene, SIGNAL(setMainScene()), this, SLOT(createMainScene()));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView, QGraphicsScene and QGraphicsItem Problem :(

    Hey dennis81,

    thanks for ur reply

    now i have solced my problem and get a new one
    when i click on my scene then i get a debug output: "MainScene: Pressed". thats all right, but when i click now on a roundrectitem in the scene, then i get this output:
    "RoundRectItem: Pressed
    MainScene: Pressed"

    but i dont want that the scene gets an MousePressEvent, because i just want to "press" the RoundRectItem.
    How can i made this? do i need an eventFilter in the Scene?

    And another question i have:
    Is it better, when i make my own SceneItems like RoundRectitem, to inherit from QGraphicsItem or QGraphicsObject?

    I ask, because when i inherit from QGraphicsItem and QObject and want to Animate like move in x-direction, then i need to create a new Q_PROPERTY. But when i inherit from QGraphicsObejct, then i have more defined Propertys, which i can animate.

    Thanks for helping

    with best regards

    nudels

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView, QGraphicsScene and QGraphicsItem Problem :(

    How can i made this? do i need an eventFilter in the Scene?
    You can call event->accept() in rect item, and test if event->isAccepted() in scene mousePressEvent.
    About your second question, I think you answered it yourself - if inheriting from QGraphicsItem requires you to do more work, isn't it better to use QGraphicsObject ?

  5. The following user says thank you to stampede for this useful post:

    nudels (26th October 2011)

Similar Threads

  1. problem with QGraphicsScene\QGraphicsView
    By Wojtek_SZ in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2011, 15:03
  2. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  3. mouseMoveEvent problem in QGraphicsView/QGraphicsScene
    By Lendrick in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2010, 05:26
  4. weird problem in QGraphicsView / QGraphicsScene
    By Mrdata in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2009, 19:26
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.