简体   繁体   English

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

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

How do I create a drop-down widget, such as a drop-down QLabel, drop-down QTextBrowser, etc.?如何创建下拉widget,比如下拉QLabel,下拉QTextBrowser等?

For example, I log information in a QTextBrowser, but I don't want it taking up space on the screen.例如,我在 QTextBrowser 中记录信息,但我不希望它占用屏幕空间。 So I want to be able to click a QToolbutton and have a scrollable QTextBrowser drop-down.所以我希望能够单击 QToolbutton 并有一个可滚动的 QTextBrowser 下拉菜单。 (A QComboBox would work too, but I can't just add each event as a separate item - I need the text to wrap, not be elided. Thus a drop-down QTextBrowser.) (QComboBox 也可以,但我不能只将每个事件添加为单独的项目 - 我需要文本换行,而不是被省略。因此下拉 QTextBrowser。)

Or, for example, I want a drop-down QLabel containing a picture, etc...或者,例如,我想要一个包含图片等的下拉 QLabel ...

Create a QWidgetAction for the drop-down widget, and add it to the tool-button's menu : 为下拉窗口小部件创建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.

相关问题 如何在PyQt5中更新QLabel? - How do I update a QLabel in PyQt5? Qt / PyQt:如何使用QMenu作为永久小部件? - Qt/PyQt: How do I use a QMenu as a permanent widget? 如何将html文件加载/显示到QTextBrowser小部件中? - How do I load/display an html file into my QTextBrowser widget? 如何在 PyQt5 中对齐 QLabel 上的中心图像? - How do I align centre image on QLabel in PyQt5? 在 PyQt5 中找到多个随机创建的小部件(如 QLabel、QPushButton 等)的 mousepress 事件的父级 - find parent of mousepress event of multiple randomly created widgets(like QLabel,QPushButton etc.) in PyQt5 如何在QT Designer中创建的PyQt4小部件上使用pyqtgraph绘制绘图? - How do I draw a plot using pyqtgraph on PyQt4 widget created in QT Designer? 如何在 Python tkinter 中自定义复选框、下拉列表等的位置? - How to customize position of checkboxes, drop down list, etc. in Python tkinter? 使用QT Designer和PyQt,如何用另一个.ui文件中定义的窗口小部件填充一个.ui文件中定义的空窗口小部件? - Using QT Designer and PyQt, how do I fill an empty widget defined in one .ui file with a widget defined in another .ui file? 如何在 PyQt 中从 QListWidget 拖放到 QLabel - How to make drag and drop from QListWidget to QLabel in PyQt 如何检测小部件是否在视线内? PyQt - How do I detect if a widget is in sight? PyQt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM