PDA

View Full Version : QGraphicsScene clearing itself....



budda
21st June 2014, 21:49
I've got a QGraphicsView with a QGraphicsScene. In Qt5.2.0 MinGW
And I'm adding custom GraphicsViewItems to said scene.
This works just fine. But after awhile the scene clears itself.
is this normal? is there an automatic clear scene after so long?



void MainWindow::drawThisView()
{
thisView->setSceneRect(0,0,690,242);
thisView->setCacheMode(QGraphicsView::CacheBackground);
thisView->setViewportUpdateMode(QGraphicsView::BoundingRectV iewportUpdate);
thisScene.setItemIndexMethod(QGraphicsScene::NoInd ex);
thisView->setFrameStyle(0);
thisView->setAlignment(Qt::AlignLeft);
thisView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
thisView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
thisView->setStyleSheet(QString(" background-color: transparent; border-color: transparent; background: url(:/pics/thisSceneBkgr.png);"));
thisScene.setSceneRect(0,0,690,242);

thisScene.clear();



CustGrItem *tempDraw = new CustGrItem(225, 0, -1, -1); //sets the 225 item, set color to black, set x, set y
thisScene.addItem(tempDraw);
tempDraw->setPos(100,32);

thisView->setScene(& thisScene);
thisView->show();

}

budda
22nd June 2014, 01:29
I am running a timerEvent method as well, so I can make a refreshScene function and call it from there. But knowing the length before the scene clears itself would be a huge help. I changed a view method to ->setViewportUpdateMode(QGraphicsView::FullViewportU pdate); but it still clears itself....

budda
23rd June 2014, 21:03
anyone have a comment on this?

d_stranz
24th June 2014, 02:06
QGraphicsScene instances don't "clear themselves". Your code has to tell them to do it, like you're doing in line 14 of the code you posted. Other than that, you've posted 25 lines of code with absolutely no other information on how that code is used, where it's called from, or even if it is called at all during the execution of your program. We have to assume that "thisView" and "thisScene" are probably member variables of your "MainWindow" class, but other than that, trying to answer your question is like trying to answer "How long is a piece of string?"

budda
24th June 2014, 02:51
mainwindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimerEvent>
#include <QTimer>
#include <QAction>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QIcon>
#include <QPixmap>
#include <QImage>
#include <QLabel>
#include <QPushButton>
#include <QStackedWidget>
#include <QFrame>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QGraphicsScene>

#include "custgritem.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

QGraphicsScene thisScene;

protected:
void timerEvent(QTimerEvent *e);

private:
Ui::MainWindow *ui;

void drawThisView();
QGraphicsView *thisView;



mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QShortcut>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPainter>
#include <QColor>
#include <cstdlib>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setMaximumSize(708,684);

thisView = new QGraphicsView(this);

QVBoxLayout *thisL = new QVBoxLayout(this);
thisL->addSpacing(100);
thisL->addWidget(thisView);
thisL->addSpacing(100);
ui->centralWidget->setLayout(thisL);

drawThisView();
}




well of course they are members..... the -> or . implies tha

- I tried calling drawThisView() from a paintEvent from MainWindow that fixes the clearing, but suddenly one of my CPU cores is bumped up to 75% when all 8 sit at only 11% without the paintEvent. (other things are running too, just the one thread goes to 75% when the apps paintEvent calls drawThisView...)

- the sample code above is just a bit of the whole app. the QVBoxLayout has ontop a QPushButton, then the QGraphicsView, & then a QStackedWidget (with a bunch of stacked widgets) I just added the drawThisView() to when I change the index of the widget stack and it kinda fixes it, as it refreshes once someone starts flipping through the QStackedWidget... and it doesn't hog CPU time...