简体   繁体   English

Qt/c++中如何让背景图片随着window的移动而变化?

[英]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?是否可以让Qt中的window(移动同一张图片的坐标)的移动使背景(一张图片)发生变化?

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"这就是我想要实现的Img ,例如,如果我将 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.好的,这是我的解决方案:通过重新实现paintEventmoveEvent (在移动时重新绘制),您可以达到您的目标。

Here is my reimplementation of QWidget::paintEvent :这是我对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:moveEvent我们调用repaint所以paintEvent被调用:

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

    QMainWindow::moveEvent(e);
}

Here the full code in a github repo .这里是 github repo中的完整代码。

I hope it helps even tought it's a bit laggy.我希望它能有所帮助,即使它有点滞后。 And a happy new year!和新年快乐!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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