Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QLabel consuming too much space

Tags:

qt

qt4

I use a QLabel and QPLineEdit within a QStackedWidget , the QLable should be nearly the size of the window holding this widget.

But when I set a extra long text to QLabel , it's expanding too much , and I'm not able to reduce the window size horizontally , the minimum width was too much.

I set the size policy of these three widgets to Minimum already , it just won't work for me.

UPDATE

maybe it's better saying like this: how to let QLabel display part of the text , when there's not enough space

SAMPLE CODE

  #include <QtGui>

int main ( int argc , char **argv ) 
{
    QApplication app (argc , argv);

    QWidget w;
    QLabel *label = new QLabel ("Very very very long text");
    label->setSizePolicy (QSizePolicy::Minimum , QSizePolicy::Fixed);
    QVBoxLayout layout (&w);
    layout.addWidget ( label );
    w.show();
    return  app.exec();
}
like image 644
daisy Avatar asked May 06 '12 12:05

daisy


1 Answers

If I understand you correctly, the simplest thing to do is simply to ignore that label's horizontal size hint.
As long as you have other widgets in there (or force a minimum width manually to the container), this should do what you want:

#include <QtGui>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QLabel *l1 = new QLabel("This very long text doesn't influence "
                            "the width of the parent widget");
    l1->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
    // Style just to make it clear that the widget is 
    // being resized to fit the parent, it doesn't "overflow"
    l1->setFrameShape(QFrame::Box);
    l1->setFrameShadow(QFrame::Raised);
    l1->setAlignment(Qt::AlignHCenter);

    QLabel *l2 = new QLabel("This influences the width");
    l2->setFrameShape(QFrame::Box);
    l2->setFrameShadow(QFrame::Raised);

    QWidget w;
    QVBoxLayout layout(&w);
    layout.addWidget(l1);
    layout.addWidget(l2);
    w.show();
    return app.exec();
}
like image 55
Mat Avatar answered Sep 21 '22 12:09

Mat