python - Qt/PyQt : How do I create a drop down widget, 比如QLabel、QTextBrowser等?

标签 python qt drop-down-menu widget pyqt

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

例如,我在 QTextBrowser 中记录信息,但我不希望它占用屏幕空间。所以我希望能够单击 QToolbutton 并有一个可滚动的 QTextBrowser 下拉菜单。 (QComboBox 也可以,但我不能只将每个事件添加为单独的项目 - 我需要文本换行,而不是被省略。因此下拉 QTextBrowser。)

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

最佳答案

创建一个 QWidgetAction对于下拉小部件,并将其添加到工具按钮的 menu :

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_())

关于python - Qt/PyQt : How do I create a drop down widget, 比如QLabel、QTextBrowser等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9076332/

相关文章:

python - Pandas to_sql 更改数据库表中的数据类型

c++ - Qt Creator 中 Qt Designer 中的 float IDE 窗口

qt - 如何获取 QML 列布局子列表?

c++ - 提高从客户端应用程序到 MS SQL 2008 数据库的批量导入速度

html - 下拉菜单无法在 IE 中显示

python - 在 Pyspark 中读取和保存图像文件

python - 在 specgram matplotlib 中切割未使用的频率

javascript - 隐藏 select2 多搜索框

python - 如何从 Excel 电子表格中仅读取满足特定条件的行到 Python 中?

css - 从 Angular 2 的下拉列表中删除事件类