Getting a widget to slowly roll down the screen



  • Hi Everyone,

    I need to have a widget slowly roll down the screen (kind of like a screen saver). Additionally once it reaches the bottom of the screen, it need it to then slowly roll down from the top of the screen. This process should be repeated as long as the screen is idle.

    So far I have the following:

    file header.h
    @#ifndef HEADER_H
    #define HEADER_H

    #include <QtGui>

    class Widget : public QWidget
    {
    public:
    Widget(QWidget *parent = 0) : QWidget(parent)
    {
    new QPushButton("Widget", this);
    }
    };

    class Window : public QDialog
    {
    Q_OBJECT
    public:
    Window()
    {
    QPushButton *button = new QPushButton("Button", this);
    widget = new Widget(this);
    widget->move(20, 30);
    connect(button, SIGNAL(clicked()), this, SLOT(rollDownScreen()));
    }

    public slots:
    void rollDownScreen()
    {
    widget->scroll(0, 1);

    }
    

    private:
    Widget *widget;
    };

    #endif // HEADER_H
    @

    file main.cpp
    @#include "header.h"
    #include <QGraphicsScene>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Window w;
    w.show();
    return a.exec();
    }
    @

    The problem is that I need to keep pressing the button to keep the widget rolling down off the screen. The other problem is that I don't know how to get the widget to reappear and keep rolling from the top of the screen.

    So I thought to change the slot() function to do this as follows:
    @public slots:
    void rollDownScreen()
    {
    index =0;
    while(index < 20)
    {
    index++;
    widget->scroll(0, index);
    }
    }

    private:
    Widget *widget;
    int index;

    };@

    However, this just makes the widget jump out of the screen too fast.

    If anyone can help, I would be very grateful.
    PS. using Qt4.7.


  • Moderators

    Instead of loop or a button use "a timer":http://qt-project.org/doc/qt-4.8/qobject.html#startTimer with whatever interval you want.

    In the timerEvent set the widget position with move() (don't use scroll() for that).
    The position should be taken modulo ( % ) screen height. You can get it for example with "QDesktopWidget::screenGeometry":http://qt-project.org/doc/qt-4.8/qdesktopwidget.html#screenGeometry


  • Lifetime Qt Champion

    Hi,

    To add to Chris Kawa, you can also use a QPropertyAnimation.

    [edit: fixed link SGaist]



  • Wow, thank you - both of you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.