[Qt-interest] QPushButton width problems / layout issues

Kustaa Nyholm Kustaa.Nyholm at planmeca.com
Wed Jun 23 13:34:22 CEST 2010


Hi,

I've got an UI where one of the elements is a kind of menubar which
is simply a QWidget with QHBoxLayout and a series of buttons in a row.

My problem is that all the buttons have equal widths and I would like them
to occupy the minimum space needed for the button text.

I've spent two days on this and thrown everything I've been able to googleup
at it, but no matter what, all the buttons have the same width.

In desperation I used QtDesigner to create the code for a simple 'menubar'
with two buttons with labels 'A' and 'LONG'. In QtDesigner
this previews correctly, ie the 'A' button is considerably narrower than
the 'LONG' button. I did not try the generated code but I presume it would
work as such by itself. I then copied the Designer generated code  (see
below) verbatim to my application and in that context (meaning in my widget
hierarchy) the buttons are again of the same width!

I think this has to be something/some property/setting in the widget/layout
hierarchy outside this 'menubar' code that is causing this behavior but I'm
at loss on how to proceed.

All suggestions welcome.

br Kusti







    QHBoxLayout *horizontalLayout;
    QPushButton *pushButton;
    QSpacerItem *horizontalSpacer;
    QPushButton *pushButton_2;

    horizontalLayout = new QHBoxLayout(this);
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    pushButton = new QPushButton("A", this);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));

    horizontalLayout->addWidget(pushButton);

    horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding,
QSizePolicy::Minimum);

    horizontalLayout->addItem(horizontalSpacer);

    pushButton_2 = new QPushButton("LONG", this);
    pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));

    horizontalLayout->addWidget(pushButton_2);







More information about the Qt-interest-old mailing list