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

  • SEARCH
  • Home
  • 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 8968517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:27:56+00:00 2026-06-15T17:27:56+00:00

I’d like to have a QWidget instance that uses a QHBoxLayout and (ideally automatically,

  • 0

I’d like to have a QWidget instance that uses a QHBoxLayout and (ideally automatically, but upon calling some function is fine too) resizes horizontally to fit its contents. Horizontal resizing is enough since all widgets, including the container itself, will have the same height.

The use case is the following: The widget, let’s call it container, is floating, i.e. not part of any layout. A user should be able to add further widgets with a fixed size (by calling a function) to the container’s layout, upon which the container grows to fit its new contents. The user should also be able to remove a previously added widget, upon which the container shrinks again. The container will not be created until the first widget is inserted and deleted when the last one is removed, i.e. it will always contain at least one widget.

An example: after adding the first widget, the container looks like this, with | being its left/right borders:

|<1st widget>|

After adding another one, it looks like this:

|<1st widget>  <2nd, longer widget>|

After removing the first widget, it looks like this:

|<2nd, longer widget>|

I suspect that this should be kind of simple, but I got lost somewhere inbetween container’s sizeHint, sizePolicy, adjustSize(), and its layout’s sizeContraint as well as several attempts with explitely setting the size and forcing updates and essentially got nowhere (i.e. the container not resizing at all, the container only growing but not shrinking, etc). I suspect that I must have missed something kind of obvious..?

  • 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-06-15T17:27:56+00:00Added an answer on June 15, 2026 at 5:27 pm

    Your guess is right. One has to set size policies for widgets in a layout. First widget should have QSizePolicy::Minimum and second one should have QSizePolicy::Expanding to achieve a desired effect. Here is a sample application:

    #include <QtGui>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QPushButton *button1 = new QPushButton("Hello");
        button1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
        QObject::connect(button1, SIGNAL(clicked(bool)), button1, SLOT(hide()));
        QPushButton *button2 = new QPushButton("World");
        button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        QObject::connect(button2, SIGNAL(clicked(bool)), button2, SLOT(hide()));
    
        QHBoxLayout *layout = new QHBoxLayout();
        layout->addWidget(button1);
        layout->addWidget(button2);
    
        QWidget main;
        main.setLayout(layout);
        main.show();
    
        return a.exec();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
For some reason, after submitting a string like this Jack’s Spindle from a text
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I've got a string that has curly quotes in it. I'd like to replace
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.