Results 1 to 4 of 4

Thread: How do I use the zValue to display items on the scene?

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How do I use the zValue to display items on the scene?

    Hi guys, I have four QGraphicsItems that are stacked one on top of the other using the zValue. Each QGraphicsItem is the same size as the scene rect, so I can only display one item at a time. These QGraphicsItems are basically a rectangle with a white background for me to paint over. Each item is hidden using the hide() function except the one with the highest zValue. My problem is that I want to be able to display each of these pages at the click of a button by changing the zValue of the QGraphicsItem that I want to display to a high zValue and then showing the item using the show() function but my code below crashes my program.
    Qt Code:
    1. void MainWindow::on_nextBtn_clicked()
    2. {
    3. if(pageIterator <= 3){
    4. pagesList.at(pageIterator)->hide();
    5. pageIterator++;
    6. pagesList.at(pageIterator)->show();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I loaded all the pages (i.e. the QGraphicsItems) into a QList (i.e.
    Qt Code:
    1. pagesList
    To copy to clipboard, switch view to plain text mode 
    ) and the page at
    Qt Code:
    1. pagesList.at(0)
    To copy to clipboard, switch view to plain text mode 
    is the first page that loaded on program start-up. The variable
    Qt Code:
    1. pageIterator
    To copy to clipboard, switch view to plain text mode 
    is a static variable that is initialized to zero and its sole purpose is to keep track of which page is displayed on screen. You will notice that nowhere in my code am I changing the zValue, its because I tried playing around with it and it also crashed my program. Can somebody suggest something, I am bit stuck here.

  2. #2
    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 do I use the zValue to display items on the scene?

    You don't need to modify zValue. Just hide() or show() items. Your code will crash because in line #5 of the first snippet you increase the index to 4 and then you try to read from this list index while your list only has 4 and not 5 items. To be honest your code is completely impractical.
    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.


  3. #3
    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: How do I use the zValue to display items on the scene?

    Quote Originally Posted by ayanda83 View Post
    Hi guys, I have four QGraphicsItems that are stacked one on top of the other using the zValue. Each QGraphicsItem is the same size as the scene rect, so I can only display one item at a time.
    Why bother with z value if only one if visible at any given time?

    Quote Originally Posted by ayanda83 View Post
    Qt Code:
    1. void MainWindow::on_nextBtn_clicked()
    2. {
    3. if(pageIterator <= 3){
    4. pagesList.at(pageIterator)->hide();
    5. pageIterator++;
    6. pagesList.at(pageIterator)->show();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    You said you had four pages. This code is allowing access to 5 entries in pagesList.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do I use the zValue to display items on the scene?

    I have four QGraphicsItems that are stacked one on top of the other using the zValue.
    You know, if you would simply listen to my suggestion that you put the 4 pages side-by-side in the scene and set the view's sceneRect to show only one of them at a time, you wouldn't have to go through all this show() / hide() idiocy. I guess you are just too stubborn and insist on doing things your way no matter how difficult and awkward it makes your code or how much time you waste in doing it and asking for help here when it doesn't work.

Similar Threads

  1. copy and paste items in a scene
    By gaurav6005 in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2011, 12:09
  2. Replies: 7
    Last Post: 26th June 2009, 19:33
  3. add items into scene
    By Noxxik in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2009, 16:32
  4. Can't move Items in the scene
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2008, 09:40
  5. How to update scene after removing items
    By nileshsince1980 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2007, 09:56

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.