Results 1 to 5 of 5

Thread: add QImage to QGraphisScene

  1. #1

    Default add QImage to QGraphisScene

    Hi!

    I have my own subclass of QGraphicsView which displays a QGraphicsScene. I add lines on the scene. Now I also want do add QImages to the scene (or view).
    I have tried the following:
    I overode QWidget:: paintEvent(QPaintEvent*) in my QGraphicsView subclass:
    Qt Code:
    1. void myViewSubclass:paintEvent(QPaintEvent * event) {
    2. QGraphicsView::paintEvent(event);
    3. QPainter painter;
    4. painter.begin(this);
    5. QPointF point(GetCenter().x(), GetCenter().y());
    6. painter.drawImage(point, *image);
    7. painter.end();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Unfortunately Qt Creator tells me:
    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    But I only paint in the above method which IS the result of a paint Event...
    Where is my mistake? Or is my aprroach to drawing an image on the scene rubbish anyway?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: add QImage to QGraphisScene

    Why not using the QGraphicsScene::addPixmap() method ? It inserts image on the scene and returns an object which you can use to manipulate the image later.

  3. #3

    Default Re: add QImage to QGraphisScene

    Your idea seems to be very good for me. However I have problems adding the pixmap to the GraphicScene's origin, which is supposed to be in the scene's upper left border.
    I tried the following:
    Qt Code:
    1. QGraphicsScene* scene = new QGraphicsScene(0, 0, 500, 500);
    2. view->setScene(scene);
    3. QPixmap pixmap = QPixmap::fromImage(image);
    4. QGraphicsPixmapItem* pixmapItem = scene->addPixmap(pixmap);
    5. pixmapItem->setPos(0, 0);
    To copy to clipboard, switch view to plain text mode 
    however my image now appears somewhere (but not quite!) in the middle of my view.
    Why doesn't it appear in the upper left corner? SetPos is supposed to work in parent's coordinates and when i print the scenePos() coordinates of the PixmapItem I get (0, 0)... But it's not there...

    Does anyone have a clue?

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: add QImage to QGraphisScene

    Read ALL of the documentation on the GraphicsView/GraphicsScene/GraphicsItem framework, including the portions on the different, independent coordinate systems used by each object.

    http://doc.qt.nokia.com/4.7/graphicsview.html

  5. #5

    Default Re: add QImage to QGraphisScene

    I already read the most inportant sections. But, ok I read it all again.
    It says:
    An item's position is the coordinate of the item's center point in its parent's coordinate system. The scene is ... regarded as all parent-less items' "parent".
    I figured out that my QGraphicsPixmapItem is parent-less. Hence its position is its center point in my GraphicScene's coordinate system. As the item's pos() returns (0, 0) I know that the item is in the right place in my scene. The only possible problem could be, that it is not in the (0, 0) of my GraphicsView. I checked this using the QGraphicsView::mapFromScene() function. And indeed this function returned (x, 0) where x depends on my scene's height and width - even though I wanted x to be always 0.
    I don't understand, why a scene QGraphicsScene(0, 0, 500, 500) which I add to my view, is not located at the view's origin (0, 0).
    I guess this is the core of my problem. Maybe you can help me figure out, what I could do.

Similar Threads

  1. QImage
    By andreahmed in forum Qt Programming
    Replies: 5
    Last Post: 25th August 2010, 08:11
  2. QImage
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 31st July 2009, 16:19
  3. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 18:11
  4. QImage
    By mickey in forum Qt Programming
    Replies: 7
    Last Post: 14th July 2006, 22:54
  5. Replies: 3
    Last Post: 15th March 2006, 12:44

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.