3 Attachment(s)
Facing problem in setting shrink behaviour of widgets in QGridLayout
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
Attachment 8960
This is max vertical shrink
Attachment 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.
Attachment 8962
Code:
CTimeSelect
::CTimeSelect(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.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
timeDisp.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
timeUp.setText("+");
timeDown.setText("-");
am_pm.setText("PM");
am_pm.setReadOnly(true);
am_pm.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
am_pm.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
am.setText("^");
pm.setText("v");
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.
Re: Facing problem in setting shrink behaviour of widgets in QGridLayout
- 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?
1 Attachment(s)
Re: Facing problem in setting shrink behaviour of widgets in QGridLayout
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.
Attachment 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?
Re: Facing problem in setting shrink behaviour of widgets in QGridLayout
Quote:
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
Code:
void CTimeSelect::onInit()
{
timeDisp.setText("11:30");
timeDisp.
setFont(QFont("Lucida Console",
24,
QFont::Bold,
false));
timeDisp.setReadOnly(true);
timeDisp.setMinimumWidth(100);
timeUp.setText("+");
timeUp.setMaximumWidth(25);
timeDown.setText("-");
timeDown.setMaximumWidth(25);
am_pm.setText("PM");
am_pm.setReadOnly(true);
am_pm.setMaximumWidth(25);
am.setText("^");
am.setMaximumWidth(25);
pm.setText("v");
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
Re: Facing problem in setting shrink behaviour of widgets in QGridLayout
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.