Results 1 to 8 of 8

Thread: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

  1. #1
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    Hey Guys,

    I subclassed QGraphicsView and an QGraphicsPixmapItem...
    What i want: I want to add a Pixmap to the GraphicsView and resize it (if the pixmap does not have it's desired maximum size) on resizing the GraphicsView..

    What i receive: an unhandled exception in QPaintDevice:aintingActive()

    Qt Code:
    1. inline bool QPaintDevice::paintingActive() const
    2. { return painters != 0; } // seems i get the exception here
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. BackgroundItem::BackgroundItem(const QPixmap &pixmap)
    2. {
    3. myOriginalPixmap = pixmap;
    4. }
    5.  
    6. QPixmap BackgroundItem::getOriginalPixmap(){
    7. return myOriginalPixmap;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void FloorPlanGraphicsView::init()
    2. {
    3. canvas = new QGraphicsScene(this);
    4. //scene->setSceneRect(0,0,this->geometry().width(),this->geometry().height();
    5. setScene(canvas);
    6. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    7. }
    8.  
    9. void FloorPlanGraphicsView::setFloorPlan(const QPixmap &image){
    10. BackgroundItem *backImg = new BackgroundItem(image);//.scaled(geometry().width(),geometry().height(),Qt::KeepAspectRatio));
    11. scene()->addItem(backImg);
    12. backImg->setPos(0,0);
    13. backImg->setZValue(0);
    14. backImg->setPixmap(image.scaled(geometry().width(),geometry().height(),Qt::KeepAspectRatio));
    15. }
    16. void FloorPlanGraphicsView::resizeEvent(QResizeEvent *event){
    17. scene()->setSceneRect(0,0,this->geometry().width(),this->geometry().height());
    18.  
    19. QList<QGraphicsItem*> graphicItems = scene()->items();
    20. for (int i = 0; i < graphicItems.size(); ++i){
    21.  
    22. if (graphicItems.at(i)->type() == FloorPlanGraphicsView::BackgroundType){
    23. BackgroundItem *myBackItem = qgraphicsitem_cast<BackgroundItem*>(graphicItems.at(i));
    24.  
    25. if(this->geometry().width() <= myBackItem->getOriginalPixmap().size().width() && this->geometry().height() <= myBackItem->getOriginalPixmap().size().height())
    26. // i get the error immediately here on resize!
    27. myBackItem->setPixmap(myBackItem->getOriginalPixmap().scaled(this->geometry().width(),this->geometry().height(),Qt::KeepAspectRatio));
    28. else
    29. myBackItem->setPixmap(myBackItem->getOriginalPixmap());
    30. }
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    The invocation to setFloorPlan() looks like the following:
    Qt Code:
    1. ui.floorPlan->setFloorPlan(QPixmap::fromImage(QImage::fromData(query.value(0).toByteArray())));
    To copy to clipboard, switch view to plain text mode 

    Do i have to reimplement a paint() function or something similar here?
    Anbody any idea for me?

  2. #2
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    Has really nobody an idea for me? =/

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    Maybe the following is your problem.

    From the docs (link):
    T qgraphicsitem_cast ( QGraphicsItem * item )

    Returns the given item cast to type T if item is of type T; otherwise, 0 is returned.
    Note: To make this function work correctly with custom items, reimplement the type() function for each custom QGraphicsItem subclass.
    Check out the diagramscene and elasticnodes examples.

    In backgrounditem.h add:
    Qt Code:
    1. enum { Type = UserType +1 };
    2. int type() const { return Type; }
    To copy to clipboard, switch view to plain text mode 
    And in FloorPlanGraphicsView::resizeEvent() change:
    Qt Code:
    1. //if (graphicItems.at(i)->type() == FloorPlanGraphicsView::BackgroundType){
    2. if (graphicItems.at(i)->type() == BackgroundItem::Type{
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    Ney norobro!

    Thanks for your reply, i have not posted that part of code cauze i thought it might not be useful for a reader - but i am aware of that ..
    Here is the code from the header:

    Qt Code:
    1. class FloorPlanGraphicsView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. FloorPlanGraphicsView(QWidget * parent = 0);
    7. FloorPlanGraphicsView(QGraphicsScene * scene, QWidget * parent = 0);
    8. ~FloorPlanGraphicsView();
    9.  
    10. void setFloorPlan(const QPixmap & image);
    11. void setIndicator(const QImage & image);
    12.  
    13. static const int IndicatorType = QGraphicsItem::UserType + 1;
    14. static const int BackgroundType = QGraphicsItem::UserType + 2;
    15.  
    16. protected:
    17. virtual void resizeEvent(QResizeEvent *);
    18. virtual void mouseDoubleClickEvent(QMouseEvent *event);
    19.  
    20. private:
    21. void init();
    22. void placeIndicator(const QPointF & point);
    23. void removeIndicator(const QPointF & point);
    24. QGraphicsScene *canvas;
    25. QImage m_indicator;
    26.  
    27. };
    28.  
    29. class ImageItem: public QGraphicsRectItem
    30. {
    31. public:
    32. ImageItem( QImage img );
    33.  
    34. int type() const { return FloorPlanGraphicsView::IndicatorType; }
    35. protected:
    36. void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
    37. private:
    38. QImage image;
    39. QPixmap pixmap;
    40. };
    41.  
    42. class BackgroundItem: public QGraphicsPixmapItem
    43. {
    44. public:
    45. BackgroundItem(const QPixmap &pixmap);
    46.  
    47. virtual int type() const { return FloorPlanGraphicsView::BackgroundType; }
    48. QPixmap getOriginalPixmap();
    49. private:
    50. QPixmap myOriginalPixmap;
    51. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    I don't think qgraphicsitem_cast will work the way you are using it.

    Check your pointer in debug mode:
    Qt Code:
    1. BackgroundItem *myBackItem = qgraphicsitem_cast<BackgroundItem*>(graphicItems.at(i));
    2. Q_ASSERT(myBackItem);
    To copy to clipboard, switch view to plain text mode 

    Try static_cast instead.

  6. #6
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    I changed the stuff a little bit and got another error facing the same problem as it seems ... as found out that the qgraphicsitem_cast isn't working i wanted to reply here -> then i saw your reply

    Anyways .. you're absolutely right.. and static_cast works as i want it to work... (in my new case - i will reconstruct the old one with the old files from SVN)

    Could you give me an explanation why "qgraphicsitem_cast" is not working as i expected? Is that my fault or what's wrong here?

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    I'm not sure what the advantage of qgraphicsitem_cast is. Maybe someone can enlighten us.

    Anyway, here's the source code for qgraphicsitem_cast from qgraphicsitem.h:
    Qt Code:
    1. template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
    2. {
    3. return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
    4. || (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Since you don't have "Type" defined in your BackgroundItem class, int(static_cast<BackgroundItem *>(0)->Type) returns 7 which is the parent Type (QGraphicsPixmapItem).
    Thus:
    The first part of the return statement above becomes: 7==1 which is false;
    The conditional operator expression (true && 7 == 65538) is false;
    Therefore zero is returned and you end up with a null pointer.

    Hope this makes sense.
    Last edited by norobro; 1st December 2011 at 02:38. Reason: Typo

  8. The following user says thank you to norobro for this useful post:

    shock (3rd December 2011)

  9. #8
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView with QGraphicsPixmapItem that resizes on resize of QGraphicsView

    Hmm what you said makes sense but i do NOT get the sense of that graphicsitem_cast .. looks complete senseless ...

    Qt Code:
    1. template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
    2. {
    3. return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
    4. || (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
    5. }
    To copy to clipboard, switch view to plain text mode 

    I do not really get that part: "int(static_cast<T>(0)->Type) == item->type()"
    What should that comparison be useful for?

Similar Threads

  1. Replies: 5
    Last Post: 19th November 2011, 23:05
  2. How to resize a QGraphicsScene to a QGraphicsView?
    By CassioTC in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2011, 10:03
  3. QGraphicsView won't update until resize
    By stevel in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2009, 22:45
  4. getting QGraphicsView to resize
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 22nd January 2008, 04:49
  5. QGraphicsScene / QGraphicsView speed after resize
    By themolecule in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 00:46

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.