Results 1 to 4 of 4

Thread: Crashes When Using QGraphicsScene?

  1. #1
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Crashes When Using QGraphicsScene?

    Hello,

    I am still pretty new to Qt and it is pretty different from most of the editors I have ever used before. I have started a simple project - When you click on a QGraphicsScene it draws a red background. However, upon clicking on the scene it crashes the program.

    Can anyone see where I went wrong, for the life of me I cannot figure this one out?

    Scene Class:

    Qt Code:
    1. class scene2 : public QGraphicsScene
    2. {
    3. #include <QMessageBox>
    4.  
    5. public:
    6. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    7. };
    To copy to clipboard, switch view to plain text mode 

    Set's Scene To View:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene = new scene2;
    8. ui->GraphicsMap->setScene(scene);
    9. scene->setSceneRect(0,0,900,900);
    10. }
    To copy to clipboard, switch view to plain text mode 

    On Click:
    Qt Code:
    1. void scene2::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. QBrush whiteBrush(Qt::red);
    4. scene->setBackgroundBrush(whiteBrush);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help guys,
    Matt <Noob>

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crashes When Using QGraphicsScene?

    Please post the whole project, if you can. What you posted seems ok, it shouldn't cause a crash.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crashes When Using QGraphicsScene?

    Thanks for the help man,

    It appears to be working now. It turns out that I was actually decalring two scene variables named scene and each one with there own class that was cancelling out the others commands and crashing the system.

    Now that I have that working - I can click on the scene and get a red background but I cannot drag items that have the ItemIsMovable flag on, why is this?

    Thanks,
    Matt

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crashes When Using QGraphicsScene?

    Because the flag is implemented in scene's mousePressEvent. You have overriden it without calling the base class implementation, so the code that handles moving items is not called. If you call the base class implementation of the mousePressEvent, all should work fine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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


Similar Threads

  1. Replies: 1
    Last Post: 8th September 2012, 05:08
  2. Program crashes
    By Fallen_ in forum Qt Programming
    Replies: 49
    Last Post: 20th September 2010, 01:41
  3. app crashes again
    By sophister in forum Qt Programming
    Replies: 7
    Last Post: 15th June 2009, 10:01
  4. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27
  5. QGraphicsScene render() crashes
    By iebele in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2008, 13:38

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
  •  
Qt is a trademark of The Qt Company.