Please refactor class below to act exactly like QSrollWidget from Qt3 but using Qt4 QScrollArea it must implement methods from https://neo.dmcs.pl/po/qt-tutorial/docs/qt/q3scrollview.html previous proposition pasted below is crashing : #ifndef KSCROLLVIEW_H
#define KSCROLLVIEW_H

#include <QFrame>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QResizeEvent>
#include <QPaintEvent>

class KScrollView : public QFrame
{
    Q_OBJECT

public:
    explicit KScrollView(QWidget* parent = nullptr);
    ~KScrollView();

    void setWidget(QWidget* widget);
    QWidget* widget() const;
    QWidget* viewport() const;

    void addChild(QWidget* child, int x = 0, int y = 0);
    void resizeContents(int width, int height);
    void updateScrollBars();
    void updateLayout(); // Public method to update layout
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

    QScrollBar* verticalScrollBar() const;
    QScrollBar* horizontalScrollBar() const;

    void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy);
    void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy);
    void setWidgetResizable(bool resizable);
    bool isWidgetResizable() const;

protected:
    void resizeEvent(QResizeEvent* event) override;
    void paintEvent(QPaintEvent* event) override;

private slots:
    void onHorizontalScroll(int value);
    void onVerticalScroll(int value);

private:
    void init(); // Initialization function

    QWidget* contentWidget; 
    QScrollBar* hScrollBar;
    QScrollBar* vScrollBar;
    QVBoxLayout* layout;
    bool widgetResizable; // Flag to check if the widget is resizable
};

#endif // KSCROLLVIEW_H   

#include "KScrollView.h"

KScrollView::KScrollView(QWidget* parent)
    : QFrame(parent), contentWidget(new QWidget(this)),
      hScrollBar(new QScrollBar(Qt::Horizontal, this)),
      vScrollBar(new QScrollBar(Qt::Vertical, this)),
      layout(new QVBoxLayout(this)), widgetResizable(false) // Default to non-resizable
{
    init();
}

KScrollView::~KScrollView()
{
}

void KScrollView::init()
{
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(contentWidget);
    layout->addWidget(vScrollBar);
    layout->addWidget(hScrollBar);

    setLayout(layout);

    // Connect scroll bars to their respective slots for content management
    connect(hScrollBar, SIGNAL(valueChanged(int)), this, SLOT(onHorizontalScroll));
    connect(vScrollBar, SIGNAL(valueChanged(int)), this, SLOT(onVerticalScroll));
}

void KScrollView::setWidget(QWidget* widget)
{
    // Clean up the old widget if exists
    if (contentWidget) {
        delete contentWidget;
    }

    contentWidget = widget;
    contentWidget->setParent(this); // Set the parent to manage lifetime
    layout->insertWidget(0, contentWidget); // Add to the layout
    updateScrollBars();
}

QWidget* KScrollView::widget() const
{
    return contentWidget; // Return current content widget
}

QWidget* KScrollView::viewport() const
{
    return contentWidget; // Content widget serves as the viewport
}

void KScrollView::addChild(QWidget* child, int x, int y)
{
    child->setParent(contentWidget);
    child->move(x, y); // Position based on provided x and y
    child->show(); // Show the child widget
    contentWidget->layout()->addWidget(child);
    updateScrollBars(); // Update scrollbars after adding child
}

void KScrollView::resizeContents(int width, int height)
{
    contentWidget->resize(width, height);
    updateScrollBars();
}

QSize KScrollView::sizeHint() const
{
    return QSize(400, 300); // Default size hint
}

QSize KScrollView::minimumSizeHint() const
{
    return QSize(100, 100); // Minimum size hint
}

QScrollBar* KScrollView::verticalScrollBar() const
{
    return vScrollBar; // Return vertical scrollbar
}

QScrollBar* KScrollView::horizontalScrollBar() const
{
    return hScrollBar; // Return horizontal scrollbar
}

void KScrollView::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
{
    hScrollBar->setVisible(policy == Qt::ScrollBarAlwaysOn || policy == Qt::ScrollBarAsNeeded);
}

void KScrollView::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
{
    vScrollBar->setVisible(policy == Qt::ScrollBarAlwaysOn || policy == Qt::ScrollBarAsNeeded);
}

void KScrollView::setWidgetResizable(bool resizable)
{
    widgetResizable = resizable;
    updateLayout(); // Update layout when widget resizable state changes
}

bool KScrollView::isWidgetResizable() const
{
    return widgetResizable; // Return the resizable state
}

void KScrollView::resizeEvent(QResizeEvent* event)
{
    QFrame::resizeEvent(event);
    updateScrollBars(); // Update scrollbars on resize
}

void KScrollView::paintEvent(QPaintEvent* event)
{
    QFrame::paintEvent(event);
    // Optional additional paint logic can go here
}

void KScrollView::onHorizontalScroll(int value)
{
    contentWidget->move(-value, contentWidget->y()); // Move content horizontally
}

void KScrollView::onVerticalScroll(int value)
{
    contentWidget->move(contentWidget->x(), -value); // Move content vertically
}

void KScrollView::updateScrollBars()
{
    int contentWidth = contentWidget->width();
    int contentHeight = contentWidget->height();
    int viewportWidth = width() - vScrollBar->width();
    int viewportHeight = height() - hScrollBar->height();

    hScrollBar->setRange(0, contentWidth - viewportWidth);
    vScrollBar->setRange(0, contentHeight - viewportHeight);
}

void KScrollView::updateLayout()
{
    if (widgetResizable) {
        // Adjust the content widget size if it is resizable
        contentWidget->setMinimumSize(sizeHint());
    }
    
    contentWidget->resize(size()); // Resize content widget to fit the KScrollView
    updateScrollBars(); // Update scrollbars after layout changes
}