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
Forum Updated to NodeBB v4.3 + New Features

Problem removing widgets from a layout

Scheduled Pinned Locked Moved Mobile and Embedded
20 Posts 5 Posters 24.1k Views 1 Watching
  • 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
    #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