@Joe-von-Habsburg My example recast with a changing background:
// widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    // QWidget interface
protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);
private slots:
    void setBackground();
private:
    QImage mBackground;
    QPointF mFrom;
    QPointF mTo;
};
#endif // WIDGET_H
// widget.cpp
#include "widget.h"
#include <QPaintEvent>
#include <QPainter>
#include <QLinearGradient>
#include <QRandomGenerator>
#include <QTimer>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , mBackground(500, 500, QImage::Format_RGB32)
{
    setBackground();
    QTimer *t = new QTimer(this);
    connect(t, &QTimer::timeout, this, &Widget::setBackground);
    t->start(1000);
}
Widget::~Widget()
{
}
void Widget::mousePressEvent(QMouseEvent *event)
{
    mFrom = event->position();
    mTo = mFrom;
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    mTo = event->position();
    update();
}
void Widget::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    p.drawImage(event->rect(), mBackground);
    if (mFrom != mTo) {
        QPen pen(Qt::red);
        pen.setWidth(3);
        p.setPen(pen);
        p.drawLine(mFrom, mTo);
    }
    p.end();
}
void Widget::setBackground()
{
    QPainter p(&mBackground);
    const QRectF rectf(mBackground.rect());
    QLinearGradient grad(rectf.topLeft(), rectf.bottomRight());
    grad.setColorAt(0,  QColor::fromRgb(QRandomGenerator::global()->generate()));
    grad.setColorAt(1,  QColor::fromRgb(QRandomGenerator::global()->generate()));
    p.fillRect(mBackground.rect(), QBrush(grad));
    p.end();
    update();
}