簡體   English   中英

Qt/PyQt:如何創建下拉小部件,例如 QLabel、QTextBrowser 等?

[英]Qt/PyQt: How do I create a drop down widget, such as a QLabel, QTextBrowser, etc.?

如何創建下拉widget,比如下拉QLabel,下拉QTextBrowser等?

例如,我在 QTextBrowser 中記錄信息,但我不希望它占用屏幕空間。 所以我希望能夠單擊 QToolbutton 並有一個可滾動的 QTextBrowser 下拉菜單。 (QComboBox 也可以,但我不能只將每個事件添加為單獨的項目 - 我需要文本換行,而不是被省略。因此下拉 QTextBrowser。)

或者,例如,我想要一個包含圖片等的下拉 QLabel ...

為下拉窗口小部件創建QWidgetAction ,並將其添加到工具按鈕的菜單中

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QHBoxLayout(self)
        self.button = QtGui.QToolButton(self)
        self.button.setPopupMode(QtGui.QToolButton.MenuButtonPopup)
        self.button.setMenu(QtGui.QMenu(self.button))
        self.textBox = QtGui.QTextBrowser(self)
        action = QtGui.QWidgetAction(self.button)
        action.setDefaultWidget(self.textBox)
        self.button.menu().addAction(action)
        layout.addWidget(self.button)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(100, 60)
    window.show()
    sys.exit(app.exec_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM