How to auto-resize qt widget?

Sibevin Wang picture Sibevin Wang · Jan 12, 2010 · Viewed 17.5k times · Source

QT Version: 4.5.2 OS: ubuntu 8.04

I have a trouble with auto-resizing widget, here is an example:

alt text http://img15.imageshack.us/img15/3479/autoresize.png

As the figure shows, here are two widgets, one is a "StrWidget"(the upper widget in the figure 1) which has 5 QLabels for showing strings, the other is a "CtrlWidget" which tells StrWidget what string to show.

When I click "send CCC", the StrWidget becomes the widget shown in figure 2

But when I click "send A" again, the StrWidget becomes the widget shown in figure 3

There is a member function StrWidget::changeString which is a slot of StrWidget and receive the string for showing.

void
StrWidget::changeString(QString inputStr)
{
    for(int i=0;i<5;i++){
        strEntries[i]->setText(inputStr);
    }
}

the StrWidget cannot auto-resize itself when changeString is called. I try the following two methods:

(1) Insert "adjustSize()" in StrWidget::changeString, it works but I need to click a button twice to make it resized. I don't know why it happens.

(2) Insert "hide();show();" in StrWidget::changeString, it works but the StrWidget would flush when I click buttons.

Dose anyone have an idea about it? Thanks~


The following is the source code of the example

main:

#include "StrWidget.h"
#include "CtrlWidget.h"
#include <QApplication>

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    StrWidget *strWidget=new StrWidget(0);
    CtrlWidget *ctrlWidget=new CtrlWidget(0);
    strWidget->show();
    ctrlWidget->show();
    QObject::connect(ctrlWidget,SIGNAL(sendString(QString)),strWidget,SLOT(changeString(QString)));
    int ref=app.exec();
    strWidget->deleteLater();
    ctrlWidget->deleteLater();
    return ref;
}

StrWidget.h

#include <QWidget>
#include <QString>
#include <QLabel>

#ifndef _StrWidget_H_
#define _StrWidget_H_

class StrWidget:public QWidget
{
    Q_OBJECT
    public:
        StrWidget(QWidget *parent=0);
    private:
        QLabel *strEntries[5];
    public slots:
        void changeString(QString inputStr);
        void clearString();
};

#endif

StrWidget.cpp

#include "StrWidget.h"
#include <QPushButton>
#include <QHBoxLayout>

StrWidget::StrWidget(QWidget *parent):QWidget(parent)
{
//main layout
    QHBoxLayout *mainLayout=new QHBoxLayout(this);
    setLayout(mainLayout);
    //info label setup
    for(int i=0;i<5;i++){
        strEntries[i]=new QLabel(this);
        strEntries[i]->setText("A");
        strEntries[i]->setFrameShape(QFrame::StyledPanel);
        mainLayout->addWidget(strEntries[i]);
    }
    //clearBtn setup
    QPushButton *clearBtn=new QPushButton(tr("Clear Str"),this);
    connect(clearBtn,SIGNAL(clicked()),this,SLOT(clearString()));
    mainLayout->addWidget(clearBtn);
}

void
StrWidget::changeString(QString inputStr)
{
    for(int i=0;i<5;i++){
        strEntries[i]->setText(inputStr);
    }
}

void
StrWidget::clearString()
{
    changeString("");
}

CtrlWidget.h

#include <QWidget>
#include <QString>

#ifndef _CtrlWidget_H_
#define _CtrlWidget_H_

class CtrlWidget:public QWidget
{
    Q_OBJECT
    public:
        CtrlWidget(QWidget *parent=0);
    signals:
        void sendString(QString sentStr);
};

CtrlWidget.cpp

#include "CtrlWidget.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QSignalMapper>

CtrlWidget::CtrlWidget(QWidget *parent):QWidget(parent)
{
    //main layout
    QHBoxLayout *mainLayout=new QHBoxLayout(this);
    setLayout(mainLayout);
    //control btns setup
    QPushButton *sendABtn=new QPushButton("send A",this);
    QPushButton *sendBBBtn=new QPushButton("send BB",this);
    QPushButton *sendCCCBtn=new QPushButton("send CCC",this);
    mainLayout->addWidget(sendABtn);
    mainLayout->addWidget(sendBBBtn);
    mainLayout->addWidget(sendCCCBtn);
    //map setup
    QSignalMapper *btnMapper=new QSignalMapper(this);
    btnMapper->setMapping(sendABtn,"A");
    btnMapper->setMapping(sendBBBtn,"BB");
    btnMapper->setMapping(sendCCCBtn,"CCC");
    connect(btnMapper,SIGNAL(mapped(QString)),this,SIGNAL(sendString(QString)));
    //connection setup
    connect(sendABtn,SIGNAL(clicked()),btnMapper,SLOT(map()));
    connect(sendBBBtn,SIGNAL(clicked()),btnMapper,SLOT(map()));
    connect(sendCCCBtn,SIGNAL(clicked()),btnMapper,SLOT(map()));
}

Answer

balpha picture balpha · Jan 12, 2010

You could try

mainLayout->activate();

in changeString(). That forces the layout to be redone. (You'll have to make mainLayout a class member though, or alternatively get the layout through layout()).

Also, try adding the controls with a stretch factor of zero:

mainLayout->addWidget(strEntries[i], 0);