Results 1 to 14 of 14

Thread: How to draw QGraphicsItem at the same location?

  1. #1
    Join Date
    May 2011
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to draw QGraphicsItem at the same location?

    H'llo there,

    I'm trying to paint a QGraphicsItem to stay at the top edge of my view, but when I scroll the view down, the item scrolls off the view.
    I have connected the vertical scroll bar valueChanged() function to call my slot:
    Qt Code:
    1. // vertical_scroll is the slot that gets called
    2. // ival comes from valueChanged(int)
    3. void my_graphics_item::vertical_scroll(int ival)
    4. {
    5. int top = pgraphicsview->viewport()->rect().top();
    6. this->moveBy(0, top);
    7. this->update();
    8. }
    To copy to clipboard, switch view to plain text mode 

    1/ when this slot is called, why is top always 0? (the view is scrolled down, but this is always returned as 0)
    2/ when I use ival instead of top, item moves down in big steps.
    3/ how do I solve this issue? What am I doing wrong?

    I just want the item to stay in view regardless of my scrolls (top, right, left or bottom).

    Many thanks
    Jean

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

    Default Re: How to draw QGraphicsItem at the same location?

    Try putting your slot(s) in your view. e.g.
    Qt Code:
    1. void pgraphicsview::vertical_scroll(int vPos){
    2. int hPos = horizontalScrollBar()->value();
    3. my_graphics_item->setPos(hPos, vPos);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Create another slot for horizontal scrolling. Using the passed parameter(s) should keep the item in the upper left corner. Adjust to suit.
    HTH

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

    jeanremi (25th August 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw QGraphicsItem at the same location?

    Quote Originally Posted by jeanremi View Post
    1/ when this slot is called, why is top always 0? (the view is scrolled down, but this is always returned as 0)
    Because it is the top of the viewport (the widget actually showing the graphics scene) and not the scene, thus the top is always 0.
    2/ when I use ival instead of top, item moves down in big steps.
    You don't want to use that.

    3/ how do I solve this issue? What am I doing wrong?
    What kind of item is that? Maybe it shouldn't be an item at all? Items are tied to the scene and not to the view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    jeanremi (25th August 2011)

  6. #4
    Join Date
    May 2011
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Post Re: How to draw QGraphicsItem at the same location?

    Good morning and thank you for your answers.

    To norobro:
    vertical_scroll is effectively called from graphicsview when its vertical (and horiz.) scroll bar's value changes. As you suggest, setPos(x,y) is probably what I need to call instead of moveBy(x, y);
    ( I will try this as soon as I get back from work ;-) )

    To Wysota:
    You have now spurred my curiosity about the graphicsview's viewport. If its top is always 0 when the view is scrolled up or down, does it mean that it is the scene which is moving ? (I was under the impression that the viewport should be the one moving)
    In other words, what happens to the viewport when a user scrolls down/up?

    The item is derived from QGraphicsItem as usual and it is added to scene as one would expect.
    class my_graphics_item: public QGraphicsItem
    ....
    my_graphics_item* test = new m_graphics_item(parent);
    pscene->addItem(test);


    (I will post my results later tonight)

    Thanks,
    Jean

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw QGraphicsItem at the same location?

    Quote Originally Posted by jeanremi View Post
    You have now spurred my curiosity about the graphicsview's viewport. If its top is always 0 when the view is scrolled up or down, does it mean that it is the scene which is moving ? (I was under the impression that the viewport should be the one moving)
    The view's projection matrix changes. You could say that it is the scene that moves (the same way the train station seems to move when you are sitting in the train and looking through the window).

    In other words, what happens to the viewport when a user scrolls down/up?
    Nothing. It only gets redrawn.

    The item is derived from QGraphicsItem as usual and it is added to scene as one would expect.
    I understand that. My point is that it should probably not be an item at all. If it is non-interactive, it's easiest to just subclass QGraphicsView, reimplement its paintEvent and simply paint on the viewport whatever you need painted after calling the base class implementation (so that graphics view paints the scene first).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #6
    Join Date
    May 2011
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Post Re: How to draw QGraphicsItem at the same location?

    The view's projection matrix changes.
    I see. This makes a lot of sense now. Ok point taken. ;-)

    If it is non-interactive, it's easiest to just subclass QGraphicsView, reimplement its paintEvent and simply paint on the viewport whatever you need painted after calling the base class implementation (so that graphics view paints the scene first).
    This is a very good suggestion, but the item will be an interactive one. A user will be able to click on it in order to relocate the view elsewhere. I should remember this suggestion for non-interactive ones.
    However why would this suggestion only be good for non-interactive items? what's the limitation?

    Many thanks,
    Jean

    PS: I haven't tested with setPos(x, y) yet. Still to come.

  9. #7
    Join Date
    May 2011
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: How to draw QGraphicsItem at the same location? [SOLVED]

    Hi,

    norobro:
    setPos(hPos, vPos); has effectively solved this issue very well.

    wysota:
    If I had more time I would try the qgraphicsview subclassing. I believe it would also work.

    Thank you all for your endeavouring inputs :-D

    Until then,
    Jean

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw QGraphicsItem at the same location?

    Quote Originally Posted by jeanremi View Post
    However why would this suggestion only be good for non-interactive items? what's the limitation?
    For interactive items you also need to reimplement other events There is an alternative though. Make your navigation thingy a widget and position it in the viewport. I guess that's the easiest and prettiest solution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #9
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw QGraphicsItem at the same location?

    Hi,

    I have the same problem. I have an Item that must stay always on top edge of an viewport. I want try whit a widget on my viewport but I have some problem.

    Qt Code:
    1. void CMCanvasManager::createMenu(QWidget *canvas_parent)
    2. {
    3. QWidget *w=new QWidget(canvas_parent, Qt::Popup | Qt::WindowStaysOnTopHint);
    4. w->setGeometry(100, 100, 100, 100);
    5. w->setMaximumSize(100,100);
    6. w->setMinimumSize(100,100);
    7. w->setFocusPolicy(Qt::NoFocus);
    8. w->show();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Whit the code above I expect that my popup widget stay at 100,100 respect the parent geometry, but this does not happen. The widget appear at 100,100 respect the desktop, olso if I click on viewport the popup disappear. Any advice?

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw QGraphicsItem at the same location?

    What is canvas_parent? And why are you setting those flags in the constructor?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw QGraphicsItem at the same location?

    canvas_parent is the widget that contain my custom QGraphicsView class, and I have set the flags in the costructo because I thought it was the best way to do it. I think that I have wrong all.
    I am proceeding to attempts because I did not quite clear how the widgets work. Sorry.. Councils do what I described? Thank you!

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw QGraphicsItem at the same location?

    Quote Originally Posted by Zikoel View Post
    canvas_parent is the widget that contain my custom QGraphicsView class
    So how is this supposed to work? Do you understand the relationship between a widget and its parent?
    I think that I have wrong all.
    I think so too

    I am proceeding to attempts because I did not quite clear how the widgets work. Sorry.. Councils do what I described? Thank you!
    Looking at my earlier explanation, I write that you should position the widget on the viewport. To do that the widget needs to be a child of the viewport, not of an arbitrary widget.

    If you have no idea about how things work, kindly please consider posting in the Newbie section and please be very explicit about what you are doing, it will save you a lot of time.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #13
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw QGraphicsItem at the same location?

    When I say "I am proceeding to attempts because I did not quite clear how the widgets work" I mean that I do not know the widget, but I work with Qt at least a year ;D.

    I have modified the code in this mode:

    Qt Code:
    1. void CMCanvas::createMenu(){
    2. m_menu_canvas=new QWidget();
    3. m_menu_canvas->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
    4. m_menu_canvas->setGeometry(100, 100, 100, 100);
    5. m_menu_canvas->setMaximumSize(100,100);
    6. m_menu_canvas->setMinimumSize(100,100);
    7. m_menu_canvas->show();
    8. }
    9. void CMCanvas::destroyMenu(){
    10. m_menu_canvas->close();
    11. }
    To copy to clipboard, switch view to plain text mode 

    The two metod is inside my custom class of QGraphicsView, but I do not know how to anchor the two windows.

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to draw QGraphicsItem at the same location?

    Please get familiar with the concept or parent-child relationship between widgets. And I don't mean Qt but rather the GUI architecture currently used on desktop systems. So far you are trying to stack two top-level windows on top of each other which is unlikely to do what you want. Of course unless you want something else than what this thread is about.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 10
    Last Post: 10th February 2011, 23:31
  2. Replies: 7
    Last Post: 29th November 2010, 19:20
  3. Bitmap Memory location to read and draw in Qt
    By augusbas in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2010, 21:21
  4. Replies: 0
    Last Post: 16th August 2009, 17:46
  5. Location of QGraphicItem!!
    By rachana in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2007, 21:20

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.