如果缩小窗口/工具栏的大小/宽度,则将QpushButton放入下拉菜单中

问题描述 投票:0回答:1

我有一个装满QPushButton的QToolBar。当调整窗口大小以减小其宽度时,工具栏的宽度将减小。如果宽度减小到某个点以上,则按钮开始消失。我认为情况已经明白。

在某些情况下,某些人必须减小窗口宽度,因此无法设置最小宽度。

因此,无论窗口大小如何,所有这些按钮都应该可以访问。

我正在考虑将寡妇的尺寸减小时将这些按钮放入下拉菜单。 (还有另一种方法吗?)我什至不知道从哪里开始。如果有人可以告诉我该怎么做或将我指向正确的方向,那就太好了。

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_())
python python-3.x linux pyside2 qtoolbar
1个回答
0
投票

不是使用QPushButton,而是将以下操作添加到QToolBar:

toolbar.addAction("m", "m")
toolbar.addAction("w", "w")
toolbar.addAction("q", "q")
toolbar.setStyleSheet("QToolBar{background:grey;}"
                          "QToolButton {background:yellow ;"
                          "color: red;}")
© www.soinside.com 2019 - 2024. All rights reserved.