Results 1 to 10 of 10

Thread: Remove column from QGridLayout

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Remove column from QGridLayout

    I have a qgridlayout which consists of a grid 3 rows and X columns. It starts as one column. I then programatically add columns using "addWidget" adding to rows 0 and 1 in a new column. This works fine. I would like to then delete all the columns (except the first) then start over adding new columns the same way. I tried using "removeItem" but this does not seem to work.

    Qt Code:
    1. //Add columns
    2. QGridLayout *layout = ui->gridlayout;
    3. Qlabel label, label2;
    4. QGraphicsView gv, gv2;
    5.  
    6. layout->addWidget(label1,0,1)
    7. layout->addWidget(gv1,1,1)
    8. layout->addWidget(label2,0,2)
    9. layout->addWidget(gv2,1,2)
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Remove columns
    2. QGridLayout *layout = ui->gridlayout;
    3.  
    4. layout->columnCount() // 3 as expected
    5.  
    6. layout->removeItem(layout->itemAtPosition(0,1)
    7. layout->removeItem(layout->itemAtPosition(1,1)
    8.  
    9. layout->columnCount() // Still 3, expecting 2
    10.  
    11. layout->removeItem(layout->itemAtPosition(0,2)
    12. layout->removeItem(layout->itemAtPosition(1,2)
    13.  
    14. layout->columnCount() // Still 3, expecting 1
    To copy to clipboard, switch view to plain text mode 
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Remove column from QGridLayout

    Why is it important for you to have the layout with specific amount of column/rows?
    Layouts are not made for you to manage - but to take the layout management from you!
    Since you don't have to add N columns when you want the first item to be at N+1, similarly you don't have to do anything when you remove an item from the layout.
    Maybe if you explain why this is important for you, a better way of solving it can be suggested.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Remove column from QGridLayout

    I dont specifically want to change the layout directly. I am just removing items from the layout. I expect the layout to automatically remove the columns but it does not seem to be doing that.

    perhaps I implemented this in the wrong way.

    I would like to display the following in a box:

    first line: QLabel
    second line: QGraphicsView
    Third line: QSlider

    My program uses a set of images of the same thing taken over time. When the users clicks on a specific part of the image, I would like the bottom to fill with image tiles of the corresponding zoomed in section on all the other images.

    So when a user double clicks, I open each image and pull the tile out. I would like to display all the tiles in one row. I would like the date and time of the image to show on top. The first image has a QSlider below it to give the option for the user can move the slider and see the image difference on top of each other. To keep things aligned and to help with resizing, I put it all in a gridlayout and programatically add columns for each tile.

    Since the number of tiles may change, each time the user double clicks on a part of the image, I would like each tile to be removed to make room for the new tiles to be added.

    So far it is working, except if I double click on a different part of the image, I have no way to remove the tiles that I just added.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Remove column from QGridLayout

    So far it is working, except if I double click on a different part of the image, I have no way to remove the tiles that I just added.
    Why not?
    QLayout::removeItem()
    QLayout::removeWidget()

    That is the point I was making.
    You are trying to manage the layout - but you don't have to, this is what QLayout is for.
    Manage your widgets instead!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Remove column from QGridLayout

    That is what my first message said and my original question.
    I use QLayout::removeItem() but it doesn't get removed.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Remove column from QGridLayout

    i use qlayout::removeitem() but it doesn't get removed.
    Oh, I see now.
    But you "are doing it without doing it".
    What I mean is, you ask the layout for the items.
    Instead, you must have the pointers to the objects somewhere right, so use these pointers in remoevItem() - don't ask the layout for them.

    To do it the way you are doing it, you need to keep track of everything you add and remove to the layout, but why would you want to do that - that is what the layout it for!
    Asking the layout for the viewer in a given cell only guarantees you that you get a pointer to a viewer (if any) which is in that cell - but if your bookkeeping is wrong, you are not getting the viewer you expect, or, not any viewer at all.
    Last edited by high_flyer; 28th June 2012 at 14:37.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Remove column from QGridLayout

    Quote Originally Posted by high_flyer View Post
    Oh, I see now.
    To do it the way you are doing it, you need to keep track of everything you add and remove to the layout, but why would you want to do that - that is what the layout it for!
    I think I see what you mean.
    I was was not tracking the pointers after they are added to the layout.
    I was depending on the ability to find the pointer by asking the layout.

    Therefore, what would you propose is the best way I can accomplish this.

    I can try tracking all my pointers and deleting directly but as you say "thats what a layout is for"
    I can also ask the layout for the items, but that doesnt seem to be working right for me.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Remove column from QGridLayout

    Hmm... after thinking about it a bit more:

    I am not sure what's QLayout policy in a case when you add a viewer to an already occupied cell.
    Does it put the widgets on top of each other or creates a new row/column (cell).
    I'd expect the later which is the reason I thought working with the layout position is not reliable.
    However if the layout stacks the widgets, then your approach should work, but the question then is, which of the stacked widgets (in a cell) do you get?

    Test it.


    At any rate, you don't really need to track or manage anything, you just need to store the pointers of your widgets.
    A QVector, or a QList is ok.
    You don't need the layout position if you have the pointer.
    Unless you allocate your items as children, you need these pointers to delete them, otherwise you get a memory leak.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Mar 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Remove column from QGridLayout

    Reviving this very old thread as I have a similar problem...

    I have a QWidget that includes a QGridLayout Layout. This QGridLayout has on each row two QLineEdit's and one QPushButton, which are part of an Object of MyClass. The Layout has one header row at the top (row 0) and all Objects are stored in the ObjectList (a QList).

    I have written the following code to remove a row regarding a specific Object.

    Qt Code:
    1. // Deleting an Object
    2. Layout->removeWidget(Object->LineEdit1());
    3. Layout->removeWidget(Object->LineEdit2());
    4. Layout->removeWidget(Object->PushButton());
    5. ObjectList.removeOne(Object);
    6. delete Object; // Deletes its LineEdit1, LineEdit2 and PushButton
    To copy to clipboard, switch view to plain text mode 

    To add a row for a new Object, I have the following code.

    Qt Code:
    1. // Adding a new Object
    2. MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
    3. ObjectList.append(Object);
    4. Layout->addWidget(Object->LineEdit1(), ObjectList.size() + 1, 0);
    5. Layout->addWidget(Object->LineEdit2(), ObjectList.size() + 1, 1);
    6. Layout->addWidget(Object->PushButton(), ObjectList.size() + 1, 2);
    To copy to clipboard, switch view to plain text mode 

    For some reason this code does not always work according to my expectation. In some situations the new Object does become visible and in other situations not, or at least no new row becomes visible (while the new Object is actually created). It seems that an existing row is basically overwritten with a row for the new Object.

    Anyone a clue of what I am doing wrongly?

    Edit: I may have clarified the symptom a bit: It seems that if not the last row in the QGridLayout deleted but any other row, a subsequent addition of a new row overwrites a row that remained to exist. How to enforce QGridLayout to delete rows that do not contain any Widgets anymore?

    Reviving this very old thread as I have a similar problem...

    I have a QWidget that includes a QGridLayout Layout. This QGridLayout has on each row two QLineEdit's and one QPushButton, which are part of an Object of MyClass. The Layout has one header row at the top (row 0) and all Objects are stored in the ObjectList (a QList).

    I have written the following code to remove a row regarding a specific Object.

    Qt Code:
    1. // Deleting an Object
    2. Layout->removeWidget(Object->LineEdit1());
    3. Layout->removeWidget(Object->LineEdit2());
    4. Layout->removeWidget(Object->PushButton());
    5. ObjectList.removeOne(Object);
    6. delete Object; // Deletes its LineEdit1, LineEdit2 and PushButton
    To copy to clipboard, switch view to plain text mode 

    To add a row for a new Object, I have the following code.

    Qt Code:
    1. // Adding a new Object
    2. MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
    3. ObjectList.append(Object);
    4. Layout->addWidget(Object->LineEdit1(), ObjectList.size() + 1, 0);
    5. Layout->addWidget(Object->LineEdit2(), ObjectList.size() + 1, 1);
    6. Layout->addWidget(Object->PushButton(), ObjectList.size() + 1, 2);
    To copy to clipboard, switch view to plain text mode 

    For some reason this code does not always work according to my expectation. In some situations the new Object does become visible and in other situations not, or at least no new row becomes visible (while the new Object is actually created). It seems that an existing row is basically overwritten with a row for the new Object.

    Anyone a clue of what I am doing wrongly?

    Edit: I may have clarified the symptom a bit: It seems that if not the last row in the QGridLayout deleted but any other row, a subsequent addition of a new row overwrites a row that remained to exist. How to enforce QGridLayout to delete rows that do not contain any Widgets anymore?


    Added after 1 45 minutes:


    Ok, I found a solution, but I am not sure whether I like it... I changed the code to add an Object to:

    Qt Code:
    1. // Adding a new Object
    2. MyClass *Object = new MyClass; // Creates its LineEdit1, LineEdit2 and PushButton
    3. ObjectList.append(Object);
    4. int Count = Layout->rowCount();
    5. Layout->addWidget(Object->LineEdit1(), Count, 0);
    6. Layout->addWidget(Object->LineEdit2(), Count, 1);
    7. Layout->addWidget(Object->PushButton(), Count, 2);
    To copy to clipboard, switch view to plain text mode 

    Is this really the way it should be done? I would expect that this creates a lot of empty rows / cells in the QGridLayout that only consume memory (although probably not much).
    Last edited by Mini4Ever; 20th August 2016 at 10:57.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Remove column from QGridLayout

    The grid layout doesn't have any methods to remove cells, so if you need them removed you need to recreate the layout,

    Alternatively you can move all content below the cleared row upwards so that empty rows are at the end and then reuse those when adding new items.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 9th October 2012, 02:12
  2. QSqlTableModel remove column by name
    By qlands in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2011, 12:48
  3. Replies: 1
    Last Post: 7th April 2011, 21:18
  4. Limit number of row/column in QGridLayout
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2007, 19:54
  5. Remove a QLayout from QGridLayout
    By grosem in forum Qt Programming
    Replies: 18
    Last Post: 2nd January 2007, 11:19

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.