Results 1 to 6 of 6

Thread: Trying to refresh a QGridLayout

  1. #1
    Join Date
    Jul 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Trying to refresh a QGridLayout

    I've used Qt Designer to create a QWidget with a grid layout on it, containing checkboxes which are dynamically created in my code and correspond to a number in a file on my computer. When a file is changed on the machine, I need the layout to refresh with a number of check boxes equal to a new specified number in the file. I'm trying to use the following code:

    Qt Code:
    1. //Refresh the layout
    2. ui.TPCheckBoxLayout_G->invalidate();
    3. ui.TPCheckBoxLayout_G->activate();
    4. ui.TPCheckBoxLayout_G->update();
    5. QWidget::repaint();
    To copy to clipboard, switch view to plain text mode 
    I call this code right after I delete all of the items from the layout, using the following code:

    Qt Code:
    1. //Refresh checkboxes and buttons
    2. int rowCount = ui.TPCheckBoxLayout_G->rowCount();
    3. int columnCount = ui.TPCheckBoxLayout_G->columnCount();
    4. for(int i=0; i<rowCount; i++)
    5. {
    6. for(int j=0; j<columnCount; j++)
    7. {
    8. //Clear the TP layout
    9. ui.TPCheckBoxLayout_G->removeItem(ui.TPCheckBoxLayout_G->itemAtPosition(i,j));
    10. delete ui.TPCheckBoxLayout_G->itemAtPosition(i,j);
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    However, after this code finished, the new boxes are present, but they appear over the old boxes, which don't exist in my code! My new stuff appears, but the old stuff doesn't go away. Any idea what I'm doing wrong?
    Last edited by jpn; 9th July 2008 at 19:16. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trying to refresh a QGridLayout

    When you modify a layout by adding or removing a widget from it, it gets updated by itself.

    Your problem is that removing widgetss from the layout doesn't cause them to be deleted. You are deleting layout items, not widgets contained in them.

  3. #3
    Join Date
    Jul 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to refresh a QGridLayout

    Then what steps should I take to properly delete the old items? Since I create them dynamically, they all have the same name and I don't have access to it. Code to add check boxes:

    Qt Code:
    1. msCount = 0;
    2. [loop]
    3. QCheckBox* chb = new QCheckBox();
    4. chb->setText(tempName);
    5. chb->setCheckState(Qt::Unchecked);
    6. connect(chb, SIGNAL(stateChanged(int)), this, SLOT(on_checkbox_change_MS(int)));
    7. ui.MSCheckBoxLayout_G->addWidget(chb,0,msCount);
    8. msCount++;
    9. [end of loop]
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 9th July 2008 at 19:16. Reason: missing [code] tags

  4. #4
    Join Date
    Jul 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to refresh a QGridLayout

    I figured it out...here's help to anybody who comes across this in the future. Pretty obvious, but it took me a while to figure it out...

    Qt Code:
    1. //Clear the TP layout
    2. QWidget* widget = ui.TPCheckBoxLayout_G->itemAtPosition(i,j)->widget(); ui.TPCheckBoxLayout_G->removeItem(ui.TPCheckBoxLayout_G->itemAtPosition(i,j));
    3. delete ui.TPCheckBoxLayout_G->itemAtPosition(i,j);
    4. delete widget;
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 9th July 2008 at 19:16. Reason: missing [code] tags

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trying to refresh a QGridLayout

    It would be simpler to store a list of widgets:
    Qt Code:
    1. QList<QWidget *> widgets;
    2. QWidget *w = new ....;
    3. widgets <<w;
    To copy to clipboard, switch view to plain text mode 
    Then you could just do:
    Qt Code:
    1. qDeleteAll(widgets);
    2. widgets.clear();
    To copy to clipboard, switch view to plain text mode 

    Alternatively if all your widgets are checkboxes (or some other particular widget classes), you can create the list on demand:
    Qt Code:
    1. QList<QCheckBox*> list = findChildren<QCheckBox*>();
    2. qDeleteAll(list);
    To copy to clipboard, switch view to plain text mode 

    Then you can insert new widgets into the layout...

  6. #6
    Join Date
    Apr 2009
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Trying to refresh a QGridLayout

    Quote Originally Posted by zlacelle View Post
    I figured it out...here's help to anybody who comes across this in the future. Pretty obvious, but it took me a while to figure it out...

    Qt Code:
    1. //Clear the TP layout
    2. QWidget* widget = ui.TPCheckBoxLayout_G->itemAtPosition(i,j)->widget(); ui.TPCheckBoxLayout_G->removeItem(ui.TPCheckBoxLayout_G->itemAtPosition(i,j));
    3. delete ui.TPCheckBoxLayout_G->itemAtPosition(i,j);
    4. delete widget;
    To copy to clipboard, switch view to plain text mode 
    Using Qt 4.5 on Linux I found that the index count needs resetting to 0 each time so I've just used this:

    Qt Code:
    1. while(layout->count() > 0) {
    2. QWidget* widget = layout->itemAt(0)->widget();
    3. layout->removeWidget(widget);
    4. delete widget;
    5. }
    To copy to clipboard, switch view to plain text mode 

    ...or alternatively remove the items in reverse from the end of the layout.

    chris

Similar Threads

  1. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 14:01
  2. Qt 3.3 QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2007, 18:40
  3. Refresh QDataTable
    By shamik in forum Qt Programming
    Replies: 25
    Last Post: 1st December 2006, 06:04
  4. QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 29th June 2006, 21:34
  5. QPixmap and QGridLayout
    By Talon_Karrde in forum Qt Programming
    Replies: 5
    Last Post: 22nd February 2006, 13:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.