2016-07-22 25 views
0

我使用QGraphicsWidgetQGraphics[Linear]LayoutQGraphicsScene內部創建一個像「Widget」一樣的節點。 每個節點都有一個標題,多個IOGraphicsWidgets和一個頁腳。QGraphicsWidget和QGraphicsLayout間距和大小

代碼結構:

Code structure

的希望佈局:

Node layout

當前代碼的結果:

Current result

您可以看到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

回答

0

我找到了解決我的問題我自己。首先,我忘記了NodeGraphicsWidget的根佈局,但這並沒有解決整個問題。主要問題,物品之間的間距,並不是真正的問題。真正的問題是,每個QGraphicsLinearLayout默認都有保證金,AbstractIOGraphicsWidget根目錄有這些保證金。 layout->setContentsMargins(0,0,0,0)解決了這個問題。

相關問題