PDA

View Full Version : Pictures won't display - possible qt coordinate system confusion



MarkoSan
6th December 2007, 08:26
Hi to all!

With the following code I want to put some pictures on a scene:
#include "CMerchandizeSelector.h"

CMerchandizeSelector::CMerchandizeSelector(QWidget * pParent)
{
QGraphicsScene* pScene=new QGraphicsScene(this); // creates new scene
Q_CHECK_PTR(pScene); // checks object creation
pScene->setItemIndexMethod(QGraphicsScene::NoIndex);
setSceneRect(minX, minY, maxX, maxY);
setScene(pScene);
setWindowTitle(trUtf8("Merchandize selector"));
// TODO: sets scene widget flags so it cannot be closed
/*
m_pIconList=new QStringList; // creates new string list for storing icon names
Q_CHECK_PTR(m_pIconList); // checks creation
*/

m_cPixmap=QPixmap(100, 100);
m_cPixmap.fill(Qt::black);

QPainter p(&m_cPixmap); // painter
//QPainter p(this); // painter
p.translate(rCenterX, rCenterY); // translated coordinate system to centre of widget
p.setWindow(QRect(minX, minY, maxX, maxY));
p.setViewport(QRect(minX, minY, maxX, maxY));

// only for test purposes, images must be read from database
m_cImageList.append(QImage(":/images/apple_cake.png"));
m_cImageList.append(QImage(":/images/bigmac.png"));
m_cImageList.append(QImage(":/images/applejuice.png"));
m_cImageList.append(QImage(":/images/cafe_cool"));
m_cImageList.append(QImage(":/images/chesseburger.png"));
m_cImageList.append(QImage(":/images/coke.png"));

// computes positions for images
for (m_iCounter=0; m_iCounter<iLoadTreshold; m_iCounter++)
{
QPointF tmpPoint=computePoint(0, 0, rCircleRadius, m_iCounter*rAngle*Pi/180);
m_cImagePointList.append(tmpPoint);
qDebug() << "(" << tmpPoint.rx() << ", " << tmpPoint.ry() << "); ";
}

// draws an image at computed coords
for (m_iCounter=0; m_iCounter<m_cImageList.count(); m_iCounter++)
{
p.drawImage(m_cImagePointList.at(m_iCounter), m_cImageList.at(m_iCounter));
//p.drawImage(QPointF(80*m_iCounter, 0), m_cImageList.at(m_iCounter));
}
}

CMerchandizeSelector::~CMerchandizeSelector()
{
}

// computes cartesian coordinates for given circle and its angle
QPointF CMerchandizeSelector::computePoint(qreal rOriginX, qreal rOriginY, qreal rRadius, qreal rAngle)
{
QPointF p; // point
p.setX(rOriginX+rRadius*cos(rAngle*Pi/180)); // computes x
p.setY(rOriginX+rRadius*sin(rAngle*Pi/180)); // computes y
//p.setX(rOriginX+rRadius*cos(rAngle)); // computes x
//p.setY(rOriginX+rRadius*sin(rAngle)); // computes y
return p; // returns p
}

// painter event
/*
void CMerchandizeSelector::paintEvent(QPaintEvent *e)
{
QPainter p(this);
p.setClipRect(e->rect());
p.drawTiledPixmap(rect(), m_cPixmap);

for (m_iCounter=0; m_iCounter<m_cImageList.count(); m_iCounter++)
{
//p.drawImage(m_cImagePointList.at(m_iCounter), m_cImageList.at(m_iCounter));
p.drawImage(QPointF(80*m_iCounter, 0), m_cImageList.at(m_iCounter));
}
}
*/Here is header file:
#ifndef CMERCHANDIZESELECTOR_H_
#define CMERCHANDIZESELECTOR_H_

// qt includes
#include <QtGui/QGraphicsView>
//#include <QtGui/QGraphicsScene>
//#include <QtGui/QGraphicsItem>
#include <QtGui/QImage>
//#include <QtCore/QPointer>
//#include <QtCore/QStringList>
#include <QtCore/QPointF>
#include <QtCore/QRect>
//#include <QtCore/QRectF>
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <QtGui/QPaintEvent>
#include <QtDebug>

// math support includes
#include <cmath>

// custom includes
#include "globals.h"

class CMerchandizeSelector : public QGraphicsView
{
Q_OBJECT

public:
CMerchandizeSelector(QWidget* pParent=0);
virtual ~CMerchandizeSelector();

private:
QList<QImage> m_cImageList; // list of icon filenames on 3d graphics browse
QList<QPointF> m_cImagePointList; // list of coordinates for icons
quint16 m_iCounter; // protected loop counter
QPixmap m_cPixmap; // pixmap
//QPointer <CLens> m_pLens; // lens for zoming selected merchandize
//QStringList m_pIconList; // list of icon filenames on 3d graphics browse
/*
void keyPressEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
void wheelEvent(QWheelEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect);
*/
//void paintEvent(QPaintEvent *e); // painter event
QPointF computePoint(qreal rOriginX, qreal rOriginY, qreal rRadius, qreal rAngle);
};

#endif /*CMERCHANDIZESELECTOR_H_*/And here are constants, used in app (file globals.h):
// globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

// qt includes
#include <QtGlobal>

// scene rect coords
static const qint16 minX=0;
static const qint16 minY=0;
static const qint16 maxX=600; // scene width
static const qint16 maxY=600; // scene height

//static const qreal rCenterX=(maxX+minX)/2; // scente centre x
static const qreal rCenterX=(maxX-minX)/2; // scente centre x
//static const qreal rCenterY=(maxY+minY)/2; // scente centre x
static const qreal rCenterY=(maxY-minY)/2; // scente centre x
static const qreal rCirleMargin=20.0; // circle margin
static const qreal rCircleRadius=maxX-rCenterX-rCirleMargin; // radius of circle

// geometric constants
static const qreal pi=3.141592653589793238462643383279502884197169399 37510; // pi constant
static const qreal Pi=pi; // alias for pi
static const quint8 iLoadTreshold=10; // icon load treshold
static const qreal rEmtpyCircle=0; // emtpy circle
static const qreal rFullCircle=360; // full circle has 360 degrees
static const qreal rAngle=rFullCircle/iLoadTreshold; // merchandize icon appears every rAngle degrees on rotary
// merchandize selector

#endifCan anyone help me please why pictures are not shown?! I read the docs about qt's coordinate system, but I simply do not get it how it works!!!! :confused::crying::(

Please help!

jpn
6th December 2007, 09:04
Try using QGraphicsPixmapItems instead of trying to paint images by hand.

MarkoSan
6th December 2007, 09:10
QGraphicsPixmap items? I tried, picture is shown, but I cannot move it to arbitrary coordinate.

jpn
6th December 2007, 09:12
QGraphicsPixmap items? I tried, picture is shown, but I cannot move it to arbitrary coordinate.
You can move them like any other items; QGraphicsItem::setPos().

MarkoSan
6th December 2007, 09:13
But what is the difference between my current approach and qpixmapitem approach?

jpn
6th December 2007, 09:19
But what is the difference between my current approach and qpixmapitem approach?
What's the point using a QGraphicsScene/QGraphicsView without items? QGraphicsView has a sophisticated paintEvent() will its caching facilities and such. There are specialized QGraphicsView::draw*() and QGraphicsItem::paint() methods in case you want to draw anything by hand. I wouldn't suggest reimplementing QGraphicsView::paintEvent(), it will just destroy the fine framework.

MarkoSan
6th December 2007, 16:42
How do I add QImage to QGraphicsScene? I am reading docs and I just do not get it ... :o:confused:

jpn
6th December 2007, 17:39
How do I add QImage to QGraphicsScene? I am reading docs and I just do not get it ... :o:confused:
Again, use QGraphicsPixmapItems. I thought you said you already got one visible. :) You may use QPixmap::fromImage() to convert a QImage to QPixmap.

MarkoSan
6th December 2007, 18:45
I tried to used it before, something did not work. I gor some errors about QGraphicsPixelItem contructor that is private or sometinhg lke that.

jpn
6th December 2007, 19:24
I tried to used it before, something did not work. I gor some errors about QGraphicsPixelItem contructor that is private or sometinhg lke that.
The copy constructor of QGraphicsPixmapItem is private which means that you cannot copy QGraphicsPixmapItems.

The easiest way is to use the convenience method of QGraphicsScene:


QGraphicsPixmapItem* item = scene->addPixmap(pixmap);
item->setPos(...);

but you can create QGraphicsPixmapItems yourself as well:


QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, parent);
scene->addItem(item);
item->setPos(...);

MarkoSan
6th December 2007, 20:56
The copy constructor of QGraphicsPixmapItem is private which means that you cannot copy QGraphicsPixmapItems.

The easiest way is to use the convenience method of QGraphicsScene:


QGraphicsPixmapItem* item = scene->addPixmap(pixmap);
item->setPos(...);
but you can create QGraphicsPixmapItems yourself as well:


QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, parent);
scene->addItem(item);
item->setPos(...);


And one more question: why is the copy contructor of QGraphicsPixmapItem private, I mean, what do we gain with that?

Thank you very very much for help, let me now dig into it and i will report results. :D

jpn
6th December 2007, 21:12
And one more question: why is the copy contructor of QGraphicsPixmapItem private, I mean, what do we gain with that?
It's a programmatical technique to prevent objects from getting copied. Sometimes it makes no sense to copy an object, like a QObject or a QGraphicsItem. Both QObjects and QGraphicsItems are organized in object trees where each object has an optional parent and children. What should happen when such object is copied? Should children get copied? Should parent get copied as well? Should possible signal-slot connections get copied? What person A would expect to happen would be most likely different than person B would expect. The result would be chaotic no matter how the functionality was implemented. Therefore it's just better to prevent it from happening.

PS. Did you notice that your signature says "kUbutnu"? ;)

MarkoSan
6th December 2007, 21:23
It's a programmatical technique to prevent objects from getting copied. Sometimes it makes no sense to copy an object, like a QObject or a QGraphicsItem. Both QObjects and QGraphicsItems are organized in object trees where each object has an optional parent and children. What should happen when such object is copied? Should children get copied? Should parent get copied as well? Should possible signal-slot connections get copied? What person A would expect to happen would be most likely different than person B would expect. The result would be chaotic no matter how the functionality was implemented. Therefore it's just better to prevent it from happening.

PS. Did you notice that your signature says "kUbutnu"? ;)

Thanks for explanation, and, YES, I DID NOTICE my signature says kUbutnu :D, but I wondered who will correct me first. I bet on wysotta but you did it before him. :D

MarkoSan
6th December 2007, 21:59
Ok, jpn, I again tumbed into wall. Loaded pics are different sizes, how do I limit the size of every loaded pic to, for example, to 100x100 points?

wysota
6th December 2007, 22:08
I bet on wysotta but you did it before him. :D

I don't read signatures :)

About your question: use QPixmap::scaled() or QImage::scaled().

jpn
6th December 2007, 22:09
I wondered who will correct me first. I bet on wysotta but you did it before him. :D
Heh, it's been buggering me for a while but I couldn't resist myself anymore.. :)


Ok, jpn, I again tumbed into wall. Loaded pics are different sizes, how do I limit the size of every loaded pic to, for example, to 100x100 points?
Well, it depends a bit what you want to achieve. Both QPixmap and graphics items are scalable. See QPixmap::scaled() and QGraphicsItem::scale().

MarkoSan
6th December 2007, 22:22
Well, these pictures will be placed on "ghost" circle coordinates. And this is ugly if every pic has different size, so every created pixmap must be scaled to predefined size, let me try.

And, let me correct signature so you will not bothered again. :D

jpn
6th December 2007, 22:44
Warning: out of topic

And, let me correct signature
One might still argue about its "correctness". Or did you ever see it written like that? ;)

wysota
6th December 2007, 23:33
Warning: out of topic

One might still argue about its "correctness".

I would... It's Kubuntu or KUbuntu. The "K" stands for KDE which is written with a capital letter.

MarkoSan
7th December 2007, 01:09
I would... It's Kubuntu or KUbuntu. The "K" stands for KDE which is written with a capital letter.

Yeah, man, I've been waiting for genius wysotta to comment previous "warning". Now I must go back to code. :D:D

wysota
7th December 2007, 01:28
My name spells with a single "t". Don't mess it up, please. It's a Slavic name, so you shouldn't be making any mistakes in it.

MarkoSan
7th December 2007, 01:34
I am very sorry if I got you insulted. It won't happen again.

wysota
7th December 2007, 10:01
You didn't but I prefer my name to be spelled correctly.