[Qt-interest] QPushButton width problems / layout issues

Ramesh ramesh.bs at robosoftin.com
Wed Jun 23 14:07:17 CEST 2010


You have two option,
First:
Set the horizontal size policy to proffered for a button which you want to
keep fixed.
Make horizontal size policy to expanding for the button to be expand.

like this,

QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pushButton_2->setSizePolicy(sizePolicy);


Second:
You can use setgeometry() to set the position of a button.


-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Kustaa Nyholm
Sent: Wednesday, June 23, 2010 5:04 PM
To: QT Interest List
Subject: [Qt-interest] QPushButton width problems / layout issues

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);




_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


-----------------------------------------------
Robosoft Technologies - Come home to Technology

Disclaimer: This email may contain confidential material. If you were not an intended recipient, please notify the sender and delete all copies. Emails to and from our network may be logged and monitored. This email and its attachments are scanned for virus by our scanners and are believed to be safe. However, no warranty is given that this email is free of malicious content or virus.



More information about the Qt-interest-old mailing list