Problem removing widgets from a layout
-
Why is it necessary to set a certain button, or other widgets to not visible (using setVisible(false)) when removing from a layout (using layout.removeWidget(widget) )? Shouldn't layout.removeWidget(widget) be enough?
Thank you. -
Quote from docs.
bq. After this call, it is the caller's responsibility to give the widget a reasonable geometry.
After a widget is removed from layout, it remains "alive".
-
no, it is not.
If you remove it from the layout, you just remove it from automatic layouts.
Could be you want to add it to another layout.
If you just call setVisible(false) it will also be hidden and the layout will manage everything else.
Why do you remove it from layout? -
Ok ... but it sucks because if I have to use customWidget.setVisible(false) and then, when I want to reuse them I set this property to true I am faced with another problem:
Basically I have 3 widgets: 1 QLabel, 1 QLineEdit and 1 QPushButton, and when I make them visible again (when I want to reuse them), the QLineEdit looses focus.I want to delete them because I use them as a mode of interaction with the user, thus when I take the input I need them do disappear.
-
If you don't want to use them anymore, delete them with
@
delete pMyEdit;
@That should definitely remove them also from the layout. But it sounds weird to delete the elements and create them new each time...
-
I tried that, but when recreated they are resized awkwardly, although the first time they look ok.
-
I too think that deleting them and create them again and again is not ok.
I don't understand why the QLineEdit looses focus (i cannot input text anymore).@
mapsLayout->addWidget(checkpointQuestion);
mapsLayout->addWidget(userAnswer);
mapsLayout->addWidget(validateAnswer);userAnswer->setVisible(true); checkpointQuestion->setVisible(true); validateAnswer->setVisible(true);
@
-
If you need the same widgets again, I would only hide them, that typically works and we also do it here often...
-
I do that in other cases, but in this case I will not be able to use the QLineEdit widget again.
-
Well if you want your widget to have focus again when it is re-shown then explicitly give it the focus in code. See QWidget::setFocus().
-
I use @ userAnswer->setFocus(Qt::OtherFocusReason); @ and still nothing. I check the status if it is focused using hasFocus()
-
can you show a bit more code? I assume the bug is somewhere else...
-
The layout
@
mapsLayout = new QVBoxLayout;
mapsLayout->setAlignment(Qt::AlignTop);@this is the function which sets visible the 3 widgets and as comment, I inserted the part from the init method:
@void MapsWidget::enterAnswer()
{
// //for entering the answer
// checkpointQuestion = new QLabel();
// checkpointQuestion->setWordWrap(true);
// checkpointQuestion->setStyleSheet("QLabel { background-color : #505050 ; color : white; }");
// checkpointQuestion->setAlignment(Qt::AlignCenter);// validateAnswer = new QPushButton();
// validateAnswer->setText("Ok");
// // validateAnswer->setMaximumWidth(50);// userAnswer = new QLineEdit();
// userAnswer->setStyleSheet("QLineEdit { background-color : #505050 ; color : white; }");
// userAnswer->setPlaceholderText("Enter answer here");
// userAnswer->setAlignment(Qt::AlignCenter);// connect(validateAnswer, SIGNAL( pressed() ), this, SLOT( checkAnswer()) );
// //end part for entering the answercheckpointQuestion->setText("Cum se numeste pizzeria cu nume italienesc?"); //set the text to the right one userAnswer->setFocus(); mapsLayout->setAlignment(validateAnswer, Qt::AlignCenter); mapsLayout->addWidget(checkpointQuestion); mapsLayout->addWidget(userAnswer); mapsLayout->addWidget(validateAnswer); checkpointQuestion->setVisible(true); validateAnswer->setVisible(true); userAnswer->setVisible(true); userAnswer->setFocus(Qt::OtherFocusReason); userAnswer->setFocus(Qt::MouseFocusReason); qDebug() << userAnswer->hasFocus();
}
@This is the code where they should be removed from the layout:
@void MapsWidget::checkAnswer()
{
qDebug() << userAnswer->text();mapsLayout->removeWidget(checkpointQuestion); mapsLayout->removeWidget(userAnswer); mapsLayout->removeWidget(validateAnswer); checkpointQuestion->setVisible(false); userAnswer->setVisible(false); validateAnswer->setVisible(false); answerCorectitude = new QPushButton(); mapsLayout->setAlignment(answerCorectitude, Qt::AlignCenter); connect(answerCorectitude, SIGNAL( pressed() ), this, SLOT( answerCorectitudeButton()) ); if (userAnswer->text() == "test") { answerCorectitude->setText("Correct answer!"); answerCorectitude->setVisible(true); mapsLayout->addWidget(answerCorectitude); } else { answerCorectitude->setText("Wrong answer!"); answerCorectitude->setVisible(true); mapsLayout->addWidget(answerCorectitude); }
}@
-
What OS are you using ? I did a little test in win 7 and it worked well.
-
I'm on Mac OS 10.6.8 with QTCreator 2.2.1.
Well, have you tried to hide them and then setVisible(true) again? -
Yes. ( + (setStyleSheet & setPlaceholder) )
-
hmm ... awkward. Maybe I should try my whole project on a windows machine .... could be the possibility that the simulator to be different?
-
Well, I have not fully tested your code. Just create one dialog with vertical layout, and add in this layout one button and one line edit. When the button was pressed, just remove the line edit from layout and set invisible, and if pressed again, show the line edit and added to layout. This "pseudocode" work well.
-
Well, ok. Thank you
-
dear comunity,
i have a problem with creating a layout be qtcreator, my clas is derivated from QMAinWindow and i create in the window three Greupboxes and a layout, but the layout not work, if i tray to make it window larger the group boxes have still the same size. here is the source code from the constructor.
@ this->set_DICS_Window_Positio(this);
this->set_Left_Up_Group_Box(this);
this->set_Right_Up_Group_Box(this);
this->set_Down_Box(this);this->ptr_all_QGroupBox = new QList<QGroupBox*>; this->set_all_Group_Boxes(); this->top_grid_layout = new QGridLayout; top_grid_layout->addWidget(this->left_Up_GroupBox, 0, 0); top_grid_layout->addWidget(this->right_Up_GroupBox, 0 , 1); top_grid_layout->addWidget(this->down_GroupBox, 1 , 0, 1, 2); top_grid_layout->addWidget(this); this->setLayout(top_grid_layout);
@
the Gruoboxes are created on this way in a separated function:
@ this->left_Up_GroupBox = new QGroupBox(ptr_MW);
this->left_Up_GroupBox->setGeometry(10, 10, 380, 280);
this->left_Up_GroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
@sorry for my bad english.
Thank you very much in advance for your answer.