I have a QToolBar full of QPushButtons. As the window is resized to reduce its width, the width of toolbar is reduced. If the width is reduced beyond a certain point, the buttons starts disappearing. I think the situations is understood.
There are certain situations when someone has to reduce the window width so setting minimum width is out of the question.
So basically, all those buttons should be accessible regardless of the window size.
I was thinking of putting those buttons into a drop down menu as the widow size is being reduced. (Is there another way?) I don’t even know where to start with this. If somebody could tell me how to do this or point me in the right direction, that’d be great.
import sys from PySide2.QtWidgets import * from PySide2 import * class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.resize(500, 400) self.setMinimumSize(200, 200) toolbar = QToolBar() toolbar.setStyleSheet("QToolBar{" "background: rgb(60, 60, 60);}") toolbar.setFixedHeight(30) a = QPushButton() a.setFixedSize(25, 25) a.setText("a") a.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") b = QPushButton() b.setFixedSize(25, 25) b.setText("b") b.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") c = QPushButton() c.setFixedSize(25, 25) c.setText("c") c.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") d = QPushButton() d.setFixedSize(25, 25) d.setText("d") d.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") e = QPushButton() e.setFixedSize(25, 25) e.setText("e") e.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") f = QPushButton() f.setFixedSize(25, 25) f.setText("f") f.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") g = QPushButton() g.setFixedSize(25, 25) g.setText("g") g.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") h = QPushButton() h.setFixedSize(25, 25) h.setText("h") h.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") i = QPushButton() i.setFixedSize(25, 25) i.setText("i") i.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") j = QPushButton() j.setFixedSize(25, 25) j.setText("j") j.setStyleSheet("QPushButton{" "color: red;" "background: yellow}") k = QPushButton() k.setFixedSize(25, 25) k.setText("k") k.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") l = QPushButton() l.setFixedSize(25, 25) l.setText("l") l.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") m = QPushButton() m.setFixedSize(25, 25) m.setText("m") m.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") n = QPushButton() n.setFixedSize(25, 25) n.setText("n") n.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") o = QPushButton() o.setFixedSize(25, 25) o.setText("o") o.setStyleSheet("QPushButton{" "color: red;" "background: yellow;}") toolbar.addWidget(a) toolbar.addWidget(b) toolbar.addWidget(c) toolbar.addWidget(d) toolbar.addWidget(e) toolbar.addWidget(f) toolbar.addWidget(g) toolbar.addWidget(h) toolbar.addWidget(i) toolbar.addWidget(j) toolbar.addWidget(k) toolbar.addWidget(l) toolbar.addWidget(m) toolbar.addWidget(n) toolbar.addWidget(o) #solution toolbar.addAction("m", "m") toolbar.addAction("w", "w") toolbar.addAction("q", "q") toolbar.setStyleSheet("QToolBar{background: grey;}" "QToolButton { background:yellow ;" "color: red;}") layout = QVBoxLayout() layout.addWidget(toolbar) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = MainWindow() MainWindow.show() sys.exit(app.exec_())
Advertisement
Answer
Instead of using QPushButtons, I’ve added the following actions to the QToolBar:
toolbar.addAction("m", "m") toolbar.addAction("w", "w") toolbar.addAction("q", "q") toolbar.setStyleSheet("QToolBar{background:grey;}" "QToolButton {background:yellow ;" "color: red;}")