QCheckBox is too high compared to QLabel.
-
Allo,
I am on Kubuntu 20.10.
I have Qt Creator 4.14.2, it says based on Qt 5.15.2 (GCC 7.3.1 20180303 (Red Hat 7.3.1-5), 64 bit)I have a QCheckBox and a QLabel. They are both suppose to be on the same “line” if you know what I mean.
Imagine that I draw a horizontal line on my window.
I want to place a LABEL on the line and a little to right, I will place a CHECKBOX (with its own text).I use setGeometry for the QCheckBox and the QLabel to set their position and sizes.
I also have a function that compares their height. If the height of QCheckBox is too little, it pushes it down.
For example,
If HeightOfCheckBox = 15 pixels
If HeightOfLabel = 20 pixels
Push down CheckBox by 5 pixels.So, in principle, they should be at the same level.
I attached an image of what I get. -
Hi,
Why not use a QHBoxLayout ? It does what you want automatically.
-
Hi,
Why not use a QHBoxLayout ? It does what you want automatically.
@SGaist Ok, I tried it in there. It doesn’t show anything.
layout=new QHBoxLayout(this); layout->addWidget(CHECKWordInserted); layout->addWidget(STATICWordInserted); layout->addWidget(EDITWordToInsert); QRect rect; rect.setX(CHECKWordInsertedPosSize[0]); rect.setY(CHECKWordInsertedPosSize[1]); rect.setWidth(CHECKWordInsertedPosSize[2]); rect.setHeight(CHECKWordInsertedPosSize[3]); layout->setGeometry(rect);
Do I have to call setGeometry for CHECKWordInserted (which is a QCheckBox) and for STATICWordInserted and for EDITWordToInsert.
Is the coordinate relative to the QHBoxLayout? -
@SGaist Ok, I tried it in there. It doesn’t show anything.
layout=new QHBoxLayout(this); layout->addWidget(CHECKWordInserted); layout->addWidget(STATICWordInserted); layout->addWidget(EDITWordToInsert); QRect rect; rect.setX(CHECKWordInsertedPosSize[0]); rect.setY(CHECKWordInsertedPosSize[1]); rect.setWidth(CHECKWordInsertedPosSize[2]); rect.setHeight(CHECKWordInsertedPosSize[3]); layout->setGeometry(rect);
Do I have to call setGeometry for CHECKWordInserted (which is a QCheckBox) and for STATICWordInserted and for EDITWordToInsert.
Is the coordinate relative to the QHBoxLayout?@stretchthebits said in QCheckBox is too high compared to QLabel.:
. It doesn’t show anything.
Did you forgot to add your layout to your mainLayout or set it as mainLayout if your widget had no layout already?
No you usually dont need
setGeometry
when using layouts. -
@SGaist Ok, I tried it in there. It doesn’t show anything.
layout=new QHBoxLayout(this); layout->addWidget(CHECKWordInserted); layout->addWidget(STATICWordInserted); layout->addWidget(EDITWordToInsert); QRect rect; rect.setX(CHECKWordInsertedPosSize[0]); rect.setY(CHECKWordInsertedPosSize[1]); rect.setWidth(CHECKWordInsertedPosSize[2]); rect.setHeight(CHECKWordInsertedPosSize[3]); layout->setGeometry(rect);
Do I have to call setGeometry for CHECKWordInserted (which is a QCheckBox) and for STATICWordInserted and for EDITWordToInsert.
Is the coordinate relative to the QHBoxLayout?Maybe this will help
`
widgetInWhichYouWantLayout->setLayout(layout);// mainWidget->setLayout(layout);
` -
@stretchthebits said in QCheckBox is too high compared to QLabel.:
. It doesn’t show anything.
Did you forgot to add your layout to your mainLayout or set it as mainLayout if your widget had no layout already?
No you usually dont need
setGeometry
when using layouts.@Pl45m4 Well, how else is it going to know that I want this layout thing to be positioned below the textbox (QLabel) "Copy character count (min 1 and max 3)"?
I am not using layouts everywhere. I do all my positioning myself except there is this problem with the level of the checkbox text.This time, I added
this->setLayout(layout);
but again, I don't see it. -
@stretchthebits said in QCheckBox is too high compared to QLabel.:
layout=new QHBoxLayout(this);
When you pass a parent to a layout, it is automatically applied to that parent.
Positioning everything yourself is pretty rarely done. Properly using layouts, in your case, a combination of QVBoxLayout and QHBoxLayout or a QGridLayout will make things easier to maintain especially with screens of different resolutions.
-
@Pl45m4 Well, how else is it going to know that I want this layout thing to be positioned below the textbox (QLabel) "Copy character count (min 1 and max 3)"?
I am not using layouts everywhere. I do all my positioning myself except there is this problem with the level of the checkbox text.This time, I added
this->setLayout(layout);
but again, I don't see it.@stretchthebits said in QCheckBox is too high compared to QLabel.:
except there is this problem with the level of the checkbox text.
A problem you wouldn't have when you use layouts everywhere :)
HBox and VBox Layouts will make your widgets allign properly in rows and columns. -
@stretchthebits said in QCheckBox is too high compared to QLabel.:
except there is this problem with the level of the checkbox text.
A problem you wouldn't have when you use layouts everywhere :)
HBox and VBox Layouts will make your widgets allign properly in rows and columns.@Pl45m4 OK I tried it in a test program and looks like it is able to align the text of a button and QCheckBox and a QLabel.
But how am I suppose to have control on the size of buttons and EDITBOXes? -
Sounds like a QGridLayout.
Can you show a mock-up of what you want to achieve ? It would help us help you.