Results 1 to 13 of 13

Thread: Error code after inheriting QGraphicsView

  1. #1
    Join Date
    Dec 2010
    Location
    Warsaw, Poland
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Error code after inheriting QGraphicsView

    Hi everyone!

    I have some problem with QGraphicsView. I want to create a program that will allow me to draw rectangles on loaded image. I have created my own GraphicsView class that inherits from QGraphicsView and I have overloaded QGraphicsView::mouseReleaseEvent to get the mouse position. I also added 'Widget' from Qt Creator to my form and replaced it with my class. It works fine until I close the window. After that I get an error code (-1073741819). Debugger shows segmentation fault in function QScopedPointer (no idea why). Probably I have to call some function, but I don't know which one and where. I will show you my code (only .cpp). Tell me if you need the header files.

    I hope you understand what I mean Could you help me?

    MainWindow class:
    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QWidget(parent), ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7. ui->widget->setScene(&scene);
    8.  
    9. connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
    10. connect(ui->loadButton, SIGNAL(clicked()), this, SLOT(loadImage()));
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::loadImage()
    19. {
    20. QString fileName = QFileDialog::getOpenFileName();
    21. image.load(fileName);
    22. scene.addPixmap(image);
    23. ui->widget->loadImage(image);
    24. ui->widget->show();
    25. }
    To copy to clipboard, switch view to plain text mode 

    GraphicsView class:
    Qt Code:
    1. #include "GraphicsView.h"
    2.  
    3. GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
    4. {
    5. firstRect = true;
    6. }
    7.  
    8. GraphicsView::~GraphicsView()
    9. {
    10. scene()->clear();
    11. }
    12.  
    13. void GraphicsView::loadImage(QPixmap item)
    14. {
    15. image = item;
    16. }
    17.  
    18. void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
    19. {
    20. if(point1.isNull())
    21. {
    22. point1.setX(event->x());
    23. point1.setY(event->y());
    24. }
    25. else
    26. {
    27. point2.setX(event->x());
    28. point2.setY(event->y());
    29. int width = point2.x() - point1.x();
    30. int height = point2.y() - point1.y();
    31.  
    32. if(firstRect)
    33. {
    34. rect.setRect(point1.x(), point1.y(), width, height);
    35. rect.setPen(QPen(Qt::red));
    36. firstRect = false;
    37. }
    38. else
    39. {
    40. scene()->removeItem(&rect);
    41. rect.setRect(point1.x(), point1.y(), width, height);
    42. }
    43.  
    44. scene()->clear();
    45. scene()->addPixmap(image);
    46. scene()->addItem(&rect);
    47.  
    48. point1.setX(0);
    49. point1.setY(0);
    50. }
    51.  
    52. event->accept();
    53. }
    To copy to clipboard, switch view to plain text mode 
    Platform: Windows 7
    Qt: 4.7.0

    Thanks in advance
    Kuba
    Last edited by K.Sejdak; 13th March 2011 at 00:36.

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

    Default Re: Error code after inheriting QGraphicsView

    Please provide a backtrace from the 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
    Dec 2010
    Location
    Warsaw, Poland
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error code after inheriting QGraphicsView

    Qt Code:
    1. 0 QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data qscopedpointer.h 135 0x00fc8bee
    2. 1 qGetPtrHelper<QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> > > qglobal.h 2328 0x00de5b5d
    3. 2 QGraphicsScene::d_func qgraphicsscene.h 297 0x00ebbff0
    4. 3 QGraphicsScene::clear qgraphicsscene.cpp 2423 0x00df8d2f
    5. 4 ~GraphicsView GraphicsView.cpp 10 0x00401f02
    6. 5 QObjectPrivate::deleteChildren qobject.cpp 1949 0x6a2120f8
    7. 6 ~QWidget qwidget.cpp 1589 0x008938e8
    8. 7 ~MainWindow MainWindow.cpp 16 0x004018be
    9. 8 qMain main.cpp 10 0x0040140d
    10. 9 WinMain@16 qtmain_win.cpp 131 0x00402892
    11. 10 main 0 0x004025b8
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Error code after inheriting QGraphicsView

    Please show us how you created the view and scene objects.
    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. #5
    Join Date
    Dec 2010
    Location
    Warsaw, Poland
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error code after inheriting QGraphicsView

    The scene object is a private member of MainWindow class (not pointer) and the view, just as I said, was replaced with the "widget" object in Qt Designer so I don't create it myself. I will provide the header files to be clear:

    MainWindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5. #include <QtGui>
    6.  
    7. namespace Ui
    8. {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. public slots:
    21. void loadImage();
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25. QPixmap image;
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    GraphicsView.h
    Qt Code:
    1. #ifndef GRAPHICSVIEW_H
    2. #define GRAPHICSVIEW_H
    3.  
    4. #include <QGraphicsView>
    5. #include <QtGui>
    6.  
    7. class GraphicsView : public QGraphicsView
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit GraphicsView(QWidget *parent = 0);
    12. ~GraphicsView();
    13. void loadImage(QPixmap item);
    14. virtual void mouseReleaseEvent (QMouseEvent *event);
    15.  
    16. signals:
    17.  
    18. public slots:
    19.  
    20. private:
    21. QPixmap image;
    22. QPoint point1;
    23. QPoint point2;
    24. bool firstRect;
    25. };
    26.  
    27. #endif // GRAPHICSVIEW_H
    To copy to clipboard, switch view to plain text mode 
    Now you have almost all my code (only main is left).

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Error code after inheriting QGraphicsView

    Wild guess: Check if scene() returns a valid pointer in GraphicsView's destructor.

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

    Default Re: Error code after inheriting QGraphicsView

    I would suspect there is a parent-child relationship between some objects that causes double deletion of something but I can't see anything like that in the code.
    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.


  8. #8
    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: Error code after inheriting QGraphicsView

    Qt Code:
    1. // qgraphicsscene.cpp
    2. //...
    3. void QGraphicsScene::clear(){
    4. // Recursive descent delete
    5. for (int i = 0; i < d->indexedItems.size(); ++i) {
    6. if (QGraphicsItem *item = d->indexedItems.at(i)) {
    7. if (!item->parentItem())
    8. delete item; //!< here
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Maybe scene tries to delete your rect created on stack. Try to call removeItem() before clear() and see if it still crashes:
    Qt Code:
    1. GraphicsView::~GraphicsView()
    2. {
    3. scene()->removeItem(&rect);
    4. scene()->clear();
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Error code after inheriting QGraphicsView

    If the lifespan of the scene is tied to the lifespan of the view then the easiest way is to make the scene a dynamically allocated object with its parent set to the view. Then you don't have to clear the scene because the scene's destructor will do it for you. Maybe the problem will go away then. Also, what is causing the view to be deleted?
    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.


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

    K.Sejdak (13th March 2011)

  11. #10
    Join Date
    Dec 2010
    Location
    Warsaw, Poland
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error code after inheriting QGraphicsView

    Quote Originally Posted by wysota View Post
    If the lifespan of the scene is tied to the lifespan of the view then the easiest way is to make the scene a dynamically allocated object with its parent set to the view. Then you don't have to clear the scene because the scene's destructor will do it for you. Maybe the problem will go away then. Also, what is causing the view to be deleted?
    That was it! I did just as you said and it worked perfectly. In fact, it is much simplier and logical to modify an existing widget this way, than to replace it. Thanks for all your help. I will provide the changed code for the others with similar problems:

    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) : QWidget(parent), ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7. view = new GraphicsView(ui->widget); // <--- here
    8. view->setScene(&scene); //
    9. view->resize(ui->widget->size()); //
    10.  
    11. connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
    12. connect(ui->loadButton, SIGNAL(clicked()), this, SLOT(loadImage()));
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete view;
    18. delete ui;
    19. }
    20.  
    21. void MainWindow::loadImage()
    22. {
    23. QString fileName = QFileDialog::getOpenFileName();
    24. image.load(fileName);
    25. scene.addPixmap(image);
    26. view->loadImage(image);
    27. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Error code after inheriting QGraphicsView

    So what exactly did you change? Because if you mean line #7 of the above snippet then it's certainly not what I meant and it shouldn't have any influence on your application.
    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.


  13. #12
    Join Date
    Dec 2010
    Location
    Warsaw, Poland
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error code after inheriting QGraphicsView

    Hmm, now I realized that when I was reading your post I saw "view" instead of "scene" in part about dynamic allocation. Nevertheless it somehow solved the problem with the exit code. Do you think that it was just a luck? Maybe there is another mistake?

    And to answer your question: I just dynamically allocated memory for view object and I set wiget from the main form to be a parent for it.
    Last edited by K.Sejdak; 13th March 2011 at 19:37.
    "My programs don't have bugs. They just provide random features."

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

    Default Re: Error code after inheriting QGraphicsView

    If you look inside setupUi(), that's exactly what was happening previously too.
    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.


Similar Threads

  1. Replies: 4
    Last Post: 9th June 2010, 07:46
  2. exited with code -1073741819 error
    By arpspatel in forum Qt Programming
    Replies: 8
    Last Post: 2nd March 2010, 09:47
  3. Replies: 2
    Last Post: 9th November 2009, 09:31
  4. Linker error when i link C code
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 22nd November 2008, 09:48
  5. Error in Q3Canvas Code...
    By Kapil in forum Newbie
    Replies: 10
    Last Post: 27th March 2006, 05:15

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.