【问题标题】:QGraphicsView / QGraphicsScene Timer Event not workingQGraphicsView / QGraphicsScene 定时器事件不工作
【发布时间】:2012-10-22 16:10:31
【问题描述】:

我正在尝试在 qgraphicsview 中实现一个基本的 qtimer,但似乎无法使其正常工作。

这是我的 main.cpp 代码:

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();
}

这里是视图标题...注意 qtimer 和提前功能

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;

};

最后,我的视图实现构造函数:

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);



}   

【问题讨论】:

  • 您预计会发生什么? QGraphicsScene::advance() 只是将其中的所有项目告诉 'advance()'。

标签: c++ qt user-interface qt4


【解决方案1】:

您需要将您的Advance() 函数声明为头文件中的一个槽。否则 Qt 不知道这个特定的函数是一个槽:

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

然后,您将超时信号连接到场景中的 Advance() 插槽 - 但您在视图中声明了它。当您当前在视图中时,您可以使用this 指针将信号连接到您的视图。像这样改变你的连接:

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

[编辑] 作为侧节点:您正在创建类型为Game 的 QGraphicsView 子类,但您显示了View 的源代码。如果Game 继承自View,这可能无关紧要。

【讨论】:

  • 感谢您的帮助,是的,游戏继承自视图,但我认为仅在线显示视图会更容易:)
猜你喜欢
  • 2017-02-16
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 2013-10-26
相关资源
最近更新 更多