QLabel size problems when adding the label to a HBoxLayout



  • Hi,

    I know this is pretty basic stuff...... It's a few hours of experimenting with some widgets and Layouts and reading the docs, yet I still can't find a solution for this.

    I simply want to add a label to a QHBoxLayout, but it always takes all the space available. I'd like it to take only the space it needs to display it's text.

    In this example, the Video Viewer node label, painted in gray takes too much place:
    !http://i41.tinypic.com/6f61xz.jpg(Nodes)!
    (yeah, ugly colors... so I can clearly see what's happening)

    Closest I got to what I want is by adding stretch, which causes all the nodes to be the same size (!!!, I don't get it), as shown below.
    !http://i40.tinypic.com/2414eq0.jpg!

    @NodeItem::NodeItem(int nodeType, QGraphicsItem parent/, Qt::WindowFlags wflags*/)
    : QGraphicsProxyWidget(parent)
    {
    this->m_instance = NodeBuilder::getInstance()->createNode(nodeType);

    setCacheMode(DeviceCoordinateCache);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    //setStyle(QStyleFactory::create("plastique"));
    setZValue(0);
    
    QFrame *m_nodeWidget = new QFrame();
    m_nodeWidget->setObjectName("NodeItem");
    
    QLabel *nodeHeader = new QLabel(tr(m_instance->getName().c_str()));
    nodeHeader->setObjectName("NodeHeaderLabel");
    nodeHeader->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
    nodeHeader->setAlignment(Qt::AlignLeft);
    
    QVBoxLayout *outputsLayout;
    QVBoxLayout *inputsLayout;
    
    QFrame *connectorsFrame = new QFrame();
    QFrame *leftFrame = new QFrame();
    QFrame *rightFrame = new QFrame();
    
    connectorsFrame->setObjectName("NodeMainFrame");
    leftFrame->setObjectName("NodeInputsFrame");
    rightFrame->setObjectName("NodeOutputsFrame");
    
    inputsLayout = new QVBoxLayout();
    leftFrame->setLayout(inputsLayout);
    outputsLayout = new QVBoxLayout();
    rightFrame->setLayout(outputsLayout);
    
    QHBoxLayout *connectorsLayout = new QHBoxLayout();
    connectorsLayout->addWidget(leftFrame);
    connectorsLayout->addWidget(rightFrame);
    connectorsFrame->setLayout(connectorsLayout);
    
    inputsLayout->setMargin(0);
    outputsLayout->setMargin(0);
    connectorsLayout->setMargin(0);
    
    QHBoxLayout *headerLayout = new QHBoxLayout();
    headerLayout->addWidget(nodeHeader);
    //headerLayout->addStretch();
    
    QVBoxLayout *nodeLayout = new QVBoxLayout();
    nodeLayout->addLayout(headerLayout);
    nodeLayout->addWidget(connectorsFrame);
    nodeLayout->setMargin(0);
    
    m_nodeWidget->setLayout(nodeLayout);
    setWidget(m_nodeWidget);
    
    /* Add input ports */
    input_map_t ins = this->m_instance->inputs();
    for (input_map_t::iterator it = ins.begin(); it!=ins.end(); ++it) {
        addConnector(inputsLayout, QString::fromStdString(it->first), NodeConnectorItem::Input);
    }
    
    /* Add output ports */
    output_map_t outs = this->m_instance->outputs();
    for (output_map_t::iterator it = outs.begin(); it!=outs.end(); ++it) {
        addConnector(outputsLayout, QString::fromStdString(it->first), NodeConnectorItem::Output);
    }
    
    if (this->m_instance)
        this->setToolTip(QString::fromStdString(m_instance->getDescription()));
    
    setEdited(false);
    

    }
    @

    The solution is probably really trivial and has probably been right in my face the whole time, but if you have any inputs about what I could do to fix this, I'd appreciate!



  • Hi, The QHBoxLayout and all other layouts resize the included widgets automatic. That's the trivial piece of it. If you want the QLabel to only be as large as is needed you should add the QLabel and a HorizontalSpacer in a Layout of there own and add that layout to the QHBoxLayout you already have now. The HorizontalSpacer will take any "empty" space on the GUI widget that is not needed.
    Greetz



  • Suggestion: Try to do it in Designer with just a couple of simple widgets to get an idea of what a spacer does.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.