简体   繁体   中英

QGraphicsView issue

I dont understand what`s going on: when i create QGraphicsView object directly and adding scene with a pixmap, all is ok, pixmap appears on the screen:

scene.addPixmap(pix);
QGraphicsView graphicsView;
graphicsView.setScene(&scene);

But when i try to inherit QGraphicsView class with purpose of reimplementing events, nothing happend and i got white screen without pixmap, but events like changing cursor is working:

scene.addPixmap(pix);
DrawArea graphicsView;
graphicsView.setScene(&scene);

.h file:

class DrawArea : public QGraphicsView
{
    Q_OBJECT
public:
    DrawArea(QWidget *parent = 0);
    ~DrawArea();
signals:
public slots:
    void mousePressEvent(QMouseEvent * e);
    void paintEvent(QPaintEvent *);
    void enterEvent(QEvent *e);
private:
QPoint coord;
};

.cpp file:

DrawArea::DrawArea(QWidget *parent)
    : QGraphicsView(parent){

}

DrawArea::~DrawArea(){

}
void DrawArea::mousePressEvent(QMouseEvent * event){

}
void DrawArea::paintEvent(QPaintEvent *event){

}
void DrawArea::enterEvent(QEvent *event){
    viewport()->setCursor(Qt::CrossCursor);
}

Tell me if something missed, Thanks in advance.

You should process your events. Try this:

void DrawArea::mousePressEvent(QMouseEvent * event)
{
     //some actions
     QGraphicsView::mousePressEvent(event);
}

void DrawArea::paintEvent(QPaintEvent *event)
{
     //some actions
     QGraphicsView::paintEvent(event);
}

Also I think that you don't need paintEvent at all, do all needed things in the scene.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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