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

Friday, July 27th 2007, 4:12pm

GraphicsImageItem

Hi ,

I tried to create a GraphicsImageItem similar to QGraphicsPixmapItem but displaying an image.

Basically i have an QImage member and reimplement the two pure functions of QGraphicsItem:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
QRectF GraphicsImageItem::boundingRect() const
{
	return QRectF(-_image->size().width()/2,-_image->size().height()/2,_image->size().width(),_image->size().height());
	// return QRectF(-50.0,-50.0,100.0,100.0);

//return QRectF(-_size.width()/2,-_size.height()/2,_size.width(),_size.height());
}

void GraphicsImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
	if (_image) painter->drawImage(boundingRect(),*_image);
}


The problem is as soon as i change the BoundingRect to be anything else than the image pixel size, my Item display nothing.

For info, this image is updated from outside and require redraw often.

I can't figure why i can not resize it.

Would somebody have a brillliant idea? ;)

elpepe

Trainee

  • "elpepe" is male

Posts: 59

Location: Argentina

Occupation: Software developer

  • Send private message

2

Monday, July 30th 2007, 2:09pm

Hi,
Sorry but I am a beginner. If I understand you are trying to generate a new GraphicsItem starting from a QImage, aren't you?.
This QGraphicsImageItem can be added to a QScene, as the other GraphicsItem?. If it is so, I am interested to know which GraphicsItem you subclassed to do this. It is the class I a need to use for my project.
Sorry again but as you understand still I did not reach the point you are now, so I cannot help you, but If I can begin with this class, surely I will be in position to discuss your points in a few days.
Thanks in advance and regards

3

Monday, July 30th 2007, 5:20pm

I don't use any subclass.

I subclassed QGraphicsItem directly and reimplement only the two methods from the precedent message.

Curiously it seems to work on Static image but my purpose is to get a QImage from a camera, so i used QImage(const uchar*...) constructor and points to the camera buffer.
But as soon as i do a little change on the matrix , like a scale or rotate on the item or on the view, i get nothing inside the QImage.

As soon as my matrix is identity , i reget my good old camera view.

Really i don't understand. What can happen ?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
GraphicsImageItem::GraphicsImageItem(QImage* image, QSizeF size, QGraphicsItem * parent)
	: QGraphicsItem(parent),QObject()
{
	_image = image;
	if ((!size.isValid())&&(_image)) _size = _image->size();
	else _size = size;
}

GraphicsImageItem::~GraphicsImageItem()
{
}

QRectF GraphicsImageItem::boundingRect() const
{
	if (!_image) return QRectF();
	return QRectF(-_image->size().width()/2,-_image->size().height()/2,_image->size().width(),_image->size().height());
}

void GraphicsImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
	//painter->setOpacity(0.5);
	if (_image) painter->drawImage(boundingRect(),(*_image));
	if (_image) painter->drawRoundRect(boundingRect());
}

void GraphicsImageItem::setImage(QImage* image)
{
	_image = image;
}

QImage* GraphicsImageItem::image()
{
	return _image;
}

QSizeF GraphicsImageItem::size()
{
	return _size;
}

void GraphicsImageItem::setSize(QSizeF size)
{
	_size = size;
}

This post has been edited 5 times, last edit by "Daes" (Jul 30th 2007, 5:34pm)


elpepe

Trainee

  • "elpepe" is male

Posts: 59

Location: Argentina

Occupation: Software developer

  • Send private message

4

Tuesday, July 31st 2007, 2:54am

Thanks for the information. I will try your code to see what is going on.

elpepe

Trainee

  • "elpepe" is male

Posts: 59

Location: Argentina

Occupation: Software developer

  • Send private message

5

Tuesday, July 31st 2007, 3:38pm

Hi Daes,
Sorry but I couldn't even link my small project:

QGraphicsImageItem.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QGraphicsImageItem::metaObject(void)const " (?metaObject@QGraphicsImageItem@@UBEPBUQMetaObject@@XZ)
1>QGraphicsImageItem.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall QGraphicsImageItem::qt_metacast(char const *)" (?qt_metacast@QGraphicsImageItem@@UAEPAXPBD@Z)
1>QGraphicsImageItem.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall QGraphicsImageItem::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QGraphicsImageItem@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

I am using MS Visual C++ 2005 Express Edition with Qt 4.2 Windows Open Source Edition, in a XP SP2 station. I always get these messages when I forgot to force the link to some .lib libray, but at this time I checked and as QGraphicsItem is included in QtGui ..., there will be nothing additional to tell the compiler.
Anyway, I coded a class copied from yours one:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "QGraphicsImageItem.h"
QGraphicsImageItem::QGraphicsImageItem(QImage* image, QSizeF size, QGraphicsItem * parent)
	: QGraphicsItem(parent),_image(image)
{
	_image = image;
	if ((!size.isValid())&&(_image)) _size = _image->size();
	else _size = size;
}

QGraphicsImageItem::~QGraphicsImageItem()
{
}

QRectF QGraphicsImageItem::boundingRect() const
{
	if (!_image) return QRectF();
	return QRectF(-_image->size().width()/2,-_image->size().height()/2,_image->size().width(),_image->size().height());
}

void QGraphicsImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
	//painter->setOpacity(0.5);
	if (_image) painter->drawImage(boundingRect(),(*_image));
	if (_image) painter->drawRoundRect(boundingRect());
}

void QGraphicsImageItem::setImage(QImage* image)
{
	_image = image;
}

QImage* QGraphicsImageItem::image()
{
	return _image;
}

QSizeF QGraphicsImageItem::size()
{
	return _size;
}

void QGraphicsImageItem::setSize(QSizeF size)
{
	_size = size;
}


with a .h file:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <QtGUI>
#include<QGraphicsItem>

class QGraphicsImageItem :
	public QGraphicsItem
{
	Q_OBJECT
public:
	QGraphicsImageItem(QImage* image, QSizeF size, QGraphicsItem * parent);
	void setImage(QImage* image);
	QImage* image();
	QSizeF size();
	void setSize(QSizeF size);
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
private:
	QImage *_image;
	QSizeF _size;

public:
	~QGraphicsImageItem(void);
};

and the main to use this class: (should I instantiate a QGraphics Scene to use QGraphicsImageItem?)

Source code

1
2
3
4
5
6
7
8
9
10
11
12
#include <QApplication>

#include "QGraphicsImageItem.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
	QSizeF mySize(286,128);
	QImage myImage("FondoTigre.jpg");
    QGraphicsImageItem myQImage(& myImage,mySize,0);
    myQImage.show();
}

6

Wednesday, August 1st 2007, 1:48am

I think this problem come from the fact that you put a Q_OBJECT declaration in a class not derived from QObject.

You should perhaps either add (in first position, i believe i find something about that) QObject as a base class, either remove your Q_OBJECT.

elpepe

Trainee

  • "elpepe" is male

Posts: 59

Location: Argentina

Occupation: Software developer

  • Send private message

7

Wednesday, August 1st 2007, 3:46pm

Hi Daes,
I was having problems even with the Graphics View Framework samples that come with Qt Windows Open Source Edition. Now I "solved" them. There's something wrong in the way (or the properties) I am setting in VC++ 2005 EE. Part of my problems are I don't know much about the IDE and part ...yes, it is not the best IDE but is free, and it is right for Windows (unfortunately I work in Windows). I found a workaround to my problems (something like go out of the car, close the doors, open the doors, go in again, and turn on the engine!) and finally could compile everything, so I am just starting with this. I will let you know about my progress.

8

Wednesday, August 1st 2007, 3:51pm

Sorry i didn't see the end of your code:

you have to use a QGraphicsItem in a QGraphicsScene viewable in a QGraphicsView

something like this:

Source code

1
2
3
4
5
6
7
8
9
test::test(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	QGraphicsScene* scene = new QGraphicsScene();
	scene->addRect(QRect(100,100,200,200));
	scene->addItem(new QImageItem(new QImage("047.png")));
	QGraphicsView* view = new QGraphicsView(scene);
	setCentralWidget(view);
}