PDA

View Full Version : How do I use the zValue to display items on the scene?



ayanda83
11th April 2015, 06:10
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.
void MainWindow::on_nextBtn_clicked()
{
if(pageIterator <= 3){
pagesList.at(pageIterator)->hide();
pageIterator++;
pagesList.at(pageIterator)->show();
}
}

I loaded all the pages (i.e. the QGraphicsItems) into a QList (i.e.
pagesList) and the page at
pagesList.at(0) is the first page that loaded on program start-up. The variable
pageIterator 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.

wysota
11th April 2015, 07:45
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.

anda_skoa
11th April 2015, 07:58
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?




void MainWindow::on_nextBtn_clicked()
{
if(pageIterator <= 3){
pagesList.at(pageIterator)->hide();
pageIterator++;
pagesList.at(pageIterator)->show();
}
}

You said you had four pages. This code is allowing access to 5 entries in pagesList.

Cheers,
_

d_stranz
12th April 2015, 00:53
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.