Reputation: 119
here in my sample program i want to add widget to Center of the gridlayout.I used alignments methods but its not working.Can any one please help me how to adjust the my widget in center of the grid layout. Thank you in advance.
Given below is my sample code:
import sys
from PyQt4 import QtGui,QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.grid = QtGui.QGridLayout(self)
self.title_hbox = QtGui.QHBoxLayout()
self.title_hbox.addStretch()
self.close_pushbtn = QtGui.QPushButton("Close")
self.title_hbox.addWidget(self.close_pushbtn)
self.grid.addLayout(self.title_hbox,0,0)
self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
self.grid.addWidget(self.line,1,0)
self.order_hbox = QtGui.QHBoxLayout()
vbox = QtGui.QVBoxLayout()
label = QtGui.QLabel("address ")
vbox.addWidget(label)
hbox=QtGui.QHBoxLayout()
le =QtGui.QLabel("scroll area")
hbox.addWidget(le)
self.order_hbox.addLayout(vbox)
self.order_hbox.addLayout(hbox)
self.grid.addLayout(self.order_hbox,2,0)
self.addhbox = QtGui.QHBoxLayout()
self.add_btn = QtGui.QPushButton("Add") #@ Add button i want to adjust the center of the gridlayout (its tacking whole length..)
self.addhbox.addWidget(self.add_btn,QtCore.Qt.AlignCenter)
self.grid.addLayout(self.addhbox,3,0)
self.resize(800, 300)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
my add button is taking the whole width but i want to adjust the middle of the widget
Upvotes: 2
Views: 2989
Reputation: 243955
You have to change the QSizePolicy of the button so that it does not expand horizontally.
Do not abuse self, this is used "self" to keep a reference but in the case of layouts in rare cases you access it. On the other hand to maintain an order I declared the widgets first.
Finally I deleted some layout that were unnecessary.
def initUI(self):
self.close_pushbtn = QtGui.QPushButton(
text="Close",
sizePolicy=QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
)
self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
self.add_btn = QtGui.QPushButton(
text="Add",
sizePolicy=QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
)
label = QtGui.QLabel(text="address ")
le = QtGui.QLabel(text="scroll area")
title_hbox = QtGui.QHBoxLayout()
title_hbox.addWidget(self.close_pushbtn, alignment=QtCore.Qt.AlignRight)
order_hbox = QtGui.QHBoxLayout()
order_hbox.addWidget(label)
order_hbox.addWidget(le)
addhbox = QtGui.QHBoxLayout()
addhbox.addWidget(self.add_btn, alignment=QtCore.Qt.AlignCenter)
grid = QtGui.QVBoxLayout(self)
grid.addLayout(title_hbox)
grid.addWidget(self.line)
grid.addLayout(order_hbox)
grid.addLayout(addhbox)
self.resize(800, 300)
self.show()
Another option is to use stretch on each side:
def initUI(self):
self.close_pushbtn = QtGui.QPushButton(text="Close")
self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
self.add_btn = QtGui.QPushButton(text="Add")
label = QtGui.QLabel(text="address ")
le = QtGui.QLabel(text="scroll area")
title_hbox = QtGui.QHBoxLayout()
title_hbox.addStretch()
title_hbox.addWidget(self.close_pushbtn)
order_hbox = QtGui.QHBoxLayout()
order_hbox.addWidget(label)
order_hbox.addWidget(le)
addhbox = QtGui.QHBoxLayout()
addhbox.addStretch()
addhbox.addWidget(self.add_btn)
addhbox.addStretch()
grid = QtGui.QVBoxLayout(self)
grid.addLayout(title_hbox)
grid.addWidget(self.line)
grid.addLayout(order_hbox)
grid.addLayout(addhbox)
self.resize(800, 300)
self.show()
Upvotes: 1