Pictures won't display - possible qt coordinate system confusion
Hi to all!
With the following code I want to put some pictures on a scene:
Code:
#include "CMerchandizeSelector.h"
CMerchandizeSelector
::CMerchandizeSelector(QWidget* pParent
){
Q_CHECK_PTR(pScene); // checks object creation
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.fill(Qt::black);
//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
) {
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:
Code:
#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"
{
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
//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):
Code:
// 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.14159265358979323846264338327950288419716939937510; // 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
#endif
Can 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!
Re: Pictures won't display - possible qt coordinate system confusion
Try using QGraphicsPixmapItems instead of trying to paint images by hand.
Re: Pictures won't display - possible qt coordinate system confusion
QGraphicsPixmap items? I tried, picture is shown, but I cannot move it to arbitrary coordinate.
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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().
Re: Pictures won't display - possible qt coordinate system confusion
But what is the difference between my current approach and qpixmapitem approach?
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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.
Re: Pictures won't display - possible qt coordinate system confusion
How do I add QImage to QGraphicsScene? I am reading docs and I just do not get it ... :o:confused:
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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.
Re: Pictures won't display - possible qt coordinate system confusion
I tried to used it before, something did not work. I gor some errors about QGraphicsPixelItem contructor that is private or sometinhg lke that.
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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:
but you can create QGraphicsPixmapItems yourself as well:
Code:
scene->addItem(item);
item->setPos(...);
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
jpn
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:
but you can create QGraphicsPixmapItems yourself as well:
Code:
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
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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"? ;)
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
jpn
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
Re: Pictures won't display - possible qt coordinate system confusion
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?
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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().
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
MarkoSan
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.. :)
Quote:
Originally Posted by
MarkoSan
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().
Re: Pictures won't display - possible qt coordinate system confusion
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
Re: Pictures won't display - possible qt coordinate system confusion
Warning: out of topic
Quote:
Originally Posted by
MarkoSan
And, let me correct signature
One might still argue about its "correctness". Or did you ever see it written like that? ;)
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
jpn
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.
Re: Pictures won't display - possible qt coordinate system confusion
Quote:
Originally Posted by
wysota
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