Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting min/max width for widget within QHBoxLayout...
Forum Update on Monday, May 27th 2025

Setting min/max width for widget within QHBoxLayout...

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 14.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    datahead8888
    wrote on 8 Jun 2015, 05:01 last edited by Chris Kawa 6 Aug 2015, 05:55
    #1

    I was using a QHBoxLayout to maintain ratios for widgets horizontally. I was using a 90:4:6 ratio to maintain percentages during resizes of the window. This worked fine.

    However, I'd like to make the rightmost widget in this layout (named colorBar) have a maximum width. Thus it would usually follow the layout above but go ahead and stop expanding after a certain point. I would also be interested in setting a minimum width. If this is not possible, forcing this widget alone to be a fixed size may also be a good idea; it may even turn out to be the better option.

    I tried using both QSizePolicy and setMaximumWidth and cannot get it to work properly.
    The relevant (simplified) code is below. If anyone knows how to get this to work, it would be greatly appreciated.

    class TreeMapWindow : public QFrame
    {
        Q_OBJECT
            public:
                     TreeMapWindow();
    	private:
    		QHBoxLayout *mainLayout;
    		QScrollArea * scrollArea = nullptr;
    		QFrame *ltmPlot;
    		QWidget * placeholder = nullptr;
    		QFrame * colorBar = nullptr;
    };
    
    TreeMapWindow::TreeMapWindow()
    {
              //Instantiation
    	mainLayout = new QHBoxLayout;
    	ltmPlot = new QFrame();
    	scrollArea = new QScrollArea;
    	placeholder = new QWidget();
    	colorBar = new QFrame();
    
    	//Put the plot widget inside the scrollbar
    	ltmPlot->resize(800, 800);
    	scrollArea->setWidget(ltmPlot);
    	scrollArea->setFrameStyle(QFrame::NoFrame);
    
    	//Size policies for the 3 widgets contained within the layout	
    	QSizePolicy plotPolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    	plotPolicy.setHorizontalStretch(90);
    	scrollArea->setSizePolicy(plotPolicy);
    
    	QSizePolicy placeholderPolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    	placeholderPolicy.setHorizontalStretch(4);
    	placeholder->setSizePolicy(placeholderPolicy);
    
    	QSizePolicy colorBarPolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
    	colorBarPolicy.setHorizontalStretch(6);
    	colorBar->setSizePolicy(colorBarPolicy);
    	colorBar->setFrameStyle(QFrame::Box);
    	colorBar->setMaximumWidth(200);  //This does not work
    
    	//Set up the layout
    	mainLayout->addWidget(scrollArea);
    	mainLayout->addWidget(placeholder);
    	mainLayout->addWidget(colorBar);
    	setLayout(mainLayout);
    
    	//Palettes for quick color
    	QPalette myPalette(palette());
    	myPalette.setColor(QPalette::Background, QColor(255, 255, 255, 255));
    	this->setPalette(myPalette);
    	this->setAutoFillBackground(true);
    	placeholder->setPalette(myPalette);
    	placeholder->setAutoFillBackground(true);
    
    	QPalette Pal(palette());
    	Pal.setColor(QPalette::Background, Qt::magenta);
    	ltmPlot->setPalette(Pal);
    
    	QPalette Pal2(palette());
    	Pal2.setColor(QPalette::Background, Qt::red);
    	colorBar->setPalette(Pal2);
    	colorBar->setAutoFillBackground(true);
    }
    

    I would attach some screenshots, but I'm not sure how to attach them to this forum.

    [EDIT Chris Kawa: fixed code formatting]

    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 8 Jun 2015, 05:54 last edited by
      #2

      Using setMinimumWidth/setMaximumWidth or setFixedWidth gives me the expected result. Can you explain what "does not work" mean?

      The forum does not host images. Upload them somewhere and post with ![description](url "optional title").
      Also, please surround code with ``` (3 backticks) to format it correctly.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        datahead8888
        wrote on 8 Jun 2015, 23:02 last edited by
        #3

        It turns out my maximum size was too large. It actually does honor the minimum/maximum sizes now. Thank you for you help.

        1 Reply Last reply
        0

        1/3

        8 Jun 2015, 05:01

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved