【问题标题】:QHBoxLayout add widget in a different orderQHBoxLayout 以不同的顺序添加小部件
【发布时间】:2019-11-19 02:17:16
【问题描述】:

编写 QHBoxLayout.addWidget() 将小部件添加到右侧。有没有办法让我把它添加到不同的位置,例如,将它插入到最后一个最右边和第二个最右边的小部件之间?

【问题讨论】:

    标签: python pyqt pyqt5 qlayout


    【解决方案1】:

    您可以使用insertWidget() 方法在任意位置插入小部件:

    例子:

    import sys
    
    from PyQt5 import QtWidgets
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QWidget()
        hlay = QtWidgets.QHBoxLayout(w)
        for i in range(8):
            label = QtWidgets.QLabel("label-{}".format(i))
            hlay.addWidget(label)
        # insert widget between label-6 and label-7
        hlay.insertWidget(hlay.count() - 1, QtWidgets.QPushButton("Press me"))
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-11
      • 2017-07-11
      • 2018-08-14
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2018-07-28
      相关资源
      最近更新 更多