You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, July 26th 2010, 1:19pm

QPixmap in QGraphicsScene causes crash

Hey all!!

I have a program with a QGraphicsScene, displayed by a QGraphicsView. What I wanted to do was insert a number of images (using a QPixmap for each) in the QGraphicsScene. I succeeded, but whenever I click one of these images, the program crashes. I get the error message as displayed in the attachment.

Here is some source code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	mScene = new QGraphicsScene( this );
	mScene->setItemIndexMethod( QGraphicsScene::NoIndex );
	mScene->setSceneRect( 0, 0, 800, 700 );
	mScene->setBackgroundBrush( Qt::blue );

	mView = new QGraphicsView( mScene );
	mView->setCacheMode( QGraphicsView::CacheBackground );
	mView->setViewportUpdateMode( QGraphicsView::BoundingRectViewportUpdate );
	mView->setRenderHint( QPainter::Antialiasing );
	mView->setTransformationAnchor( QGraphicsView::AnchorUnderMouse );
	mView->setResizeAnchor( QGraphicsView::AnchorViewCenter );
	
	mView->show();

	mIcon = new QGraphicsPixmapItem( this );
	
	QPixmap* image = new QPixmap( "Images/Process.png", 0, Qt::AutoColor );
	mIcon->setPixmap( *image );
	mScene->addItem( mIcon );


I can click ANYWHERE in the QGraphicsScene, exept for on the image... any ideas??
Elimentz has attached the following file:
  • Naamloos.jpg (42.57 kB - 31 times downloaded - latest: Feb 7th 2013, 7:09am)

Znurre

Trainee

  • "Znurre" is male

Posts: 174

Location: Sweden

Occupation: Programmer

  • Send private message

2

Monday, July 26th 2010, 1:22pm

Change line 17 and 18:

Source code

1
2
	QPixmap image( "Images/Process.png", 0, Qt::AutoColor );
	mIcon->setPixmap( image );

See if it helps.

3

Monday, July 26th 2010, 1:37pm

No, doesn't help.. I get the same error again

Thx for trying...

Znurre

Trainee

  • "Znurre" is male

Posts: 174

Location: Sweden

Occupation: Programmer

  • Send private message

4

Monday, July 26th 2010, 1:42pm

Row 15 could be the problem.
You overwrite the existing pointer with a new one each iteration.

Try this and see if it still causes the app to crash:

Source code

1
QGraphicsPixmapItem *icon = new QGraphicsPixmapItem( this );

5

Monday, July 26th 2010, 1:54pm

no, that's not it either... I have 3 (or more) objects, each with one mIcon. rows 15-20 are executed once for each object, the other pieces of code are executed once..

Znurre

Trainee

  • "Znurre" is male

Posts: 174

Location: Sweden

Occupation: Programmer

  • Send private message

6

Monday, July 26th 2010, 1:56pm

And you don't have some slot connected to some signal in either the scene or one of the objects that's triggered when you click an item?
I recommend you to use gdb to try and find the error. QtCreator has got great gdb integration.

7

Monday, July 26th 2010, 2:03pm

the objects containing the mIcon variables are created using a slot, but other then that no slots or signals are used.

What exactly is gdb? Haven't heard of it... do you have a website with some information?

8

Monday, July 26th 2010, 2:17pm

Here's the class I was talking about, the "object" in the previous posts:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Process : public QGraphicsItemGroup
{
	public:
		Process( QGraphicsScene* scene );
		~Process();
		QGraphicsPixmapItem* getIcon()
		{
			return mIcon;
		}
	
	private:
		QGraphicsScene* mScene;
		QGraphicsPixmapItem* mIcon;
};


Is it possible that my program crashes because my class inherits from QGraphicsItemGroup and contains an QGraphicsPixmapItem?

Znurre

Trainee

  • "Znurre" is male

Posts: 174

Location: Sweden

Occupation: Programmer

  • Send private message

9

Monday, July 26th 2010, 2:28pm

gdb is GNU Debugger http://en.wikipedia.org/wiki/GNU_Debugger
If you use QtCreator, set your project to build in debug mode and just press F5 or "Debug" to start.