Setting min/max width for widget within QHBoxLayout...
-
wrote on 8 Jun 2015, 05:01 last edited by Chris Kawa 6 Aug 2015, 05:55
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]
-
Using
setMinimumWidth
/setMaximumWidth
orsetFixedWidth
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

.
Also, please surround code with```
(3 backticks) to format it correctly. -
wrote on 8 Jun 2015, 23:02 last edited by
It turns out my maximum size was too large. It actually does honor the minimum/maximum sizes now. Thank you for you help.
1/3