0

I have a class View that incorporates my QGraphicsView but I am having trouble inheriting from it. My code is as follows:

class View: public QGraphicsView {//inherits qwidget -- so we can make it full screen there

    Q_OBJECT

    public:
        View(QGraphicsScene * _scene);//this is responsible for setting up the screen and instantiating several elements
        ~View();
    protected:
        virtual void paintEvent(QPaintEvent * event) = 0;

And Game inherits from View:

#include "view.h" //this includes all of the functionality of the different elements

using namespace std;

class Game : public View {//inherit everything above and down into private functions

    public:
        Game(QGraphicsScene * _scene);
        ~Game();

    protected:
        void paintEvent(QPaintEvent * event);

};

I have implemented paintEvent with just a quick cout in game. When I compile, everything compiles okay but when I run, I keep getting a message that says a pure virtual function was called:

libc++abi.dylib: pure virtual method called
Abort trap: 6

My View constructor looks like this:

View::View(QGraphicsScene * _scene) : QGraphicsView(_scene) {...

Any help would be greatly appreciated.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
JonMorehouse
  • 1,313
  • 6
  • 14
  • 34
  • 3
    Try removing the pure virtuality (= 0) of `paintEvent` in your base class. Since it's called from within QT i think, this would be the problem. – AquilaRapax Oct 17 '12 at 09:23
  • Can you show the call stack when you get pure virtual call? – Paul Oct 17 '12 at 09:24
  • I ended up having to implement an empty function for the paint event in the base class. Thanks for quick fix – JonMorehouse Oct 17 '12 at 09:31
  • 2
    `View::paintEvent()` should not be called by Qt, `Game::paintEvent()` should be called. Maybe you call `View::paintEvent()` in your `Game::paintEvent()` implementation? – Paul Oct 17 '12 at 09:44

1 Answers1

0

I got the paintEvent function to compile and open my application. But for some reason, whenever I try to draw on my scene, it doesn't work.

However, if I take out the paintEvent function, it works fine. Any suggestions as to why the paintEvent breaks this?

in my main file:

QGraphicsEllipseItem * item = new QGraphicsEllipseItem(0, scene);

    item->setRect(50.0, 50.0, 100.0, 100.0);

    view->setRenderHints(QPainter::Antialiasing);

    view->show();

This works if I get rid of the virtual paintEvent function, but including it will break the circle from drawing?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
JonMorehouse
  • 1,313
  • 6
  • 14
  • 34
  • 1
    You generally don't need to override `QGraphicsView::paintEvent()`, instead you implement your `QGraphicsObject::paint()` method in order to draw what you need. Try to call `QGraphicsView::paintEvent()` at the beginning of your `Game::paintEvent()` and see whether it works. – Paul Oct 17 '12 at 11:03
  • Hey, this worked. By calling QGraphicsObject::paintEvent(), Everything worked out great! Thanks – JonMorehouse Oct 17 '12 at 21:27