I use QGraphicsWidget
and QGraphics[Linear]Layout
inside a QGraphicsScene
to create a Node like "Widget".
Each Node has a Header, multiple IOGraphicsWidgets
and a Footer.
The code Structure:
The wanted layout:
The result of the current code:
As you can see the NodeGraphicsWidget
(the red rectangle behind the HeaderWidget) is not resized to contain all items added to it. The spacing between the LayoutItems
is also huge and m_centerWidgetLayout->setSpacing(0)
changed nothing. Right now I'm thinking of writing all the layouts myself, but I hope there is a better way that I can use the standard qt.
NodeGraphicsWidget:addIOWidget(AbstractIOGraphicsWidget *ioWidget)
just adds the given AbstractIOGraphicsWidget
to the m_centerWidgetLayout
.
The Constructor of NodeGraphicsWidget
:
NodeGraphicsWidget::NodeGraphicsWidget(NodeGraphicsWidget::WidgetCreationFunction headerCreationFunc, NodeGraphicsWidget::WidgetCreationFunction footerCreationFunc, QGraphicsItem *parent, Qt::WindowFlags wFlags):
QGraphicsWidget(parent, wFlags)
{
m_headerWidget = new QGraphicsWidget(this);
m_centerWidget = new QGraphicsWidget(this);
m_centerWidgetLayout = new QGraphicsLinearLayout(Qt::Orientation::Vertical, m_centerWidget);
m_centerWidgetLayout->setSpacing(0);
m_centerWidget->setLayout(m_centerWidgetLayout);
m_footerWidget = new QGraphicsWidget(this);
headerCreationFunc(this, m_headerWidget);
if(footerCreationFunc != nullptr){
footerCreationFunc(this, m_footerWidget);
}
setAutoFillBackground(true);
QPalette pal;
pal.setColor(QPalette::Window, QColor(Qt::red));
this->setPalette(pal);
}
To see the full sourcecode visit: https://github.com/nidomiro/QtNodes/tree/f5426c154a4938481f00031f031507499cc0e183/src
I found the solution to my problem myself. First I forgot the root-layout of NodeGraphicsWidget
but that didn't fix the whole issue. The main issue, the spacing between the items, wasn't the real problem. The real problem was that every QGraphicsLinearLayout
has an margin by default and AbstractIOGraphicsWidget
root-layout had those margins. layout->setContentsMargins(0,0,0,0)
solved the issue.