我使用QGraphicsWidget
和QGraphics[Linear]Layout
在QGraphicsScene
內部創建一個像「Widget」一樣的節點。 每個節點都有一個標題,多個IOGraphicsWidgets
和一個頁腳。QGraphicsWidget和QGraphicsLayout間距和大小
代碼結構:
的希望佈局:
當前代碼的結果:
您可以看到NodeGraphicsWidget
(HeaderWidget後面的紅色矩形)沒有調整大小以包含添加到它的所有項目。 LayoutItems
之間的間距也很大,m_centerWidgetLayout->setSpacing(0)
沒有改變。現在我正在考慮自己編寫所有的佈局,但我希望有更好的方式可以使用標準的qt。
NodeGraphicsWidget:addIOWidget(AbstractIOGraphicsWidget *ioWidget)
只是將給定AbstractIOGraphicsWidget
添加到m_centerWidgetLayout
。
的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);
}
要查看完整的源代碼,請訪問:https://github.com/nidomiro/QtNodes/tree/f5426c154a4938481f00031f031507499cc0e183/src