PDA

View Full Version : Facing problem in setting shrink behaviour of widgets in QGridLayout



rawfool
18th April 2013, 09:16
Hi,

I'm facing difficulties in setting QSizePolicy to my widgets in QGridLayout.
I'm creating a custom time-selection widget, that has 2 QTextEdit, 4 buttons. My problem is the QTextEdit widgets and QPushButtons are not shrinking as desired.
PFB, the screenshot of the Timeselection Widget that I'm trying to develop -

This is max horizontal shrink
8960

This is max vertical shrink
8961

Desired behaviour -
I need the horizontal shrink to be limited to text in QTextEdit without wrapping text(displaying in next line). And I need the vertical size to shrink upto row-end of the text.

Here the buttons are also not shrinking beyond the one I showed in 1st image. I need the buttons to be as small as possible.

As shown in the pic below, I need the exact idea to be translated to my widgets behavior.
8962


CTimeSelect::CTimeSelect(QWidget *parent)
: QWidget(parent)
{
this->setLayout(&lyt);
this->setContentsMargins(0, 0, 0, 0);
lyt.setSpacing(0);
lyt.setContentsMargins(0, 0, 0, 0);
onInit();
}

CTimeSelect::~CTimeSelect()
{
}

void CTimeSelect::onInit()
{
timeDisp.setText("11:30");

timeDisp.setFont(QFont("Lucida Console", 24, QFont::Bold, false));
timeDisp.setReadOnly(true);
timeDisp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
timeDisp.setHorizontalScrollBarPolicy(Qt::ScrollBa rAlwaysOff);
timeDisp.setVerticalScrollBarPolicy(Qt::ScrollBarA lwaysOff);

timeUp.setText("+");
timeUp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
timeDown.setText("-");
timeDown.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

am_pm.setText("PM");
am_pm.setReadOnly(true);
am_pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
am_pm.setHorizontalScrollBarPolicy(Qt::ScrollBarAl waysOff);
am_pm.setVerticalScrollBarPolicy(Qt::ScrollBarAlwa ysOff);

am.setText("^");
am.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pm.setText("v");
pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

lyt.addWidget(&timeDisp, 0, 0, 2, 4);
lyt.addWidget(&timeUp, 0, 4, 1, 1);
lyt.addWidget(&timeDown, 1, 4, 1, 1);
lyt.addWidget(&am_pm, 0, 5, 2, 2);
lyt.addWidget(&am, 0, 7, 1, 1);
lyt.addWidget(&pm, 1, 7, 1, 1);

lyt.setColumnStretch(0, 1);
lyt.setColumnStretch(4, 0);
lyt.setColumnStretch(5, 1);
lyt.setColumnStretch(7, 0);

}

Please help me solve this issue. Thank you.

Santosh Reddy
18th April 2013, 10:48
- Use QLineEdit instead of QTextEdit, to avoid multipleline and text wrapping
- Set minimum size of all the widgets as required.

BTW do you want only to limit the minimum size, or you also want the widgets to be always at it's minimum size and never expand?

rawfool
18th April 2013, 11:05
I want the widgets to limit to minimum size. It should expand or shrink according to it's parent behavior.

I've used QTextEdit, now it's shrinking beyond it's minimum font size used in the display.
8964

Though I set 1 column & 1 row span for buttons, it's not square shaped. How do I make it square-shaped using QGridLayout and without setting size?

Santosh Reddy
18th April 2013, 11:36
Though I set 1 column & 1 row span for buttons, it's not square shaped. How do I make it square-shaped using QGridLayout and without setting size?
There are two ways
1. Set the size policy as Fixed, (widget will never change there size ever, not even if parent expands)
2. Set the size policy as Expanding, and set the minimum size limit. (this way widgets can take any size between the minimum limit size to largest possible)

Try this



void CTimeSelect::onInit()
{
timeDisp.setText("11:30");

timeDisp.setFont(QFont("Lucida Console", 24, QFont::Bold, false));
timeDisp.setReadOnly(true);
timeDisp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
timeDisp.setMinimumWidth(100);

timeUp.setText("+");
timeUp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
timeUp.setMaximumWidth(25);

timeDown.setText("-");
timeDown.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
timeDown.setMaximumWidth(25);

am_pm.setText("PM");
am_pm.setReadOnly(true);
am_pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
am_pm.setMaximumWidth(25);

am.setText("^");
am.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
am.setMaximumWidth(25);
pm.setText("v");
pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pm.setMaximumWidth(25);

lyt.addWidget(&timeDisp, 0, 0, 2, 1);
lyt.addWidget(&timeUp, 0, 1, 1, 1);
lyt.addWidget(&timeDown, 1, 1, 1, 1);
lyt.addWidget(&am_pm, 0, 2, 2, 1);
lyt.addWidget(&am, 0, 3, 1, 1);
lyt.addWidget(&pm, 1, 3, 1, 1);
}


I repeat use QLineEdit

rawfool
18th April 2013, 12:30
Sorry I used QLineEdit after your first reply. In my reply it was a typo.
And before you posted the code, I did the same way like yours. Now its looking the way I wanted.
Thank you very much.