Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 537459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:55:45+00:00 2026-05-13T09:55:45+00:00

QT Version: 4.5.2 OS: ubuntu 8.04 I have a trouble with auto-resizing widget, here

  • 0

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()));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:55:45+00:00Added an answer on May 13, 2026 at 9:55 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.