adjustSize
ボタンを削除した後、ウィジェットを呼び出すだけです。デモは次のとおりです。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
_buttons = []
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.pushButtonRemove = QtGui.QPushButton(self)
self.pushButtonRemove.setText("Remove A Button!")
self.pushButtonRemove.clicked.connect(self.on_pushButtonRemove_clicked)
self.widgetButtons = QtGui.QWidget(self)
self.layoutButtons = QtGui.QHBoxLayout(self.widgetButtons)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.pushButtonRemove)
self.layout.addWidget(self.widgetButtons)
for buttonNumber in range(3):
pushButton = QtGui.QPushButton()
pushButton.setText("Button {0}".format(buttonNumber))
self._buttons.append(pushButton)
self.layoutButtons.addWidget(pushButton)
@QtCore.pyqtSlot()
def on_pushButtonRemove_clicked(self):
if self._buttons:
pushButton = self._buttons[-1]
self._buttons.pop()
self.layoutButtons.removeWidget(pushButton)
pushButton.deleteLater()
self.widgetButtons.adjustSize()
self.adjustSize()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())