Results 1 to 10 of 10

Thread: Remove column from QGridLayout

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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.

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.