2013-05-17 1 views
0

내 로컬 디렉토리에 "tooltip.png"파일이 있습니다. '(INT 주에서QGraphicsView가 그림을 표시하지 않습니다.

QGraphicsScene scene; 
QGraphicsView *view = new QGraphicsView(&scene); 
QGraphicsPixmapItem item(QPixmap("tooltip.png")); 
scene.addItem(&item); 
view.show(); 

는 사진을 보여)하지만 생성자에서 케이 : 내가 (INT 주에 넣어)하지만 난 내 MainWindow를 클래스 생성자에 넣을 때 작동하지 않는 경우 아래 코드가 작동 t

답변

1

스택에 scene을 작성합니다. 이는 범위를 벗어나면 생성자의 끝에있는 삭제됩니다. 당신의 MainWindow를 생성자 내부에 이런 scene을 구성하십시오 :

QGraphicsScene scene(this); 

장면에 부모를 제공함으로써, Qt는 그것을 (귀하의 경우에 당신은 MainWindow를) 그 부모와 함께 살아 남아 있는지 확인합니다. Qt는 메모리 관리를 담당하므로 메모리 누수가 염려없이 포인터를 사용할 수 있습니다 :

QGraphicsScene* scene = new QGraphicsScene(this); 
QGraphicsView* view = new QGraphicsView(scene, this); // Give the view a parent too to avoid memory leaks 
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap("tooltip.png"), this); 
scene->addItem(&item); 
view->show(); 
관련 문제