1

I'm trying to implement a basic qtimer in a qgraphicsview but can't seem to get it working.

Here's my main.cpp code:

int main(int argc, char * argv[]) {


    QApplication app(argc, argv);//should stay on the stack

    QGraphicsScene * scene = new QGraphicsScene();//this is the scene -- the canvas that holds everything!

    // a view just adds stuff to the scene in that view port

    QGraphicsView * view = new Game(scene);//create a new view

    return app.exec();
}

And here is the view header ... note the qtimer and advance function

class View: public QGraphicsView {//inherits qwidget -- so we can make it full screen there

    Q_OBJECT

    public:
        View(QGraphicsScene * _scene);//this is responsible for setting up the screen and instantiating several elements
        ~View();

    protected://variables
        QGraphicsScene * scene;
        QTimer * timer;


    protected://functions

        int const int_height();//converts qreal to an int
        int const int_width();

        qreal const height();// 
        qreal const width();//this is the actual qreal floating point number 

        virtual void paintEvent(QPaintEvent * event) {};
        void timerEvent(QTimerEvent * event) {cout << "HELLO << ads" << endl;};
        virtual void keyPressEvent(QKeyEvent * event) {};
        virtual void update() = 0;


        void advance() { cout << "HELLO WORLD" << endl;}        
    private:
        qreal _height;
        qreal _width;

};

And finally, my view implementation constructor:

View::View(QGraphicsScene * _scene) : QGraphicsView(_scene) {


    scene = _scene;//set the scene which is the parent over this 


    this->showMaximized();

    QRectF geometry = this->contentsRect();

    _height = geometry.height();
    _width = geometry.width();

    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    scene->setSceneRect(0, 0, this->int_width(), this->int_height());
    // this->centerOn(qreal(0), qreal(0));

    this->setGeometry(0, 0, _width, _height);
    this->setFixedSize(_width, _height);


    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), scene, SLOT(advance()));
    timer->start(1000);
    timer->setInterval(100);



}   
1
  • What did you expect to happen? QGraphicsScene::advance() just tells all the items in it to 'advance()'.
    – cmannett85
    Oct 22, 2012 at 8:07

1 Answer 1

1

You need to declare your advance() function as a slot in the header file. Otherwise Qt does not know that this particular function is a slot:

protected slots:
    void advance() { cout << "HELLO WORLD" << endl; } 

Then, you are connecting the timeout signal to the advance() slot in the scene - but you declared it in your view. As you are currently in the view, you can use the this pointer to connect the signal to your view. Change your connect like this:

connect(timer, SIGNAL(timeout()), this, SLOT(advance())); 
//                                ^^^^

[Edit] As a side node: You are creating a QGraphicsView subclass of type Game, but you showed the source code of View. This might be irrelevant if Game inherits from View though.

1
  • Thanks for the help, and yes Game inherits from view, but I thought it would be easier to show just the view piece online :) Oct 22, 2012 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.