本文介紹了QHBoxLayout 以不同的順序添加小部件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
編寫 QHBoxLayout.addWidget() 將小部件添加到右側.有沒有辦法讓我將它添加到不同的位置,例如,將它插入到最后一個最右邊和第二個最右邊的小部件之間?
解決方案
您可以使用
writing QHBoxLayout.addWidget() adds the widget to the right. Is there a way for me to add it in in a different position, for example, insert it between the last rightmost and second-rightmost widget?
解決方案
You can insert a widget in any position using the insertWidget()
method:
Example:
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_())
這篇關于QHBoxLayout 以不同的順序添加小部件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!