Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Pictures won't display - possible qt coordinate system confusion

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Angry 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:
    Qt Code:
    1. #include "CMerchandizeSelector.h"
    2.  
    3. CMerchandizeSelector::CMerchandizeSelector(QWidget* pParent)
    4. {
    5. QGraphicsScene* pScene=new QGraphicsScene(this); // creates new scene
    6. Q_CHECK_PTR(pScene); // checks object creation
    7. pScene->setItemIndexMethod(QGraphicsScene::NoIndex);
    8. setSceneRect(minX, minY, maxX, maxY);
    9. setScene(pScene);
    10. setWindowTitle(trUtf8("Merchandize selector"));
    11. // TODO: sets scene widget flags so it cannot be closed
    12. /*
    13.   m_pIconList=new QStringList; // creates new string list for storing icon names
    14.   Q_CHECK_PTR(m_pIconList); // checks creation
    15. */
    16.  
    17. m_cPixmap=QPixmap(100, 100);
    18. m_cPixmap.fill(Qt::black);
    19.  
    20. QPainter p(&m_cPixmap); // painter
    21. //QPainter p(this); // painter
    22. p.translate(rCenterX, rCenterY); // translated coordinate system to centre of widget
    23. p.setWindow(QRect(minX, minY, maxX, maxY));
    24. p.setViewport(QRect(minX, minY, maxX, maxY));
    25.  
    26. // only for test purposes, images must be read from database
    27. m_cImageList.append(QImage(":/images/apple_cake.png"));
    28. m_cImageList.append(QImage(":/images/bigmac.png"));
    29. m_cImageList.append(QImage(":/images/applejuice.png"));
    30. m_cImageList.append(QImage(":/images/cafe_cool"));
    31. m_cImageList.append(QImage(":/images/chesseburger.png"));
    32. m_cImageList.append(QImage(":/images/coke.png"));
    33.  
    34. // computes positions for images
    35. for (m_iCounter=0; m_iCounter<iLoadTreshold; m_iCounter++)
    36. {
    37. QPointF tmpPoint=computePoint(0, 0, rCircleRadius, m_iCounter*rAngle*Pi/180);
    38. m_cImagePointList.append(tmpPoint);
    39. qDebug() << "(" << tmpPoint.rx() << ", " << tmpPoint.ry() << "); ";
    40. }
    41.  
    42. // draws an image at computed coords
    43. for (m_iCounter=0; m_iCounter<m_cImageList.count(); m_iCounter++)
    44. {
    45. p.drawImage(m_cImagePointList.at(m_iCounter), m_cImageList.at(m_iCounter));
    46. //p.drawImage(QPointF(80*m_iCounter, 0), m_cImageList.at(m_iCounter));
    47. }
    48. }
    49.  
    50. CMerchandizeSelector::~CMerchandizeSelector()
    51. {
    52. }
    53.  
    54. // computes cartesian coordinates for given circle and its angle
    55. QPointF CMerchandizeSelector::computePoint(qreal rOriginX, qreal rOriginY, qreal rRadius, qreal rAngle)
    56. {
    57. QPointF p; // point
    58. p.setX(rOriginX+rRadius*cos(rAngle*Pi/180)); // computes x
    59. p.setY(rOriginX+rRadius*sin(rAngle*Pi/180)); // computes y
    60. //p.setX(rOriginX+rRadius*cos(rAngle)); // computes x
    61. //p.setY(rOriginX+rRadius*sin(rAngle)); // computes y
    62. return p; // returns p
    63. }
    64.  
    65. // painter event
    66. /*
    67. void CMerchandizeSelector::paintEvent(QPaintEvent *e)
    68. {
    69.   QPainter p(this);
    70.   p.setClipRect(e->rect());
    71.   p.drawTiledPixmap(rect(), m_cPixmap);
    72.  
    73.   for (m_iCounter=0; m_iCounter<m_cImageList.count(); m_iCounter++)
    74.   {
    75.   //p.drawImage(m_cImagePointList.at(m_iCounter), m_cImageList.at(m_iCounter));
    76.   p.drawImage(QPointF(80*m_iCounter, 0), m_cImageList.at(m_iCounter));
    77.   }
    78. }
    79. */
    To copy to clipboard, switch view to plain text mode 
    Here is header file:
    Qt Code:
    1. #ifndef CMERCHANDIZESELECTOR_H_
    2. #define CMERCHANDIZESELECTOR_H_
    3.  
    4. // qt includes
    5. #include <QtGui/QGraphicsView>
    6. //#include <QtGui/QGraphicsScene>
    7. //#include <QtGui/QGraphicsItem>
    8. #include <QtGui/QImage>
    9. //#include <QtCore/QPointer>
    10. //#include <QtCore/QStringList>
    11. #include <QtCore/QPointF>
    12. #include <QtCore/QRect>
    13. //#include <QtCore/QRectF>
    14. #include <QtGui/QPainter>
    15. #include <QtGui/QPixmap>
    16. #include <QtGui/QPaintEvent>
    17. #include <QtDebug>
    18.  
    19. // math support includes
    20. #include <cmath>
    21.  
    22. // custom includes
    23. #include "globals.h"
    24.  
    25. class CMerchandizeSelector : public QGraphicsView
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. CMerchandizeSelector(QWidget* pParent=0);
    31. virtual ~CMerchandizeSelector();
    32.  
    33. private:
    34. QList<QImage> m_cImageList; // list of icon filenames on 3d graphics browse
    35. QList<QPointF> m_cImagePointList; // list of coordinates for icons
    36. quint16 m_iCounter; // protected loop counter
    37. QPixmap m_cPixmap; // pixmap
    38. //QPointer <CLens> m_pLens; // lens for zoming selected merchandize
    39. //QStringList m_pIconList; // list of icon filenames on 3d graphics browse
    40. /*
    41.   void keyPressEvent(QKeyEvent *event);
    42.   void timerEvent(QTimerEvent *event);
    43.   void wheelEvent(QWheelEvent *event);
    44.   void drawBackground(QPainter *painter, const QRectF &rect);
    45. */
    46. //void paintEvent(QPaintEvent *e); // painter event
    47. QPointF computePoint(qreal rOriginX, qreal rOriginY, qreal rRadius, qreal rAngle);
    48. };
    49.  
    50. #endif /*CMERCHANDIZESELECTOR_H_*/
    To copy to clipboard, switch view to plain text mode 
    And here are constants, used in app (file globals.h):
    Qt Code:
    1. // globals.h
    2.  
    3. #ifndef GLOBALS_H
    4. #define GLOBALS_H
    5.  
    6. // qt includes
    7. #include <QtGlobal>
    8.  
    9. // scene rect coords
    10. static const qint16 minX=0;
    11. static const qint16 minY=0;
    12. static const qint16 maxX=600; // scene width
    13. static const qint16 maxY=600; // scene height
    14.  
    15. //static const qreal rCenterX=(maxX+minX)/2; // scente centre x
    16. static const qreal rCenterX=(maxX-minX)/2; // scente centre x
    17. //static const qreal rCenterY=(maxY+minY)/2; // scente centre x
    18. static const qreal rCenterY=(maxY-minY)/2; // scente centre x
    19. static const qreal rCirleMargin=20.0; // circle margin
    20. static const qreal rCircleRadius=maxX-rCenterX-rCirleMargin; // radius of circle
    21.  
    22. // geometric constants
    23. static const qreal pi=3.14159265358979323846264338327950288419716939937510; // pi constant
    24. static const qreal Pi=pi; // alias for pi
    25. static const quint8 iLoadTreshold=10; // icon load treshold
    26. static const qreal rEmtpyCircle=0; // emtpy circle
    27. static const qreal rFullCircle=360; // full circle has 360 degrees
    28. static const qreal rAngle=rFullCircle/iLoadTreshold; // merchandize icon appears every rAngle degrees on rotary
    29. // merchandize selector
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 
    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!!!!

    Please help!
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Try using QGraphicsPixmapItems instead of trying to paint images by hand.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    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().
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pictures won't display - possible qt coordinate system confusion

    But what is the difference between my current approach and qpixmapitem approach?
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    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.
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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 ...
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    How do I add QImage to QGraphicsScene? I am reading docs and I just do not get it ...
    Again, use QGraphicsPixmapItems. I thought you said you already got one visible. You may use QPixmap::fromImage() to convert a QImage to QPixmap.
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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.
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    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:
    Qt Code:
    1. QGraphicsPixmapItem* item = scene->addPixmap(pixmap);
    2. item->setPos(...);
    To copy to clipboard, switch view to plain text mode 
    but you can create QGraphicsPixmapItems yourself as well:
    Qt Code:
    1. QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, parent);
    2. scene->addItem(item);
    3. item->setPos(...);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by jpn View Post
    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:
    Qt Code:
    1. QGraphicsPixmapItem* item = scene->addPixmap(pixmap);
    2. item->setPos(...);
    To copy to clipboard, switch view to plain text mode 
    but you can create QGraphicsPixmapItems yourself as well:
    Qt Code:
    1. QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, parent);
    2. scene->addItem(item);
    3. item->setPos(...);
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt 5.3 Opensource & Creator 3.1.2

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    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"?
    J-P Nurmi

  13. #13
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by jpn View Post
    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 , but I wondered who will correct me first. I bet on wysotta but you did it before him.
    Qt 5.3 Opensource & Creator 3.1.2

  14. #14
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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?
    Qt 5.3 Opensource & Creator 3.1.2

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    I bet on wysotta but you did it before him.
    I don't read signatures

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

  16. #16
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by MarkoSan View Post
    I wondered who will correct me first. I bet on wysotta but you did it before him.
    Heh, it's been buggering me for a while but I couldn't resist myself anymore..

    Quote Originally Posted by MarkoSan View Post
    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().
    J-P Nurmi

  17. #17
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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.
    Qt 5.3 Opensource & Creator 3.1.2

  18. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Warning: out of topic
    Quote Originally Posted by MarkoSan View Post
    And, let me correct signature
    One might still argue about its "correctness". Or did you ever see it written like that?
    J-P Nurmi

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by jpn View Post
    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.

  20. #20
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pictures won't display - possible qt coordinate system confusion

    Quote Originally Posted by wysota View Post
    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.
    Qt 5.3 Opensource & Creator 3.1.2

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.