2017-03-29 1 views
1

클래스 Kwadrat의 문제점은 무엇입니까? (그것이 중간에 돌아 오면 좌표 Y에서 X에 370 또는 370을 명중 할 때)Qt에서 boundingRect를 사용하려고 시도했습니다

Invalid new-expression of abstract class type 'Kwadrat'

Kwadrat* kwadrat = new Kwadrat(20); 

내가 화면에 움직이는 사각형을 원하는 : 나는 오류가 있습니다.

Kwadrat가 클래식 일 때 QGraphicsRectItem 나는 (0,0) 좌표의 버그가 있습니다. http://doc.qt.io/qt-5/qgraphicsitem.html#details

에서

#include <QApplication> 
#include <QGraphicsView> 
#include <QGraphicsScene> 
#include <QPixmap> 
#include "poruszanie.h" 
#include <QRectF> 
#include <QGraphicsRectItem> 

class Kwadrat : public QGraphicsItem 
{ 
    Q_OBJECT 

public: 
    Kwadrat(int size) 
     : QGraphicsItem(NULL) // we could parent, but this may confuse at first 
    { 
     m_boundingRect = QRectF(0, 0, size, size); 
    } 

    QRectF boundingRect() const 
    { 
     return m_boundingRect; 
    } 

private: 
    QRectF m_boundingRect; 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    QGraphicsScene*scena=new QGraphicsScene(); 

    // Poruszanie*kwadrat=new Poruszanie(); 
    // kwadrat->setRect(0,0,20,20); 
    // kwadrat->setBrush(QBrush(Qt::white)); 
    // scena->addItem(kwadrat); 

    Kwadrat*kwadrat=new Kwadrat(20); 
    kwadrat->setBrush(QBrush(Qt::white)); 
    scena->addItem(kwadrat); 

    kwadrat->setFlag(QGraphicsItem::ItemIsFocusable); 
    kwadrat->setFocus(); 

    QGraphicsView *widok=new QGraphicsView(scena); 
    widok->setBackgroundBrush(QBrush(Qt::yellow)); 
    widok->setMinimumSize(400,400); 
    widok->show(); 

    return a.exec(); 
} 

답변

3

To write your own graphics item, you first create a subclass of QGraphicsItem, and then start by implementing its two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting.

그래서 당신은 당신의 QGraphicsItem 오류를 제거하기의 그림을하기 위해 순수 가상 함수 paint를 구현해야, 컴파일러가 표시해야, 당신을 말하는 외에 클래스가 추상적이라고 (함수 구현이 누락 되었기 때문에).

관련 문제