Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Problem removing widgets from a layout

Problem removing widgets from a layout

Scheduled Pinned Locked Moved Mobile and Embedded
20 Posts 5 Posters 23.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goosebumps4
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      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".

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        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?

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goosebumps4
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            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...

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goosebumps4
              wrote on last edited by
              #6

              I tried that, but when recreated they are resized awkwardly, although the first time they look ok.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goosebumps4
                wrote on last edited by
                #7

                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);
                

                @

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  If you need the same widgets again, I would only hide them, that typically works and we also do it here often...

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goosebumps4
                    wrote on last edited by
                    #9

                    I do that in other cases, but in this case I will not be able to use the QLineEdit widget again.

                    1 Reply Last reply
                    0
                    • Z Offline
                      Z Offline
                      ZapB
                      wrote on last edited by
                      #10

                      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().

                      Nokia Certified Qt Specialist
                      Interested in hearing about Qt related work

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goosebumps4
                        wrote on last edited by
                        #11

                        I use @ userAnswer->setFocus(Qt::OtherFocusReason); @ and still nothing. I check the status if it is focused using hasFocus()

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #12

                          can you show a bit more code? I assume the bug is somewhere else...

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goosebumps4
                            wrote on last edited by
                            #13

                            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 answer

                            checkpointQuestion->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);
                            }
                            

                            }@

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              cincirin
                              wrote on last edited by
                              #14

                              What OS are you using ? I did a little test in win 7 and it worked well.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                goosebumps4
                                wrote on last edited by
                                #15

                                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?

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  cincirin
                                  wrote on last edited by
                                  #16

                                  Yes. ( + (setStyleSheet & setPlaceholder) )

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    goosebumps4
                                    wrote on last edited by
                                    #17

                                    hmm ... awkward. Maybe I should try my whole project on a windows machine .... could be the possibility that the simulator to be different?

                                    1 Reply Last reply
                                    0
                                    • C Offline
                                      C Offline
                                      cincirin
                                      wrote on last edited by
                                      #18

                                      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.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        goosebumps4
                                        wrote on last edited by
                                        #19

                                        Well, ok. Thank you

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          Dantcho
                                          wrote on last edited by
                                          #20

                                          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.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved