【发布时间】:2016-11-26 12:43:45
【问题描述】:
我在QGraphicsScene 中使用QGraphicsWidget 和QGraphics[Linear]Layout 来创建类似“Widget”的节点。
每个Node都有一个Header,多个IOGraphicsWidgets和一个Footer。
代码结构:
想要的布局:
当前代码的结果:
如您所见,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
【问题讨论】:
标签: c++ qt layout qgraphicswidget