PDA

View Full Version : Christmas snow with Qt?



whitefurrows
13th November 2008, 19:06
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?

wysota
16th November 2008, 16:47
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.

whitefurrows
17th November 2008, 18:35
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

caduel
17th November 2008, 19:03
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

whitefurrows
18th November 2008, 09:44
Hi,

i have try like this:


QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);

void MainWindow::paintEvent ( QPaintEvent * )
{
QPixmap picture;
qDebug() << picture.load(":flake_01.png"); // load picture (qDebug() == true)

QPainter painter;
painter.begin(this); // paint in MainWindow
painter.drawPixmap(150, 150, 100, 100, picture); // draw the picture at (150,150)
painter.end(); // painting done
}

but my picture was not paint. What is wrong?

caduel
18th November 2008, 09:55
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

whitefurrows
18th November 2008, 11:32
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:


QPainter painter;
painter.begin(this);
painter.setPen(Qt::black);
painter.drawText(QRect(150, 150, 250, 250), Qt::TextWordWrap, tr("Let it snow"));
painter.end();

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

aamer4yu
18th November 2008, 12:17
Theres something with QMainWindow...
convert QMainWindow to QWidget and try,,,

void MainWindow::paintEvent ( QPaintEvent * event)
{

QPixmap picture;
bool ret = picture.load(":flake_01.png"); // load picture (qDebug() == true)
QPainter painter(this);
painter.drawPixmap(0,0,QPixmap(":background.png") );
painter.drawPixmap(110, 110, picture); // draw the picture at (150,150)
}

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

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

whitefurrows
18th November 2008, 16:09
I have convert my QMainWindow to QWidget and all work's fine, but now i can't use my QMenuBar:


QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QAction *exitAction = fileMenu->addAction(tr("E&xit"));
exitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

What can i do?

aamer4yu
18th November 2008, 19:34
so u made a widget,, and its working... good..
next step is to set this widget as central widget of QMainWindow :)

whitefurrows
28th November 2008, 14:57
Thank you for your help, now it's all OK.

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