Results 1 to 14 of 14

Thread: Item size not updating after scaling QGraphicsView

  1. #1
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Post Item size not updating after scaling QGraphicsView

    i want to show some images vertically using QGraphicsView and QLabel in pyqt.

    i am using scale method for scaling QGraphicsView. the problem is that when i scale (zoom in) QGraphicsView size of labels not updating and because of that when i scale QGraphicsView and setting label's pixmap again resulting image quality is poor.

    here is my code:
    Qt Code:
    1. my_graphics_view = QtGui.QGraphicsView()
    2. scene = QtGui.QGraphicsScene()
    3. graphics_widget = QtGui.QGraphicsWidget()
    4. layout = QtGui.QGraphicsLinearLayout(QtCore.Qt.Vertical)
    5. default_size = QtCore.QSize(400 , 400)
    6. for number in xrange(num_pages):
    7. lbl = QtGui.QLabel()
    8. lbl.setMinimumSize(default_size)
    9. lbl.setStyleSheet("background-color : white")
    10. lbl.setContentsMargins(0, 0, 0, 0)
    11. lbl.setScaledContents(True)
    12. widget = scene.addWidget(lbl)
    13. layout.addItem(widget)
    14. graphics_widget.setLayout(layout)
    15. scene.addItem(graphics_widget)
    16. my_graphics_view.setScene(scene)
    17. my_graphics_view.show()
    To copy to clipboard, switch view to plain text mode 

    How can I get proper size for labels or make them resize automatically?
    Last edited by Santa.fat; 26th October 2015 at 07:50.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Item size not updating after scaling QGraphicsView

    Any reason you are using a label and not a QGraphicsPixmapItem?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    I want using a white background for empty pages because sometimes I want to add static number of items with same size to QGraphicsView and only some of them have pixmaps and sometimes pixmaps changed

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Item size not updating after scaling QGraphicsView

    So?

    Sounds like a QGraphicsPixmapItem on top of a QGraphicsRetangleItem.
    Or a custom item that paints a background and then the image

    Cheers,
    _

  5. #5
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    Could you please give me a pseudo code or some example?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Item size not updating after scaling QGraphicsView

    The graphics item framework allows you to create new items by either aggregating existing items or by implementing your own items directly.

    For the first you could, for example, group a rectangle item and a pixmap item in a graphics item group, or using the rectangle item as the parent of the pixmap item.
    For the latter, there is a simple example in the documentation of QGraphicsItem, basically providing a bounding rectangle and some custom painting.

    The examples provided by in Qt's documentation have occurences of both approaches http://doc.qt.io/qt-5/examples-graphicsview.html

    Cheers,
    _

  7. #7
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    thank you anda_skoa but i didn't see any example for QGraphicsPixmapItem on top of a QGraphicsRetangleItem.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Item size not updating after scaling QGraphicsView

    I am sorry, did you expect that any of the examples would be miraculously have exactly the combined item you wanted instead of showing generically useful techniques?

    Cheers,
    _

  9. #9
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    no but i do not know know how to combine QGraphicsPixmapItem and QGraphicsRetangleItem. any example or pseudo code of how can i fix my problem would be helpful.
    Last edited by Santa.fat; 26th October 2015 at 17:05.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Item size not updating after scaling QGraphicsView

    Either by using a QGraphicsItemGroup (as already mentioned in comment #6) or by having both as children of a common parent, or by using the rectangle as the parent of the pixmap (as also already mentioned in comment #6)

    Cheers,
    _

  11. #11
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    dear anda_skoa this is my first time using QGraphicsView and i am a little bit confused. could you please give me pseudo code even in 5 lines of code.

    thank you

  12. #12
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    hi.

    could you please give me an example of how to arrange QGraphicsPixmapItem in QGraphicsScene vertically?

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Item size not updating after scaling QGraphicsView

    Using a rect item as a parent of a pixmap item
    Qt Code:
    1. QGraphicsItem *pixmap = new QGraphicsPixmapItem(rect);
    To copy to clipboard, switch view to plain text mode 
    or with aggregation
    Qt Code:
    1. class MyItem : public QGraphicsRectItem
    2. {
    3. public:
    4. MyItem(QGraphicsItem *parent = 0) : QGraphicsRectItem(parent)
    5. {
    6. m_pixmap = new QGraphicsPixmapItem(this);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    or, as said earlier, using QGraphicsItemGroup

    Cheers,
    _

  14. The following user says thank you to anda_skoa for this useful post:

    Santa.fat (30th October 2015)

  15. #14
    Join Date
    Oct 2015
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Item size not updating after scaling QGraphicsView

    thank you.

    can i set a fixed size for items? becuase i want set a fixed size for all of my items and then fill theme with pixmap.
    another question is that how can i get the size of item after scaling?
    Last edited by Santa.fat; 29th October 2015 at 19:03.

Similar Threads

  1. Replies: 0
    Last Post: 14th June 2015, 18:50
  2. Replies: 2
    Last Post: 20th February 2012, 07:30
  3. QGraphicsView not updating properly.
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2009, 10:36
  4. Updating QGraphicsView
    By has2k1 in forum Newbie
    Replies: 2
    Last Post: 8th April 2007, 21:02
  5. Updating Foreground for QGraphicsView
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 4th January 2007, 15:28

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.