[英]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.