Hi,
I created graphicsview with vertical layout. So, each item in layout has equal size.Next, I need to repositioning items based on its pixmap size.
How to achieve this?
Simple example, that doesn't work. Item layout not updating properly.

Qt Code:
  1. #include <QApplication>
  2. #include <QtWidgets>
  3.  
  4. class Item : public QGraphicsPixmapItem,
  5. public QGraphicsLayoutItem
  6. {
  7. public:
  8. Item(QGraphicsItem *parent =0): QGraphicsPixmapItem(parent),
  9. QGraphicsLayoutItem(0, false)
  10. {}
  11. virtual QSizeF sizeHint(Qt::SizeHint, const QSizeF&) const
  12. {
  13. return QSize(500,500);
  14. }
  15. virtual void paint( QPainter* painter,const QStyleOptionGraphicsItem * o, QWidget* w) {
  16. painter->setBrush(Qt::white);
  17. painter->drawRect(boundingRect());
  18. QGraphicsPixmapItem::paint(painter,o,w);
  19. }
  20. virtual void setGeometry(const QRectF &rect)
  21. {
  22. prepareGeometryChange();
  23. QGraphicsLayoutItem::setGeometry(rect);
  24. setPos(rect.topLeft());
  25. }
  26. virtual void updateGeometry() {
  27. QGraphicsLayoutItem::updateGeometry();
  28. }
  29. virtual QRectF boundingRect() const
  30. {
  31. return QRectF(QPointF(0,0), geometry().size());
  32. }
  33. };
  34.  
  35.  
  36. int main(int argv, char *argc[])
  37. {
  38. QApplication app(argv, argc);
  39.  
  40. view.setScene(scene);
  41. QGraphicsWidget * widget = new QGraphicsWidget;
  42. QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
  43. widget->setLayout(layout);
  44. scene->addItem(widget);
  45. QList<Item*> items;
  46. for(int i=0; i<10; i++) {
  47. Item *item = new Item();
  48. items.insert(i,item);
  49. scene->addItem(item);
  50. layout->addItem(item);
  51. }
  52. // data
  53. QImage im("cat.jpg");
  54. qDebug() << im.size(); // QSize(960, 708)
  55. for(int i=0; i<5; i++){
  56. items.at(i)->setPixmap(QPixmap::fromImage(im));
  57. items.at(i)->updateGeometry(); // what?
  58. }
  59.  
  60. view.show();
  61.  
  62. return app.exec();
  63. }
To copy to clipboard, switch view to plain text mode