简体   繁体   English

Qt QGraphicsView事件

[英]Qt QGraphicsView Event

I have a class View that incorporates my QGraphicsView but I am having trouble inheriting from it. 我有一个包含我的QGraphicsView的类View ,但是在继承它时遇到了麻烦。 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 : Game继承自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 . 我已经实现paintEvent只是一个快速coutgame 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::View(QGraphicsScene * _scene) : QGraphicsView(_scene) {...

Any help would be greatly appreciated. 任何帮助将不胜感激。

I got the paintEvent function to compile and open my application. 我得到了paintEvent函数来编译并打开我的应用程序。 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. 但是,如果我取出paintEvent函数,它将正常工作。 Any suggestions as to why the paintEvent breaks this? 关于为什么paintEvent打破了这个的任何建议?

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? 如果我摆脱了虚拟的paintEvent函数,这会起作用,但是包括它会破坏绘图的圆吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM