Results 1 to 7 of 7

Thread: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout)

  1. #1
    Join Date
    Mar 2006
    Location
    Argentina - CABA
    Posts
    66
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11

    Default QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout)

    Hi!

    I'm developing a class that must contain like "products for sale list" in the form of graphics (QPixmap). So, I've chose QGraphicsView (for a similiar situation, in the past I've used QScrollArea and a QGridLayout to manage widgets buts is slow when you use to many widgets). The problem is that I've never used it.

    What I plan to do is: having a way to arrange QPixmaps items inside QGraphicsView, so my question is: Do I need a layout to handle pixmaps in a easy way? Having in mind that items can be added and deleted so I must re-arrange items.

    I've reading how QGraphicsViews works, the scene and such, but I don't have clear how to work with a layout and pixmaps items inside this.

    Can anyone supply a small example code?

    I will be very grateful.

    Thanks in advance.
    Gustavo A. DÃ*az
    artistic.gdnet.com.ar

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout

    I'm developing a class that must contain like "products for sale list" in the form of graphics (QPixmap). So, I've chose QGraphicsView (for a similiar situation, in the past I've used QScrollArea and a QGridLayout to manage widgets buts is slow when you use to many widgets).
    How many widgets are there?

    What I plan to do is: having a way to arrange QPixmaps items inside QGraphicsView, so my question is: Do I need a layout to handle pixmaps in a easy way? Having in mind that items can be added and deleted so I must re-arrange items.
    One easy way is to use QGraphicsWidget and QGraphicsLayout. If you want to use QGraphicsPixmapItem then you have take care to layout yourself.

    I've reading how QGraphicsViews works, the scene and such, but I don't have clear how to work with a layout and pixmaps items inside this.

    Can anyone supply a small example code?
    This is an example to use QGraphicsWidget
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QGraphicsWidget * widget = new QGraphicsWidget;
    5. QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
    6.  
    7. for(int i = 0; i < 10; i++)
    8. {
    9. QLabel * label = new QLabel;
    10. label->setPixmap(QPixmap("C:/image.jpg"));
    11. QGraphicsWidget * w = scene.addWidget(label);
    12. layout->addItem(w);
    13. }
    14.  
    15. widget->setLayout(layout);
    16. scene.addItem(widget);
    17. view.setScene(&scene);
    18. view.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    GuS (30th October 2013)

  4. #3
    Join Date
    Mar 2006
    Location
    Argentina - CABA
    Posts
    66
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11

    Default Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout

    Thanks for reply! yesterdays after reading, which you said the same, in some forums i got the conclusion about QGraphicsPixmapItem about the layout. So, I use QLabel to achieve this, plus using QGraphicsLinearLayout

    Thanks!, now I will test things over this

    Cheers.
    Gustavo A. DÃ*az
    artistic.gdnet.com.ar

  5. #4
    Join Date
    Mar 2006
    Location
    Argentina - CABA
    Posts
    66
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11

    Default Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout

    Well, here is the basic example of how it looks the test code: graphic_View_TEST.png

    I use QLabel but with setText() and using HTML code, so I can display both Pixmap and Text

    Cheers.
    Gustavo A. DÃ*az
    artistic.gdnet.com.ar

  6. #5
    Join Date
    Mar 2006
    Location
    Argentina - CABA
    Posts
    66
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11

    Default Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout

    Santosh Reddy: I have another dub: in this example I've subclassed QLabel, to implemente a focus "effect" (by using focusInEvent and focusOutEvent event, and stylesheet to change border color when selected). Now, how i do select multiple items/widgets in QGraphicsView?

    Thanks.
    Gustavo A. DÃ*az
    artistic.gdnet.com.ar

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout

    Set QGraphicsItem::ItemIsSelectable flag for the QGraphicsWidget (subclassed QLabel). Note that you will have to draw the selection rectangle explicitly.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #7
    Join Date
    Mar 2006
    Location
    Argentina - CABA
    Posts
    66
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11

    Default Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout

    Yeah, already applied that flag (Python):

    graphicItem.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsSelectable)
    And for the subclassed QLabel, for focus i've added:

    def focusInEvent(self, event):
    self.focusState.emit(True)
    self.setStyleSheet("QLabel{background-color: transparent; border: 2px solid #90b6e2; border-radius: 5px;}")
    return QLabel.focusInEvent(self, event)


    def focusOutEvent(self, event):
    self.focusState.emit(False)
    self.setStyleSheet("QLabel{background-color: transparent; border: 2px solid transparent; border-radius: 5px;}")
    return QLabel.focusInEvent(self, event)
    This last one, would be a problem for selecting more than one QLabel/widget at time? or is something that i must handle from QGraphicsView itself?
    Gustavo A. DÃ*az
    artistic.gdnet.com.ar

Similar Threads

  1. Replies: 6
    Last Post: 26th September 2013, 06:53
  2. Replies: 8
    Last Post: 19th July 2011, 10:52
  3. One QGraphicsScene in two QGraphicsViews
    By klaviaturist in forum Qt Programming
    Replies: 9
    Last Post: 30th March 2011, 09:37
  4. Can't change items aligment in QGraphicsLinearLayout
    By T4ng10r in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2009, 10:33
  5. Maximize width of items in QGraphicsLinearLayout
    By haTem in forum Qt Programming
    Replies: 7
    Last Post: 24th May 2009, 10:53

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
  •  
Qt is a trademark of The Qt Company.