Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:08:40+00:00 2026-06-06T15:08:40+00:00

I am having some issue with layout in pyqt. After closing the items from

  • 0

I am having some issue with layout in pyqt. After closing the items from the layout still if layout.count() returns the old item count. So I think .close() not really removing items from the layout. Here is a full working example.

import sys
from PyQt4 import QtGui,QtCore
class LayoutTest(QtGui.QWidget):
    def __init__(self):
        super(LayoutTest, self).__init__()
        self.vvbox = QtGui.QVBoxLayout()
        self.dvbox = QtGui.QVBoxLayout()
        vbox = QtGui.QVBoxLayout()
        vbox.addLayout(self.vvbox)
        vbox.addLayout(self.dvbox)
        self.setLayout(vbox)

        self.add_button = QtGui.QPushButton("Add Items")
        self.edit_button = QtGui.QPushButton("Remove Items")
        self.chk_button = QtGui.QPushButton("Check Items")

        self.vvbox.addWidget(self.add_button)
        self.vvbox.addWidget(self.edit_button)
        self.vvbox.addWidget(self.chk_button)

        self.connect(self.add_button, QtCore.SIGNAL("clicked()"), self.addButtons)
        self.connect(self.edit_button, QtCore.SIGNAL("clicked()"), self.removeButtons)
        self.connect(self.chk_button, QtCore.SIGNAL("clicked()"), self.checkItems)

        self.setGeometry(300, 200, 400, 300)

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            self.close()

    def addButtons(self):
        for i in range(0, 5):
            self.r_button = QtGui.QPushButton("Button %s " % i)
            self.dvbox.addWidget(self.r_button)

    def removeButtons(self):
        for cnt in range(self.dvbox.count()):
            self.dvbox.itemAt(cnt).widget().close()

    def checkItems(self):
        QtGui.QMessageBox.information(self, 'Count',"You have %s Items in Layout" % self.dvbox.count(), QtGui.QMessageBox.Ok)

def run():

    app = QtGui.QApplication(sys.argv)
    ex = LayoutTest()
    ex.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    run()

Just click two times in add button and after that delete the buttons. Then just check for the items. After closing also you will get you have n items in layout.

So what is the best way to remove widget from a layout other than closing ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:08:41+00:00Added an answer on June 6, 2026 at 3:08 pm

    Your comment is indeed a solution but rather than close use deleteLater. It is safer. With a bit of modification, I’d rewrite your method as:

    def removeButtons(self):
        for cnt in reversed(range(self.dvbox.count())):
            # takeAt does both the jobs of itemAt and removeWidget
            # namely it removes an item and returns it
            widget = self.dvbox.takeAt(cnt).widget()
    
            if widget is not None: 
                # widget will be None if the item is a layout
                widget.deleteLater()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some serious issues trying to tweak my layout in a table.
I am having some problems figuring out this issue. I have a server that
I'm having an issue getting some code to work. Is there a way to
I am having a strange issue with some of my packages in the same
I'm having an issue with a query I wrote where for some reason the
I'm having some issues with this layout and having a link that displays on
Some background: I am having an issue with one part of my program where
I'm having an issue with WebGrid paging; but first, here's some controller pseudo code:
I've been having some major issues with a layout I'm making specifically in Internet
Having some issues trying to create a fixed header layout for this jsfiddle: http://jsfiddle.net/4xk4D/107/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.