gpt4 book ai didi

c++ - QGraphicsView 问题

转载 作者:行者123 更新时间:2023-11-28 06:34:38 25 4
gpt4 key购买 nike

我不明白发生了什么:当我直接创建 QGraphicsView 对象并添加带有像素图的场景时,一切正常,像素图出现在屏幕上:

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

但是当我尝试继承 QGraphicsView 类以重新实现事件时,没有任何反应,我得到了没有像素图的白屏,但​​是像更改光标这样的事件正在工作:

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

.h文件:

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 文件:

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);
}

如果遗漏了什么,请告诉我,在此先感谢。

最佳答案

您应该处理您的事件。试试这个:

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

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

另外我认为你根本不需要 paintEvent,在场景中做所有需要的事情。

关于c++ - QGraphicsView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com