2012-10-17 46 views
0

我有一個類View合併我的QGraphicsView但我有麻煩繼承它。我的代碼如下:Qt QGraphicsView事件

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; 

而且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); 

}; 

我已經實現paintEvent只是一個快速coutgame。當我編譯,一切編譯好的,但是當我跑,我不斷收到一個消息,說一個純虛函數被調用:

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

View構造是這樣的:

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

任何幫助不勝感激。

+3

嘗試在您的基類中移除「paintEvent」的純虛擬(= 0)。由於它是從QT內部調用我認爲,這將是問題。 – AquilaRapax

+0

當您獲得純虛擬通話時,您能否顯示通話堆棧? – Paul

+0

我最終不得不爲基類中的paint事件實現一個空的函數。感謝您的快速解決方案 – JonMorehouse

回答

0

我得到了paintEvent函數來編譯和打開我的應用程序。但由於某種原因,每當我嘗試在我的場景中畫畫時,它都不起作用。

但是,如果我拿出paintEvent函數,它工作正常。有什麼建議爲什麼paintEvent打破這個?

在我的主文件:

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

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

    view->setRenderHints(QPainter::Antialiasing); 

    view->show(); 

這工作,如果我擺脫了虛擬paintEvent的功能,但包括它會破壞圓從圖紙?

+1

你通常不需要重寫'QGraphicsView :: paintEvent()',而是實現你的'QGraphicsObject :: paint()'方法來繪製你所需要的。嘗試在'Game :: paintEvent()'的開頭調用'QGraphicsView :: paintEvent()',看看它是否有效。 – Paul

+0

嘿,這工作。通過調用QGraphicsObject :: paintEvent(),一切都很好!謝謝 – JonMorehouse