简体   繁体   中英

How to make the background picture change with movement of the window in Qt/c++?

Is it possible to make the background (one picture) change with the movement of the window (move the coordinates of the SAME picture) in Qt?

I tried setting Stylesheets but it did not work.

This is what I want to achieve Img so for example, if I move the window a little to the left instead of having "age of window" I should get "ound image of window"

I hope you understand what I mean.

Okay, here my solution: By reimplementing the paintEvent and moveEvent (to repaint on movement) you can reach your goal.

Here is my reimplementation of QWidget::paintEvent :

void MainWindow::paintEvent(QPaintEvent *e) {
    QMainWindow::paintEvent(e);

    QPainter painter(this);

    auto window = pos();

    painter.drawPixmap(0, 0, _pixmapBg, window.x(), window.y(), 0, 0);
}

In the moveEvent we call repaint so paintEvent gets called:

void MainWindow::moveEvent(QMoveEvent *e) {
    repaint();

    QMainWindow::moveEvent(e);
}

Here the full code in a github repo .

I hope it helps even tought it's a bit laggy. And a happy new year!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM