@rsp1984
Here is an example of a header code for a class based on a QWIdget. I'm copying and pasting only the parts of the code relvant to the question, from work, so please excuse me if it does not compile because I left something out:
The Header:
#include <QWidget>
#include <QGraphicsView>
#include <QDesktopWidget>
#include <QVBoxLayout>
#include <QApplication>
#include <QGraphicsScene>
class MonitorScreen : public QWidget
{
Q_OBJECT
public:
explicit MonitorScreen(QWidget *parent = nullptr, const QRect &screen = QRect(), qreal SCREEN_W = 1, qreal SCREEN_H = 1);
private:
QGraphicsView *gview;
};
The CPP:
#include "monitorscreen.h"
MonitorScreen::MonitorScreen(QWidget *parent, const QRect &screen, qreal SCREEN_W, qreal SCREEN_H) : QWidget(parent)
{
// Making this window frameless and making sure it stays on top.
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::X11BypassWindowManagerHint|Qt::Window);
this->setGeometry(screen);
// Creating a graphics widget and adding it to the layout
gview = new QGraphicsView(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->addWidget(gview);
gview->setScene(new QGraphicsScene(0,0,screen.width(),screen.height(),this));
gview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
gview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// White background
gview->scene()->setBackgroundBrush(QBrush(Qt::gray));
}
The idea here is that the scene is the same size as the rect of the QGraphcisView widget. That way something positioned at 100,100 will be a coordinate 100, 100 of in relation to the top left corner of the QGraphicsView. I do this because my scene will NEVER be larger than widget showing it. As a matter of fact they NEED to be the same size due to my particular problem.
Keep in mind that you CAN have a larger (or smaller) scene. If your scene is larger objects that are not withing the visible view will not be drawn, but will still exist in memory.
However I've always found that doing this, greatly facilitates how you start positioning and sizing your items in any given project, as you now know 100% sure where something at any given position should appear. You can then start coding more complex behaviour.
I hope this helps!