Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Having problems deleting and adding widgets to a layout.
Qt 6.11 is out! See what's new in the release blog

Having problems deleting and adding widgets to a layout.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 574 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.
  • B Offline
    B Offline
    blakejustblake
    wrote on last edited by
    #1

    So basically I've got a situation where I've got a square grid of QLineEdit widgets. So I've got a function that builds the grid that gets called by the constructor for the grid object and is signaled by pressing return on a QLineEdit to change the dimension, it looks something like this:

    void GridUI::buildGrid() {
        //Grab the dimension size from the input
        gridDimension = dimensionInput->text().toInt();
    
        //Delete all the widgets in the grid if they already exist
        //puzzleGridCell is a QGridLayout instantiated in the constructor
        QLayoutItem *toDelete;
        while ((toDelete = puzzleGridCell->takeAt(0)) != 0)
            delete toDelete->widget();
        
        //Create the grid
        for (int puzzCol = 0; puzzCol < gridDimension; ++puzzCol) {
            for (int puzzRow = 0; puzzRow < gridDimension; ++puzzRow) {
                QLineEdit *puzzBox = new QLineEdit("", this);
                puzzleGridCell->addWidget(puzzBox, puzzCol, puzzRow);
            }
        }
    }
    

    What's going wrong is that it's building properly to start, but when I change the dimension and press return it removes all of the QLineEdits, but doesn't seem to resize the grid or show the new QLineEdit widgets, I can tell through debugging that they're at least getting created and added to the QGridLayout, but they're just not showing up at all.

    Just wanted to see if there was something really obvious that I'm not aware of, or maybe if anyone's got any ideas on what to look at.

    jsulmJ 1 Reply Last reply
    0
    • B blakejustblake

      So basically I've got a situation where I've got a square grid of QLineEdit widgets. So I've got a function that builds the grid that gets called by the constructor for the grid object and is signaled by pressing return on a QLineEdit to change the dimension, it looks something like this:

      void GridUI::buildGrid() {
          //Grab the dimension size from the input
          gridDimension = dimensionInput->text().toInt();
      
          //Delete all the widgets in the grid if they already exist
          //puzzleGridCell is a QGridLayout instantiated in the constructor
          QLayoutItem *toDelete;
          while ((toDelete = puzzleGridCell->takeAt(0)) != 0)
              delete toDelete->widget();
          
          //Create the grid
          for (int puzzCol = 0; puzzCol < gridDimension; ++puzzCol) {
              for (int puzzRow = 0; puzzRow < gridDimension; ++puzzRow) {
                  QLineEdit *puzzBox = new QLineEdit("", this);
                  puzzleGridCell->addWidget(puzzBox, puzzCol, puzzRow);
              }
          }
      }
      

      What's going wrong is that it's building properly to start, but when I change the dimension and press return it removes all of the QLineEdits, but doesn't seem to resize the grid or show the new QLineEdit widgets, I can tell through debugging that they're at least getting created and added to the QGridLayout, but they're just not showing up at all.

      Just wanted to see if there was something really obvious that I'm not aware of, or maybe if anyone's got any ideas on what to look at.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @blakejustblake The new line edits are not shown because you do not call show() on them:

      QLineEdit *puzzBox = new QLineEdit("", this);
      puzzleGridCell->addWidget(puzzBox, puzzCol, puzzRow);
      puzzBox->show();
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      2
      • jsulmJ jsulm

        @blakejustblake The new line edits are not shown because you do not call show() on them:

        QLineEdit *puzzBox = new QLineEdit("", this);
        puzzleGridCell->addWidget(puzzBox, puzzCol, puzzRow);
        puzzBox->show();
        
        B Offline
        B Offline
        blakejustblake
        wrote on last edited by
        #3

        @jsulm Definitely something obvious I wasn't aware of. Thanks. So if add any children after initially showing a widget I need to additionally show them as well?

        jsulmJ 1 Reply Last reply
        0
        • B blakejustblake

          @jsulm Definitely something obvious I wasn't aware of. Thanks. So if add any children after initially showing a widget I need to additionally show them as well?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @blakejustblake Yes, you need to call show()

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          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