I don't understand what mechanic/trigger causes the layout to update. In the simple example I created, the button's text is updated real-time inside the method, but the layout does not update until after the method is completed even though "I should see 2 button(s)" is correctly reported. How do I get the layout/window to add the button to the layout real-time?
import sys
import time
from PySide import QtCore, QtGui
class Form(QtGui.QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.button = QtGui.QPushButton("Add")
self.newButton= QtGui.QPushButton("")
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.connect(self.button, QtCore.SIGNAL("clicked()"),self.addButton)
print()
def addButton(self):
self.button.setText("Clicked")
if self.layout.count() > 1:
self.layout.itemAt(1).widget().deleteLater()
self.repaint()
self.layout.update()
print("I should see " + str(self.layout.count()) + " button(s)")
time.sleep(3)
self.layout.addWidget(self.newButton)
self.repaint()
self.layout.update()
print("I should see " + str(self.layout.count()) + " button(s)")
time.sleep(3)
self.button.setText("")
self.button.setEnabled(False)
self.newButton.setText("New")
app = QtGui.QApplication(sys.argv)
a=Form()
a.show()
app.exec_()
Please explain or demonstrate how to make the new button appear inside the method.