Results 1 to 2 of 2

Thread: adding and removing widgets

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default adding and removing widgets

    Hello!
    My program adds widgets (QPushButtons) like this:
    Qt Code:
    1. Layout->addWidget(MyWidget);
    To copy to clipboard, switch view to plain text mode 
    and removes them like this:
    Qt Code:
    1. Layout->removeWidget(MyWidget);
    To copy to clipboard, switch view to plain text mode 
    several times during the code execution.

    Although this simple approach works most of the time, sometimes I get weird results. Sometimes after removing my QPushButton, it no loger functions but it is still there and sitting on top of another widget that is supposed to replace it.
    Questions:
    1. Do you have to check for the presence of a widget before you attempt to remove it? How do you do it?
    2. Does something need to be specifically refreshed or redrawn after the widget has been removed?
    Thank you!

  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: adding and removing widgets

    Its hard with out knowing the inside of your code better, but here what I think:
    Sometimes after removing my QPushButton, it no loger functions but it is still there and sitting on top of another widget that is supposed to replace it.
    void QLayout::removeWidget ( QWidget * widget )

    Removes the widget widget from the layout. After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout.

    Note: The ownership of widget remains the same as when it was added.
    And when you add a widget:
    void QLayout::addItem ( QLayoutItem * item ) [pure virtual]

    Implemented in subclasses to add an item. How it is added is specific to each subclass.

    This function is not usually called in application code. To add a widget to a layout, use the addWidget() function; to add a child layout, use the addLayout() function provided by the relevant QLayout subclass.

    Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
    you need to explicitly delete the widget after removing it - or place it somewhere else.
    ==========================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.

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
  •  
Qt is a trademark of The Qt Company.