Results 1 to 10 of 10

Thread: elastic QGraphicsItem (layout problem)

  1. #1
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default elastic QGraphicsItem (layout problem)

    Hy

    I need a elastic layout so that when you stretch the mainwindow all items in it also stretch.

    I have:

    1) QMainWindow
    2) GraphicsView
    3) GraphicsItem on the Scene


    Currently i have a grid layout added to my class.
    My GraphicsView i set to expanding in the designer so that's good. But i have a QGraphicsItem circle that does not expanding together with his parent.

    What i need is when you make window smaller, item also smaller. When you make window bigger item also bigger. How can i acomplish this? I'm searching and reading some hour or so and can't find it.

    I'm not sure what i'm looking for, but i thought that something like this in the constructor i need:

    pseudo-code:
    this_constructed_object->stretch_with_parent_QGraphicsView

    i don't really know...but i think that i need something like that in the constructor of my class:

    Qt Code:
    1. CircleItem::CircleItem(QGraphicsItem *parent)
    2. : QGraphicsItem(parent)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: elastic QGraphicsItem (layout problem)

    It will not be solved using a custom layout.
    You may need to subclass QGraphicsView and override its resizeEvent(). In that, you need to zoom/scale the view so each item will be scaled respectively.

  3. #3
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: elastic QGraphicsItem (layout problem)

    Thanks, i understand what you'r saying, but is there a tutorial covering something similair maybe?

  4. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: elastic QGraphicsItem (layout problem)

    Hi,

    I dont think that your problem is related to the constructor of your item.

    In fact, you want a no conventionnal display of your QGraphicsView. Remember that is only a view of a QGraphicsScene.

    So, what do you want to do exactly? When your view is resized, do you changes the positions and size of your items in the scene? Or you just want that the scene rendering will stretch the size of the view?
    Not quite same needs, isn't it?

    And what about moving in the scene? Don't you need that features?

    Anyway, have a look at QGraphicsView::transform and QGraphicsView::scaleDont forget, if you scale the view according to its rect size, to reset the transformation matrix because scale() scales the view form current matrix.

    I hope I was helpful.

  5. #5
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: elastic QGraphicsItem (layout problem)

    Please see this:
    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3. #include <QLayout>
    4. #include <QGraphicsView>
    5.  
    6. class MyView:public QGraphicsView
    7. {
    8. public:
    9. MyView(QWidget *parent = 0):QGraphicsView(parent)
    10. {}
    11. protected:
    12. void resizeEvent(QResizeEvent * evt)
    13. {
    14. this->scale(x,y);//DO SOME WORK HERE
    15. QGraphicsView::resizeEvent(evt);
    16. }
    17. };
    18.  
    19.  
    20. int main(int argc, char **argv)
    21. {
    22. QApplication app(argc, argv);
    23. QDialog dlg;
    24. QVBoxLayout *vl=new QVBoxLayout(&dlg);
    25. MyView mv(&dlg);
    26. mv.setScene(&sc);
    27. sc.addRect(10,10,200,200);
    28. vl->addWidget(&mv);
    29. dlg.show();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

    You just need to calculate the scaling factor depending upon the current size of gv.
    Pass that scaling factor in scale function of gv in resizeEvent.

  6. #6
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: elastic QGraphicsItem (layout problem)

    @Scasio:



    ...




    Btw.

    Yes, thx for the info but i haven't understood everything. I just need a simple way to resize items accordingly to the window (parent)
    Last edited by petar; 11th November 2009 at 14:07.

  7. #7
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: elastic QGraphicsItem (layout problem)

    Have you tried my way ?

  8. #8
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: elastic QGraphicsItem (layout problem)

    Quote Originally Posted by yogeshgokul View Post

    You just need to calculate the scaling factor depending upon the current size of gv.
    Pass that scaling factor in scale function of gv in resizeEvent.

    Thanks yogeshgokul i will do some simple examples to figure it out.
    It's different because in my app i have different setup.

    here i go:


    Qt Code:
    1. //Program entry point: instancing object of RuletV1 class
    2. int main(int argc, char *argv[])
    3. {
    4. QApplication a(argc, argv);
    5. RuletV1 w;
    6. w.show();
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //RuletV1.cpp
    2. RuletV1::RuletV1(QWidget *parent, Qt::WFlags flags)
    3. : QMainWindow(parent, flags) {
    4.  
    5. ui.setupUi(this);
    6. ui.graphicsView->setScene(scena);
    7. m_elipsa = new CircleItem();
    8. scena->addItem(m_elipsa);
    9.  
    10. } RuletV1::~RuletV1(){}
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //CirceItem.cpp
    2. CircleItem::CircleItem(QGraphicsItem *parent)
    3. : QGraphicsItem(parent) {}
    4. //destructor
    5. CircleItem::~CircleItem() {}
    6.  
    7. QRectF CircleItem::boundingRect() const { return QRectF(0,0,100,100); }
    8.  
    9. void CircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    10. QWidget *widget)
    11. {
    12. painter->setRenderHint(QPainter::Antialiasing);
    13. painter->drawLine(0,50, 100, 50);
    14. painter->drawLine(50, 0, 50, 100);
    15. painter->drawEllipse(0, 0, 100, 100);
    16. }
    To copy to clipboard, switch view to plain text mode 


    If i understand then you have also GraphicsView but not like me, you make yourself a class that inherits QGraphicsView, you named it MyView. And in that class you override a method called resizeEvent.

    The rest i understand...i need to study that resizeEvent method and how to calculate scaling. Thx

  9. #9
    Join Date
    Oct 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: elastic QGraphicsItem (layout problem)

    Quote Originally Posted by yogeshgokul View Post
    Have you tried my way ?
    i'm trying -> will post result later

  10. #10
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: elastic QGraphicsItem (layout problem)

    You only need to compute the right scale factor, according to the size of the widget,.

    try something like this :

    Qt Code:
    1. void YourGraphicsView::resizeEvent ( QResizeEvent * event ) {
    2.  
    3. QGraphicsView::resizeEvent(event);
    4.  
    5. const QSize& oldSize = event->oldSize();
    6. const QSize& newSize = event->size();
    7.  
    8. //be aware of integer division and division by zero
    9. double scaleFactorX = (oldSize.width ()>0) ? ((double)newSize.width())/oldSize.width ():1;
    10. double scaleFactorY = (oldSize.height()>0) ?((double)newSize.height())/oldSize.height():1;
    11.  
    12. //sclae the view
    13. scale(scaleFactorX, scaleFactorY);
    14. }
    To copy to clipboard, switch view to plain text mode 

    But I am not sure that the behaviour will be correct for all cases.

Similar Threads

  1. QGLWidget problem
    By MarkoSan in forum Qt Programming
    Replies: 33
    Last Post: 8th December 2007, 15:50
  2. problem with Tool button n Layout
    By arjunasd in forum Qt Tools
    Replies: 11
    Last Post: 24th July 2007, 21:14
  3. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 12:17
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 4
    Last Post: 19th April 2006, 11:08

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.