Results 1 to 11 of 11

Thread: Christmas snow with Qt?

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Christmas snow with Qt?

    Hey,

    i want to let it sow with qt. The idea was to move three snowflakes (pictures) over a qwidget. Have anybody a tip for my how can i do that or know example that can do that?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Christmas snow with Qt?

    It depends what exactly you want to achieve. If you want to "snow" over an existing widget, you have to install an event filter, intercept its paint event, use QWidget::render() to render the widget to a pixmap, paint the pixmap on the widget and then paint your flakes. If you don't need to interact with this widget, you can simplify things by setting a child widget on the window that will cover the whole window below, make it transparent and paint flakes on it.

  3. The following user says thank you to wysota for this useful post:

    whitefurrows (28th November 2008)

  4. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Christmas snow with Qt?

    Hi,

    sorry my late answer, i don't have get a notification to your message. Now i have try to let it snow, but i cant paint a snowflake in my mainwindow. Can

    anybody take a look to my attachment and help my please.

    Thanks in advance
    Attached Files Attached Files

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Christmas snow with Qt?

    You need to do the painting inside a QWidget::paintEvent().
    Call QWidget::update() when your timer fires (this in turn will trigger the repaint).
    In paintEvent() paint the snowflakes according to your state (e.g. the time passed.)

    HTH

  6. The following user says thank you to caduel for this useful post:

    whitefurrows (28th November 2008)

  7. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Christmas snow with Qt?

    Hi,

    i have try like this:

    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    3. timer->start(1000);
    4.  
    5. void MainWindow::paintEvent ( QPaintEvent * )
    6. {
    7. QPixmap picture;
    8. qDebug() << picture.load(":flake_01.png"); // load picture (qDebug() == true)
    9.  
    10. QPainter painter;
    11. painter.begin(this); // paint in MainWindow
    12. painter.drawPixmap(150, 150, 100, 100, picture); // draw the picture at (150,150)
    13. painter.end(); // painting done
    14. }
    To copy to clipboard, switch view to plain text mode 

    but my picture was not paint. What is wrong?

  8. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Christmas snow with Qt?

    a) for efficiency: do not load the image every time paint event gets called
    b) add a drawLine and/or qDebug to make sure the function gets executed

  9. The following user says thank you to caduel for this useful post:

    whitefurrows (28th November 2008)

  10. #7
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Christmas snow with Qt?

    a) for efficiency: do not load the image every time paint event gets called
    OK, i can do that.

    b) add a drawLine and/or qDebug to make sure the function gets executed
    the function gets executed, but if i try this:

    Qt Code:
    1. QPainter painter;
    2. painter.begin(this);
    3. painter.setPen(Qt::black);
    4. painter.drawText(QRect(150, 150, 250, 250), Qt::TextWordWrap, tr("Let it snow"));
    5. painter.end();
    To copy to clipboard, switch view to plain text mode 

    i cant see a result. Can you help me, please?

  11. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Christmas snow with Qt?

    Theres something with QMainWindow...
    convert QMainWindow to QWidget and try,,,
    Qt Code:
    1. void MainWindow::paintEvent ( QPaintEvent * event)
    2. {
    3.  
    4. QPixmap picture;
    5. bool ret = picture.load(":flake_01.png"); // load picture (qDebug() == true)
    6. QPainter painter(this);
    7. painter.drawPixmap(0,0,QPixmap(":background.png") );
    8. painter.drawPixmap(110, 110, picture); // draw the picture at (150,150)
    9. }
    To copy to clipboard, switch view to plain text mode 

    i got it... though background is not covering whole area,,, but u can scale it anytime

    or use..
    painter.drawTiledPixmap(rect(),QPixmap(":backgroun d.png") ); instead of line 7

  12. The following user says thank you to aamer4yu for this useful post:

    whitefurrows (28th November 2008)

  13. #9
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Christmas snow with Qt?

    I have convert my QMainWindow to QWidget and all work's fine, but now i can't use my QMenuBar:

    Qt Code:
    1. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    2. QAction *exitAction = fileMenu->addAction(tr("E&xit"));
    3. exitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
    4. connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    What can i do?

  14. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Christmas snow with Qt?

    so u made a widget,, and its working... good..
    next step is to set this widget as central widget of QMainWindow

  15. The following user says thank you to aamer4yu for this useful post:

    whitefurrows (28th November 2008)

  16. #11
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Christmas snow with Qt?

    Thank you for your help, now it's all OK.

    But notice you must setAutoFillBackground(true) to display the backgound from the widget.

Similar Threads

  1. Merry Christmas and a Happy New Year 2008
    By vermarajeev in forum General Discussion
    Replies: 1
    Last Post: 24th December 2007, 20:40
  2. Replies: 1
    Last Post: 24th December 2007, 11:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.