alphanumeric
alphanumeric

Reputation: 19329

How to make QToolButton stretch inside layout

I have tried every possible "trick" I know to make QToolButton stretch from left layout edge to right. Nothing works. On another hand QListWidget stretches beautifully without a line of code. Please advise it is even possible!

enter image description here

from PyQt4 import QtGui, QtCore

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()
        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.toolbutton = QtGui.QToolButton()
        self.toolbutton.setText('QToolButton')
        self.toolmenu = QtGui.QMenu()
        for i in range(3):
            action = self.toolmenu.addAction("Menu Item " + str(i))
            action.setCheckable(True)
        self.toolbutton.setMenu(self.toolmenu)
        self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)

        self.listWidget=QtGui.QListWidget()

        myBoxLayout.addWidget(self.toolbutton)
        myBoxLayout.addWidget(self.listWidget)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

Upvotes: 1

Views: 747

Answers (1)

Becca codes
Becca codes

Reputation: 542

Set the size policy (the horizontal part) of the tool button to something other than "fixed".

toolbuttonSizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
self.toolbutton.setSizePolicy(toolbuttonSizePolicy)

Upvotes: 4

Related Questions