Results 1 to 20 of 23

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

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.