Reputation: 106
Hi i have buttons in my code i would like when the user press insert new button it will move all the other button one row below and create a new button just under the one it was pressed this is my code
bascily i am trying to move all the button in layout one row below and after i add the new button :
def Insert_Stage(self) :
button = self.sender()
idx = self.Layout.indexOf(button)
location = self.Layout.getItemPosition(idx)
x=location[0]
z=self.Layout.rowCount()
print(x,z)
while(z >x+1):
items= self.Layout.itemAt(z)
# setting the item as widget
widget=items.widget()
index= self.Layout.indexOf(widget)
loc=self.Layout.getItemPosition(index)
d=loc[0]
y=loc[1]
if y!=0:
#widget.move(d+100,d)
self.Layout.addWidget(widget,(d+1),1)
else:
self.Layout.addWidget(widget,d+1,0)
z-=1
stage=QtGui.QPushButton(self)
stage.setObjectName(button.objectName())
k=(int(button.objectName()[5:])+1)
stage.setText('stage%d'%k)
self.Layout.addWidget(stage,(location[0]+1),0)
Upvotes: 1
Views: 4259
Reputation: 243887
Assuming you are using a QVBoxLayout
you have to use the insertWidget()
method:
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QLineEdit):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtGui.QVBoxLayout(self)
for i in range(5):
btn = QtGui.QPushButton(
'button {}'.format(i),
clicked=self.on_clicked
)
lay.addWidget(btn)
@QtCore.pyqtSlot()
def on_clicked(self):
btn = self.sender()
ix = self.layout().indexOf(btn)
new_btn = QtGui.QPushButton(
"button {}".format(self.layout().count()),
clicked=self.on_clicked
)
self.layout().insertWidget(ix+1, new_btn)
if __name__ == '__main__':
import sys
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Upvotes: 3